A pointer is a secondary data type that is used to declare a variable which can hold the address of another variable. Such variables, which are declared through the Pointer data type, known as pointer variables.
datatype* pointer_variable_name;
Example
1 2 3 4 5 6 7 8 |
/*declare a pointer variable to hold the address of any integer type variable*/ int *i_ptr; /*declare a pointer variable to hold the address of any Character type variable*/ char *c_ptr; /*declare a pointer variable to hold the address of any float type variable*/ float *f_ptr; |
Let us understand the address assignment through this diagram –
Remember: Pointer can hold the address of same data type variable as itself.
I above diagram, we have seen the address assignment of an integer variable into the pointer variable of the same datatype.Let us see how it works in memory –
1-Reserve a memory location 1024 by the variable i_var and initialize with value 5.
2-Reserve a memory location 1044 by the pointer variable i_ptr.
3-Assign the address of i_var(1024) to the pointer i_ptr
Indirection operator(*) is used for fetching the value from the address held by the pointer.
See how the value at the assigned address held by pointer can fetch through the use of Indirection operator.
In Simple terms, you can say that one * cuts one & symbol.
1 2 3 4 5 6 7 8 9 |
unsigned char ch_var=65; unsigned int *i_ptr; unsigned char *ch_ptr; i_ptr=&ch_var; /*wrong because data type is different*/ ch_ptr=&ch_var; /*right because both have similar data type*/ |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> int main() { unsigned int i_var=65; unsigned int *i_ptr; i_ptr=&i_var; printf("Address of i_var is = %p\n",i_ptr); printf("Value at address %p is=%u\n",i_ptr,*i_var); return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> int main() { unsigned char ch_var=65; unsigned int *i_ptr; i_ptr=(unsigned int *)&ch_var; /*right because character variable is typecasted into unsigned int*/ printf("Address of ch_var is = %p\n",i_ptr); printf("Value at address %p is=%u\n",i_ptr,*((unsigned char*)i_ptr)); return 0; } |
Output :
You can see in above example ch_var has unsigned char dataType while i_ptr has unsigned int
* symbol has a different meaning as per its uses.keep in mind that –
unsigned int i_ptr ———–> Here * indicates i_ptr is pointer variable.
*i_ptr ———————> Here * indicates the value at the address held by pointer i_ptr.
Remember:
- charpointer jumps only 1 byte in single increment whatever the processor size is.
- intpointer jumps only 4 byte in single increment if processor is 32 bit.
- float pointer jumps only 4 byte in single increment if processor is 32 bit.
- double pointer jumps only 8 byte in single increment if processor is 32 bit.
A void pointer can hold the address of any dataType Variable because its data type is void.It is also known as a generic pointer because it can hold the address of any type variable.
void *vptr; /*vptr is a void pointer*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> int main() { unsigned char ch_var=65; void *vptr; vptr=(unsigned char*)&ch_var; /*right because character variable is typecasted into unsigned int*/ printf("Address of ch_var is = %p",vptr); printf("\nValue at address %p is=%u",vptr,*((unsigned char*)vptr)); return 0; } |
Output:
A null pointer is one which is not pointing to anything, i.e. it is assigned a null value. It is a good practice to initialize a NULL value to the pointer if it is not initialized.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> int main() { unsigned int i_var=65; unsigned int *i_ptr=NULL; i_ptr=&i_var; printf("Address of i_var is = %p\n",i_ptr); printf("Value at address %p is=%u\n",i_ptr,*i_ptr); return 0; } |