To make full use of the C Programming language, you have to have a very good understanding of pointers. For most people it will take some time to fully understand pointers. So be patient. You have to learn pointers because they are used everywhere in the C language. Once you master the use of pointers, you will use them everywhere. OK, enough pep talk, let’s start.
Pointers are used (in the C language) in three different ways:
- To create dynamic data structures.
- To pass and handle variable parameters passed to functions.
- To access information stored in arrays. (Especially if you work with links).
Pointers are also used by experienced programmers to make the code more efficient and thus faster.
So why use pointers? Why don’t we use arrays to create data structures?
The answer is simple. With an array you have to declare its maximum size (for every dimension) at the beginning. Let’s say you create an array that can hold a maximum of twenty megabytes. When the array is declared, the twenty megabytes is claimed. Now this time you have only data for ten megabytes.
(A next time it could be fifteen megabytes or five megabytes). So in this case ten megabytes of memory is wasted, because only ten megabytes from the twenty is used.
This is where pointers come in. With pointers, you can create dynamic data structures. Instead of claiming the memory up-front, the memory is allocated (from the heap) while the program is running. So the exact amount of memory is claimed and there is no waste. Even better, memory not used will be returned to the heap. (Freed memory can be used for other programs).
Pointer basics
To better understand pointers, it sometimes helps to compare a “normal variable” with a pointer.
When a “normal variable” is declared, memory is claimed for that variable. Let’s say you declare an integer variable MYVAR. Four bytes of memory is set aside for that variable. The location in memory is known by the name MYVAR. At the machine level that location has a memory address.
A pointer differs in the way that a pointer is a variable that points to another variable. A pointer holds the memory address of that variable. That variable contains a value. Pointers are also called address variables because they contain the addresses of other variables.
Example: We have a piece of memory with a start address of 0x2000. That piece of memory contains a value and is named MYEXAMPLE.
(This is in fact a variable). We also have a pointer MYPOINT. In this case, our pointer MYPOINT contains the address 0x2000. This is the address of MYEXAMPLE so we say MYPOINT points to MYEXAMPLE. So there is the pointer and the value pointed to. (You can use a pointer in a certain way to get the value at the address to which the pointer points). Many novice programmers get pointers and their contents confused.
So from now on every pointer starts with the extension ptr_ . (for example: ptr_MYPOINT).
So from now on every pointer starts with the extension ptr_ . (for example: ptr_MYPOINT).
To declare a pointer you have to put an * in front of its name. A pointer can be typed or untyped. (A typed pointer points to a particular variable type such as an integer. An untyped pointer points to any data type). See the following example of a declaration of a typed pointer and an untyped pointer:
#include<stdio.h>
int main()
{
int *ptr_A; /* A typed pointer */
void *ptr_B; /* A untyped pointer */
return 0;
}
Put the address of an integer into a “pointer to an integer” by using the & operator (address operator)
in front of the integer, to get the integer’s address.
in front of the integer, to get the integer’s address.
Let’s take a look at an example:
#include<stdio.h>
int main()
{
int x;
int *ptr_p;
x = 5;
ptr_p = &x;
return 0;
}
Note: The integer x contains the value 5 on a specific address. The address of the integer x is copied in the pointer ptr_p. So ptr_p points to the address of x. In short: ptr_p = &x; means, “Assign to ptr_p the address of x.”
To access the value of the integer that is being pointed to, you have to dereference the pointer. The * is used to dereference a pointer. Take a look at the following example:
#include<stdio.h>
int main()
{
int x,y;
int *ptr_p;
x = 5;
ptr_p = &x;
y = *ptr_p;
printf("%d\n", y);
return 0;
}
Note: The integer x has a value of five. The pointer ptr_p gets the address of integer x.
The value pointed to is *ptr_p. (in this case five). So the integer y now contains the value of five.
The value pointed to is *ptr_p. (in this case five). So the integer y now contains the value of five.
Take a look at the following example:
#include<stdio.h>
int main()
{
int x;
int *ptr_p;
x = 5;
ptr_p = &x;
*ptr_p = 10;
printf("%d\n", x);
return 0;
}
Note: First the ptr_p contains the address of the integer x (ptr_p = &x). Then we change the value of the integer where ptr_p is pointing to (*ptr_p = 10;). The integer x now equals ten.
That’s all for this tutorial.
No comments:
Post a Comment