In this post we are going to discuss:
- Variables and their Memory Addresses
- Pointers
- Storing Address of Pointer
- Pointer Arithmetic
Variables:
Variables are the names given to memory locations. Every variable is stored at a unique memory location, it is very difficult to remember each location's address, so each location is assigned a name which can be easily remembered. Suppose we declare an integer variable as follows:
int a=10; //a is the name of memory location where constant 10 is stored
Let us say address of variable a is 2004, so we can represent it as:
Here we can say that
Value of a is 10 or Value at Address 2004 is 10
Address of a is 2004
Pointers : These are special variables which are used to store address of another variable. In C language we can declare a pointer using following syntax:
data-type *ptr;
Here data-type is any built-in or user defined data type.
Asterisk (*) symbol is used to tell compiler that we are declaring a pointer.
ptr is the name of pointer variable.
Note: Asterisk (*) symbol has multiple uses in C language. It is used as symbol for multiplication, it is used to declare pointers and it is also used to find value at given address,
Two Special Operators:
Asterisk (*) : Value at Address Operator
Ampersand (&) : Address of Operator
Asterisk (*) is used to find value at an address. Address is usually stored in a pointer variable.
Ampersand (&) is used to refer to address of a variable or a function.
Pointer Example:
Output :
Value of a is 10
Address of a is 2004
Value of a is 10
Address of a is 2004
In above code we have declared an int variable a with initial value 10, ptr is a pointer variable of type int.
Note: data-type of pointer variable and of variable who's address is to be stored in pointer must be same.
In our example ptr is used to store address of a i.e 2004. (data-type of a and ptr is same i.e. int)
Suppose address of a is 2004 and of ptr is 3002.
In first printf we are printing value of a
In second printf will display address of a i.e. 2004,
In 3rd printf we are printing value at address stored in ptr i.e. value at 2004 which is also 10
In last printf it will simply display the address stored in ptr i.e. 2004.
printf("%u",&ptr);
It will display address of ptr which is 3002.
printf("%d",*(*(&ptr)));
Let us evaluate above printf: &ptr is 3002 so it becomes *(*3002), now value at 3002 is 2004 again
value at 2004 is 10. So it will print value of a which is 10.
How to store address of a pointer?
In case we want to store address of a pointer we need a double pointer. To declare a double pointer we need two asterisk (**) followed by pointer name. If we need to store address of double pointer we need triple pointer and so on.
Eg:
int a=10;
int *ptr1,**ptr2;
ptr1=&a;
ptr2=&ptr1;
Let us say address of a is 2004, address of ptr1 is 3002 and address of ptr2 is 4200.
If we refer to a it is 10.
If we refer to ptr1 it is 2004.
If we refer to ptr2 it is 3002.
Address of a i.e. &a is 2004.
Address of ptr1 i.e. &ptr1 is 3002.
Address of ptr2 i.e. &ptr2 is 4200.
Value at ptr1 i.e. *ptr1 is 10.
Value at ptr2 i.e. *ptr2 is 2004.
**ptr2 is **3002 which is *2004 which is 10.
Pointer Arithmetic
Like normal variables pointer variables may also be used with some operators in C. Here are some important points for pointer arithmetic:
Integer can be added or subtracted in a pointer variable i.e. we can add or subtract integers in pointer variables using + (plus) and - (minus) operators.
We cannot apply *, /, % operators to pointer variable i.e. we cannot multiply/divide/modulus integers with pointer variables.
Eg:
int a=10;
int *ptr;
printf("%u",ptr+2);//valid two location after 2004
It will print ptr + 2 * sizeof(int) i.e. ptr+4 (if int takes 2 bytes) (2008)
printf("%u",ptr-3);//valid 3 locations before 2004
It will print ptr - 3 * sizeof(int) i.e. ptr-6 (if int takes 2 bytes) (1998)
printf("%u",ptr*2);//It will generate error
printf("%u",ptr/2;)//It will generate error
printf("%u",ptr%4);//It will generate error
We cannot apply *, /, %, + to two pointer variables, i.e. we cannot multiply/divide/modulus/add two pointer variables. But two pointer variables can be subtracted.
Eg:
int *ptr1,*ptr2;
printf("%u",ptr1-ptr2);//Valid
printf("%u",ptr1+ptr2);//It will generate error
printf("%u",ptr1*ptr2);//It will generate error
printf("%u",ptr1/ptr2);//It will generate error
printf("%u",ptr1%ptr2);//It will generate error
Note: Pointer variables are used to store addresses of memory which can be related to addresses of some houses. Like we usually say that my house is located after or before 3 (or any other number) houses of some person. It means we usually add or subtract numbers from real house addresses. We also say that there is a difference of 2 (or any other number) houses in my house and his/her house.
Follow us on: