Difference between int *p; and *p;
int *p; | *p; |
p is an integer type pointer variable which can store address of another variable of type integer. | *p means value at p. i.e. value at address stored at p. |
Example:
#include <stdio.h> #include<conio.h> int main() { int a=10; int *p; //clrscr(); p=&a; printf("%d\n",a); //value of a printf("%d\n",&a); //address of a printf("%u\n",&a); //address of a printf("%u\n",p); //address of a which is stored in p printf("%d\n",*p); //value at p i.e. value at address stored at p printf("%d\n",*(&a)); //value at address of a getch(); return(0); }
/* Output */ 10 6487572 6487572 6487572 10 10
* Address is always a positive integer
* A pointer variable always stores an address.
* Size of an integer variable is always 2 bytes
* So, size of a pointer variable is always 2 bytes
/* to display size of datatype and pointer variable */ #include<stdio.h> #include<conio.h> void main() { int a=10,*ip; char c='A',*cp; float e=10.45f,*fp; double d=34.67,*dp; //clrscr(); ip=&a; cp=&c; fp=&e; dp=&d; printf("1. int %d %d\n",sizeof(a),sizeof(ip)); printf("2. char %d %d\n",sizeof(c),sizeof(cp)); printf("3. float %d %d\n",sizeof(e),sizeof(fp)); printf("4. double %d %d\n",sizeof(d),sizeof(dp)); printf("5. int %d\n",sizeof(*ip)); printf("6. char %d\n",sizeof(*cp)); printf("7. float %d\n",sizeof(*fp)); printf("8. double %d\n",sizeof(*dp)); getch(); }
/* Output */ Output When executed in Turboc3 1. int 2 2 2. char 1 2 3. float 4 2 4. double 8 2 5. int 2 6. char 1 7. float 4 8. double 8 int 2 char 1 float 4 double 8 Output When executed in CodeBlocks / DevC++ 1. int 4 8 2. char 1 8 3. float 4 8 4. double 8 8 5. int 4 6. char 1 7. float 4 8. double 8
Pointer of pointer variables
A pointer of a pointer variable is a pointer variable that can store the address of another pointer variable of its own type.
Integer type pointer of pointer variable :
Integer type pointer of a pointer variable is a pointer variable that can store the address of another pointer variable of type integer.
float type pointer of pointer variable :
Float type pointer of a pointer variable is a pointer variable that can store the address of another pointer variable of type float.
int a=10;
int *p;
int **q;
p=&a;
q=&p;
a | “a” is a normal int variable which can store an integer value |
p | “p” is an integer type pointer variable so it can store address of another variable of type integer. Thus we can write P=&a; |
q |
int **q; |