- Pointer is a variable which holds address of another variable.
- We know that every variable is stored into memory locations. Each memory location have it’s address
- The pointer operator * is called as value at address operator which gives value stored at a particular address.
- The another operator use in pointer is & address of operator which gives address of a variable.
- The ‘value at address’ operator is also called indirection operator.
- A dereference operator (*) operator return the value of a variable pointed by a pointer.
- Pointer variable must be declare before using it.
- A type of a variable and a type of pointer should be same.
Pointers Notation
Consider the declaration,
1 |
int a = 10 ; |
This declaration tells the C compiler to:
- Reserve space in memory to hold the integer value.
- Associate the name a with this memory location.
- Store the value 10 at this location.
We may represent this declaration in memory as following-
Here from above figure, we can see that value 10 is stored in memory location 12764. But it is not necessary every time value 10 will store at the same memory location. And this address is a number.
Hence we can print address of variable as shown in following program
1 2 3 4 5 6 |
main( ) { int a = 10 ; printf ( "Address of a = %u\n", &a ) ; printf ( "Value of a = %d", a ) ; } |
Output:
1 2 |
Address of a = 12764 Value of a = 10 |
Consider the following example, which shows how pointers work. From below figure, the type of a variable a (int*) is same as a type of pointer b (int *). Here b is a pointer which holds address of a variable a.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> int main() { int a=10; int *b = &a; printf("value at a is %d\n", a); printf("address of a is %u\n", &a); printf("value at b is %d\n", b); printf("address of b is %u\n", &b); printf("value at *b is %d\n", *b); printf("size of variable a is %d bytes\n", sizeof(a)); printf("address size of variable a is %d bytes\n", sizeof(&a)); printf("size of variable b is %d bytes\n", sizeof(b)); printf("size of variable *b is %d bytes\n", sizeof(*b)); } |
Output:
1 2 3 4 5 6 7 8 9 10 |
value at a is 10 address of a is 6556 value at b is 6556 address of b is 8034 value at *b is 10 size of variable a is 4 bytes address size of variable a is 8 bytes size of variable b is 8 bytes size of variable *b is 4 bytes |
Now see an example of character data type:
1 2 3 4 5 6 7 8 9 10 |
#include<stdio.h> int main() { char a='x'; int *b = &a; printf("size of variable a is %d bytes\n", sizeof(a)); printf("address size of variable a is %d bytes\n", sizeof(&a)); printf("size of variable b is %d bytes\n", sizeof(b)); printf("size of variable *b is %d bytes\n", sizeof(*b)); } |
Output:
1 2 3 4 |
size of variable a is 1 bytes address size of variable a is 8 bytes size of variable b is 8 bytes size of variable *b is 4 bytes |
Points to be noted:-
-
12int *ptr;int *ch;
Here ptr and ch are declared as pointer variables that hold address.
Addresses are always whole numbers. Therefore pointers always contain whole numbers. -
1float *ptr;
This declaration does not mean that ptr contain a floating-point value.
It means, ptr contain address of a floating-point value. - We know that pointer is a variable that holds an address of another variable.But this another variable can be a pointer. So we can say that pointer can hold an address of another pointer.
Likewise, there could exist a pointer to a pointer to a pointer to a pointer to a pointer. - There is no limit on how far can we go on extending this definition.