| 7 | #define endl '\n' |
| 8 | |
| 9 | int main(){ |
| 10 | /* |
| 11 | cin.tie(0)->sync_with_stdio(0); |
| 12 | int n=5; |
| 13 | cout<<"the value of n is: "<<n<<endl; |
| 14 | int *ptr = &n; //created a pointer and give the address of n |
| 15 | //always initialize the pointer data type |
| 16 | cout<<"the address of 5 is: "<<&n<<endl;//& is used to show address of any data(it has hexa decimal value |
| 17 | cout<<&(*ptr); |
| 18 | int *p=0; |
| 19 | p=&n;// we can also initialize it like this | while initializing like this never use '*' again |
| 20 | cout<<"before increasing "<<*p<<endl; |
| 21 | (*p)++; |
| 22 | cout<<"after increasing "<<*p<<endl; |
| 23 | int x; |
| 24 | x=3; |
| 25 | int a; |
| 26 | a=x; |
| 27 | a++; |
| 28 | cout<<"using direct int variable : "<<x; |
| 29 | |
| 30 | int *z=&x; |
| 31 | (*z)++; |
| 32 | cout<<endl; |
| 33 | cout<<"using pointer variable :"<<x<<endl; |
| 34 | int *pt=z; |
| 35 | cout<<pt<<" - "<<z<<endl; |
| 36 | cout<<*pt<<" - "<<*z<<endl; |
| 37 | z++; |
| 38 | cout<<z<<endl;//if variable is int then addresss will increase with 4 bitss |
| 39 | double ff=9; |
| 40 | double *pff=&ff; |
| 41 | cout<<"before increasing "<<pff<<endl; |
| 42 | pff=pff+1;// the address will increase by 8 bits |
| 43 | cout<<"after increasing "<<pff<<endl; |
| 44 | // the size of a pointer is always 8 bits since it only store the address the type of variable will not affect the size of variable |
| 45 | cout<<"size of int "<<sizeof(x)<<" size of its pointer "<<sizeof(z)<<endl; |
| 46 | cout<<"size of double "<<sizeof(ff)<<" size of its pointer "<<sizeof(pff)<<endl; |
| 47 | |
| 48 | |
| 49 | */ |
| 50 | //TYPES OF POINTERS |
| 51 | //1. NULL POINTER |
| 52 | int *p;//return garbage value of address |
| 53 | int *q= NULL; |
| 54 | int *r= 0; |
| 55 | |
| 56 | //2.DOUBLE POINTER |
| 57 | int a=9; |
| 58 | int *b=&a; |
| 59 | int **c=&b; |
| 60 | // double pointer store the address of the pointer i.e b |
| 61 | |
| 62 | //3. VOID POINTER |
| 63 | void *d; |
| 64 | //it cannot be dereference. however it can be type cast and used |
| 65 | /* |
| 66 | void *ptr; |
nothing calls this directly
no outgoing calls
no test coverage detected