When instead of a value, we are interested in the address of a variable, we can create a special variable which is capable of doing this task. Such a variable is called "Pointer". It can point to any memory location of the computer's RAM. This feature makes them not only powerful but also very dangerous. If not handled properly pointers can point to any location and cause serious damage to the system. Virus programmer world over use them for creating harmful viruses.
Declaring a pointer is similar to declaring a variable with just one difference, that is the pointer name is prefixed with an "*" mark. The "*" tells the C compiler that we want to declare a pointer variable, i.e., to set aside however many bytes are required to store an address in the memory.
int *ptr;
here, "int" is the datatype of the variable of which we want to store the address. Which means if we want to store the address of a float we will declare the pointer as
float *myptr;
In both the above examples, ptr and myptr are just variable names and can be replaced by any other legal variable name.
Intialising the pointer
Once the pointer has been declared it must be assigned a value which it can manage. Just as an integer variable can store numerical values, a pointer can store address of other variables. It is a good practice to initialise a pointer as soon as it is declared or else it can point to any random address in the memory causing the system to crash. If we don't have a variable to make the pointer to we should the use the macro "NULL" to make the pointer a null pointer. A null pointer is the one which is not currently pointing to a legal variable. If there is a variable to which it can point then the unary operator "&" (Address of) can be used to make the pointer point to something.
int x=10; --> line 1
int *ptr; --> line 2
ptr=&x; --> line 3
The first line simply declares a variable named "x" with a value of 10. The second line declares a pointer named "ptr". Note that the name is prefixed with an "*" symbol. The third line assigns the address of x to ptr. To initialise a pointer we will use the "&" operator which will assign the address of x to the pointer "ptr". What will the address x? Nobody knows it for sure,since it is not in our hands to assign the address to a variable we can just assume an imaginary address, say "1024". Therefore if x's value is 10, ptr's value is the address of "x" which we are assuming as "1024". Now if we print the value of both "x" and "ptr" using the following statements,
printf("\nThe value of x is %d",x);
printf("\nThe value of ptr is %d",ptr);
You will see that the first statement will print "10" since it is the value of "x", but the second statement will print "1024" since this is the value it is holding. If you don't see "1024" on screen as ptr's value but -12 or -14 don't lose heart since it the integer representation of the memory address. Moreover "1024" is just the assumed address which will vary from system to system.
With a pointer we can not only print the address of the variable but also its value. To print the value of a variable through a pointer we will have to use what we call as the "dereferencing operator" which is an asterisk and it is used as follows:
printf("\nThe value of x, printed through the pointer is %d",*ptr);
Note the use of "*" while printing the value. This will print the value the value of the variable of which ptr is a pointer. In our example, ptr is a pointer of x, therefore the value printed will be 10.
Similarly, if we write a statement like the following
*ptr = 123;
will copy 123 to the address pointed to by ptr. Thus if ptr "points to" (contains the address
of) x, the above statement will set the value of x to 123. That is, when we use the '*' this
way we are referring to the value of that which ptr is pointing to, not the value of the
pointer itself.
Example 1
#include
#include
void main()
{
int x;
int *xptr;
x=100;
xptr=&x; // initialising the pointer by assigning it the add. of x
printf("\nValue of x is %d",x);
printf("\nAddress of x is %d",xptr); // will print the add. of x
printf("\nValue of x through the pointer is %d",*xptr);//will print the value of x
}
Example 2
#include
#include
void main()
{
int x, y;
int *xptr,*yptr;
x=100;
y=200
xptr=&x;
yptr=&y;
printf("\nValue of x is %d",x); // will print 100
printf("\nAddress of x is %d",xptr); will print the address of a
printf("\nValue of x through the pointer is %d",*xptr); // will print 100
printf("\nValue of y is %d",y); // will print 200
printf("\nAddress of y is %d",yptr); will print the address of y
printf("\nValue of y through the pointer is %d",*yptr); // will print 200
xptr=yptr; // making xptr point to what yptr is pointing to. This will make both pointers // pointing towards y
printf("\nValue of x is %d",x); // will print 100
printf("\nAddress of x is %d",xptr); // will print the address of y since the pointer has been //modified
printf("\nValue of x through the pointer is %d",*xptr);// will print 200
}
Description
In the above program we have declared 2 variables namely x and y, and 2 pointer xptr and yptr which
point to x and y respectively. When the value are printed, x prints 100,xptr prints its value which is the address of x, and *xptr prints the value of x. Similarly y prints 200, yptr prints address of y and *yptr prints the value of y. The statement "xptr=yptr" assigns the value of yptr(that is address of y) to xptr. This means that now both the pointers are pointing to y . Now that xptr is pointing to y , printing its details again will print the same values which yptr was printing since they are both pointers of y . The value of a will still be 100 since it has not been modified.
Example 3
#include
#include
void main()
{
int x=5,*xptr
xptr=&x;
printf("\nx = %d",x);// 5
printf("\nxptr = %d",xptr);// address of x
printf("\nValue at ptr = %d",*xptr);// 5
x++; // First increment
printf("\nAfter incrementing the value of x by 1 ");
printf("\nx = %d",x);// 6
printf("\nxptr = %d",xptr);// address of x , same as before
printf("\nValue at xptr = %d",*xptr);// 6
*xptr++; // Second increment
printf("\nAfter incrementing the value at xptr by 1 ");
printf("\nx= %d",x);// 7
printf("\nxptr = %d",xptr);// address of a , same as before
printf("\nValue at xptr = %d",*xptr);// 7
xptr++; // Third increment
printf("\nAfter incrementing the value of xptr by 1 ");
printf("\nx = %d",x);// 7
printf("\nxptr = %d",xptr);// a new address
printf("\nValue at ptr = %d",*xptr);// a new value (garbage)
}
0 comments:
Post a Comment