If you ask me which is best Programming construct in C ? Any day I would choose Pointers.
We already know that variables will have following attribute
- Name
- Value
- Address
#include <stdio.h> int main() { int i = 10; float f = 2.3; char c = 'a'; printf("Variable i: Sizeof-%ld ;Value - %d; Address - %p\n", sizeof(i), i, &i); printf("Variable f: Sizeof-%ld ;Value - %f; Address - %p\n", sizeof(f), f, &f); printf("Variable c: Sizeof-%ld ;Value - %c; Address - %p\n", sizeof(c), c, &c); return 0; }
Output of above program is
Variable i: Sizeof-4 ;Value - 10; Address - 0x7fff05649cf4 Variable f: Sizeof-4 ;Value - 2.300000; Address - 0x7fff05649cf8 Variable c: Sizeof-1 ;Value - a; Address - 0x7fff05649cff
Pictorially representing it
As we understand address of variable is 8 byte(64 bit) in length. Since i am using 64 bit Linux installed in 64 bit CPU it is 64 bit, if it were 23 bit computer it would be 32 bit. We can store Address of Variable in special variable called as Pointers.
To store address of integer variable we use integer Pointer.
To store address of float variable we use float pointer.
To store address of character variable we use character pointer.
Syntax
data_type *ptr_name;
ptr_name is pointer to data_type. * Telling here to compiler that it is a pointer.
Below C program demonstrates how pointers can be declared
#include <stdio.h> int main() { int i = 10; float f = 2.3; char c = 'a'; int *pi; /* pi is Interger Pointer */ float *pf; /* pf is Float Pointer */ char *pc; /* pi is Charecter Pointer */ pi = &i; pf = &f; pc = &c; printf("Address of i - %p; Pointer pi - %p\n", &i, pi); printf("Address of f - %p; Pointer pf - %p\n", &f, pf); printf("Address of c - %p; Pointer pc - %p\n", &c, pc); printf("Address of pi - %p, sizeof pi - %ld\n", &pi, sizeof(pi)); printf("Address of pf - %p, sizeof pf - %ld\n", &pf, sizeof(pf)); printf("Address of pc - %p, sizeof pc - %ld\n", &pc, sizeof(pc)); return 0; }
Output of Program is
Address of i - 0x7fffb1ddcbe4; Pointer pi - 0x7fffb1ddcbe4 Address of f - 0x7fffb1ddcbe8; Pointer pf - 0x7fffb1ddcbe8 Address of c - 0x7fffb1ddcbef; Pointer pc - 0x7fffb1ddcbef Address of pi - 0x7fffb1ddcbc8, sizeof pi - 8 Address of pf - 0x7fffb1ddcbd0, sizeof pf - 8 Address of pc - 0x7fffb1ddcbd8, sizeof pc - 8
Pictorially it can be represented as follows.
Note way data stored and sizeof int, float and char is different. But the integer pointer, float pointer and character pointer all occupy same space.
Now we know how the pointer takes its value from memory, How the address of variable is stored in Pointer. But you might ask what is just use of storing the address ? Yeah that is true only with storing there is no use. Let me introduce you to de-reference operator *. (NOTE this is different from the one that is used in declaration). Note that de-reference operator is unary operator and can be used only with pointer variable.
Syntax
*ptr_variable = 10; /* To modify the value pointed by ptr_variable */
x = *ptr_variable; /* To Access value pointed by ptr_variable */
Let me write another program that uses de-reference operator to modify original
#include <stdio.h> int main() { int i = 10; int *pa = &a; printf("Value of *pa is %d\n", *pa); printf("Value of i is %d\n", i); *pa = 20; printf("Value of *pa is %d\n", *pa); printf("Value of i is %d\n", i); return 0; }
Output of above program is
Value of *pa is 10 Value of a is 10 Value of *pa is 20 Value of a is 20
Hence we have understood that, using pointers we can access/modify the variable it is pointing to. In coming few articles we will see how pointers can be used in very intricate way.
Links
Next Article - C Programming #42: Pointer - arithmeticPrevious Article - C Programming #40: Array - two dimension
All Article - C Programming
No comments :
Post a Comment