extending little more concept about loop
sometimes we dont want to allot new memory so we can use this approach
now pointer stuff
pointer to : *p
reference of: &p
suppose
int i=1;
so,
int *p=&i;
means p points to the memory address where i is stored....
int p=&i;
means p has the memory address of "i" so whatever changes u will do in p will directly effect i..
assume it like its same as two remote of a same tv.
pointer even does the same task but in that it points to the index of the number it is addressed... like int *p=&a[5]; even when we write int *p=a(is an integer array);it points to the first element... its same as int *p=&a[0];
int *p=nullptr
nullptr is to make a pointer null ...
it is mostly used when the non linear data structure is used like queue, linked list,vector,maps etc
to mark the end of node...
summing up in a program
at first i initialized the memory for a and ax in heap and axx in stack memory
after that assigned value to the variables
after that i made use of the pointers to point to each other a->ax->axx
and call the fun function which shows a clear difference of reference and pointer
take a pen and paper and try to break and check if u got the concept if not comment me... its very important u get to pointers concept thoroughtly and play with it and try new variations than only u can master its use...
sometimes we dont want to allot new memory so we can use this approach
1 2 3 4 5 6 7 | #include<bits/stdc++.h> int main() { for(auto i:{1,2,3,4,5,6}) printf("%d\n",i); return 0; } |
now pointer stuff
pointer to : *p
reference of: &p
suppose
int i=1;
so,
int *p=&i;
means p points to the memory address where i is stored....
int p=&i;
means p has the memory address of "i" so whatever changes u will do in p will directly effect i..
assume it like its same as two remote of a same tv.
pointer even does the same task but in that it points to the index of the number it is addressed... like int *p=&a[5]; even when we write int *p=a(is an integer array);it points to the first element... its same as int *p=&a[0];
int *p=nullptr
nullptr is to make a pointer null ...
it is mostly used when the non linear data structure is used like queue, linked list,vector,maps etc
to mark the end of node...
summing up in a program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include<bits/stdc++.h> typedef struct $ { int value; struct $ *pointer; }fun; void play(fun a,fun *ax,fun &axx) { int i=a.value; int j=ax->value; int k=axx.value; printf("%d %d %d\n",i,j,k); printf("------------\n"); printf("%d\n",a.pointer->value); printf("%d\n",a.pointer->pointer->value ); printf("------------\n"); printf("%d\n",ax->pointer->value ); } int main() { fun *a=new fun[1]; fun *ax=new fun[1]; fun axx; a[0].value=12; ax[0].value=13; axx.value=45; a[0].pointer=&ax[0]; ax[0].pointer=&axx; axx.pointer=nullptr; play(*a,ax,axx); return 0; } |
at first i initialized the memory for a and ax in heap and axx in stack memory
after that assigned value to the variables
after that i made use of the pointers to point to each other a->ax->axx
and call the fun function which shows a clear difference of reference and pointer
take a pen and paper and try to break and check if u got the concept if not comment me... its very important u get to pointers concept thoroughtly and play with it and try new variations than only u can master its use...