+ 1
Anyone explain why pointer behave like this in this code ?
For first for loop only 2 is printing and for second instead of 23465 it's printing something else can you explain how this all is happening https://code.sololearn.com/c6IFZIyUWCM3/?ref=app
5 odpowiedzi
+ 3
c contains array values..
p contains address of c array first location
q contains address of c array first location then
++q is q=q+1, will not affect to c pointing. It not means c+1.
c[]= { 1, 2, 3 , 4, 5,..}
^ ^
| |
*p *p++,*p++,...
*q *q++,*q++,.... All 3 individual oparating.
Simply p point to c, but c not pointing p or q in anyway..
+ 2
The second loop is fine because you are printing the elements using a pointer by incrementing the pointer at every iteration.
But in the first loop you are incrementing the pointer but printing *c which always prints the 1st value only because as you know that the array name points to the address of the first element of the array. The q++ will have no effect.
0
Why ++q has no effect? In second loop I'm doing same thing and printing *p so why not *c is working like that?
0
First we declare two pointers q and p.
Each was assigned c means address of array.
In the first loop we are printing the value *c , c is the address of first element of array and *c means value at first address of array which is 2. Because we are increasing q there will be not effect on value of c. Hence printing 2 five time.
In the second loop we are printing *p means value at address contained in p and also increasing the same that is ++p, which will increase value to next array's element address