[Legacy Content] This page contains very old information, which might not be valid anymore, they are only kept here for legacy purposes.
if you have any inquiries, feel free to contact me! Click here
Created: Tuesday, 12 November 2013 Written by Ehab Eldeeb
Different from other normal variables which can store values, pointers are special variables that can hold the address of a variable. Since they store memory address of a variable, the pointers are very commonly said to “point to variables”. Lets try to understand the concept.
As shown in the above diagram:
A pointer is declared as :
<pointer type> *<pointer-name>
In the above declaration :
An example of a pointer declaration can be :
char *chptr;
In the above declaration, ‘char’ signifies the pointer type, chptr is the name of the pointer while the asterisk ‘*’ signifies that ‘chptr’ is a pointer variable.
A pointer is initialized in the following way :
<pointer declaration(except semicolon)> = <address of a variable> OR <pointer declaration> <name-of-pointer> = <address of a variable>
Note that the type of variable above should be same as the pointer type.(Though this is not a strict rule but for beginners this should be kept in mind).
For example :
char ch = 'c'; char *chptr = &ch; //initialize OR char ch = 'c'; char *chptr; chptr = &ch //initialize
In the code above, we declared a character variable ch which stores the value ‘c’. Now, we declared a character pointer ‘chptr’ and initialized it with the address of variable ‘ch’.
Note that the ‘&’ operator is used to access the address of any type of variable.
A pointer can be used in two contexts.
Context 1: For accessing the address of the variable whose memory address the pointer stores.
Again consider the following code :
char ch = 'c'; char *chptr = &ch;
Now, whenever we refer the name ‘chptr’ in the code after the above two lines, then compiler would try to fetch the value contained by this pointer variable, which is the address of the variable (ch) to which the pointer points. i.e. the value given by ‘chptr’ would be equal to ‘&ch’.
For example :
char *ptr = chptr;
The value held by ‘chptr’ (which in this case is the address of the variable ‘ch’) is assigned to the new pointer ‘ptr’.
Context 2: For accessing the value of the variable whose memory address the pointer stores.
Continuing with the piece of code used above :
char ch = 'c'; char t; char *chptr = &ch; t = *chptr;
We see that in the last line above, we have used ‘*’ before the name of the pointer. What does this asterisk operator do?
Well, this operator when applied to a pointer variable name(like in the last line above) yields the value of the variable to which this pointer points. Which means, in this case ‘*chptr’ would yield the value kept at address held by chptr. Since ‘chptr’ holds the address of variable ‘ch’ and value of ‘ch’ is ‘c’, so ‘*chptr’ yeilds ‘c’.
When used with pointers, the asterisk ‘*’ operator is also known as ‘value of’ operator.
Consider the following code :
CODE :
#include <stdio.h> int main(void) { char ch = 'c'; char *chptr = &ch; int i = 20; int *intptr = &i; float f = 1.20000; float *fptr = &f; char *ptr = "I am a string"; printf("\n [%c], [%d], [%f], [%c], [%s]\n", *chptr, *intptr, *fptr, *ptr, ptr); return 0; }
OUTPUT :
$ ./pointers [c], [20], [1.200000], [I], [I am a string]
To debug a C program, use gdb. The above code covers all the common pointers. The first three of them are very trivial now to understand so lets concentrate on the fourth one. In the fourth example, a character pointer points to a string.
In C, a string is nothing but an array of characters. So we have no staring pointers in C. Its the character pointers that are used in case of strings too.
Now, coming to the string, when we point a pointer to a string, by default it holds the address of the first character of the string. Lets try to understand it better.
The string, ‘I am String’ in memory is placed as shown below:
Since characters occupy one byte each, so they are placed like above in the memory. Note the last character, its a null character which is placed at the end of every string by default in C. This null character signifies the end of the string.
Now coming back to the point, any character pointer pointing to a string stores the address of the first character of the string. In the code above, ‘ptr’ holds the address of the character ‘I’ ie 1001. Now, when we apply the ‘value of’ operator ‘*’ to ‘ptr’, we intend to fetch the value at address 1001 which is ‘I’ and hence when we print ‘*ptr’, we get ‘I’ as the output.
Also, If we specify the format specifier as ‘%s’ and use ‘ptr’ (which contains the starting address of the string), then the complete string is printed using printf. The concept is that %s specifier requires the address of the beginning byte of string to display the complete string, which we provided using ‘ptr’ (which we know holds the beginning byte address of the string). This we can see as the last print in the output above.
Source: thegeekstuff.com
[Click on the image to enlarge]