The actual location of a variable in memory is system dependent. A programmer cannot know the address of a variable immediately. We can retrieve the address of a variable by using the address of (&) operator. Let’s look at the following example:
1 2 3 4 5 | int a = 10; int *p; p = &a; |
In the above example, let the variable a is stored at memory address 5000. This can be retrieved by using the address of operator as &a. So the value stored in variable p is 5000 which is the memory address of variable a. So, both the variable a, and p point to the same memory location.
Declaring and Initializing Pointer Variables
Declaration
In C, every variable must be declared for its type. Since pointer variables contain addresses that belong to a specific data type, they must be declared as pointers before we use them. The syntax for declaring a pointer is as shown below:
1 2 3 | datatype *pointer–name; |
This tells the compiler three things about the variable pointer-name. They are:
- The asterisk (*) tells that the variable pointer-name is a pointer variable.
- pointer-name needs a memory location.
- pointer-name points to a variable of type datatype.
Consider the following example for declaring pointers:
1 2 3 4 | int *p; float *p; |
Initialization
After declaring a pointer variable we can store the memory address into it. This process is known as initialization. All uninitialized pointers will have some unknown values that will be interpreted as memory addresses. Since compilers do not detect these errors, the programs with uninitialized pointers will produce erroneous results.
Once the pointer variable has been declared, we can use the assignment operator to initialize the variable. Consider the following example:
1 2 3 4 5 | int var = 20; int *p; p = &var; |
In the above example, we are assigning the address of variable var to the pointer variable p by using the assignment operator. We can also combine the declaration and initialization into a single line as shown below:
1 2 3 4 | int var = 20; int *p = &var; |
Accessing a Variable Using Pointer
After declaring and initializing a pointer variable, we can access the value of the variable pointed by the pointer variable using the * operator. The * operator is also known as indirection operator or dereferencing operator. Consider the following example:
1 2 3 4 5 | int var = 20; int *p = &var; printf(”Var = %d”, *p); |
In the above example, we are printing the value of the variable var by using the pointer variable p along with the * operator.
Pointers and Arrays
When an array is declared in a program, the compiler allocates a base address and sufficient amount of storage to contain all the elements of the array in contiguous (continuous) memory locations. The base address is the location of the first element (index 0) of the array. The compiler also defines the array name as a constant pointer to the first element. Suppose array x is declared as shown below:
1 2 3 | int x[5] = {1,2,3,4,5}; |
Suppose the array is allocated memory as shown below:
Take your time to comment on this article.