Click here to Skip to main content
14,577,432 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi, everybody;
I found the code above. But I don't understand the out where x=3 y=4. Please help me.
Why didn't work the operations ++ when case 0 twice? And why y=4, becase I think it should be 5.

What I have tried:

#include<stdio.h>

int main(){
int x=2,y=3,*ptr,i=0;
int a[9]={1,2,3,4,5,6,7,8,9};

ptr=a;
//     printf("X= %d\n",x);
//     printf("*ptr= %d\n\n",*ptr);
while(i<5)
{
    //printf("i=%d\n",i);
    switch(i%3)
    {
    case 0:
        x=*(ptr)++;
//        printf("X= %d\n",x);
//        printf("*ptr= %d\n\n",*ptr);
    break;
    case 1:
       y=++*(ptr);
//       printf("Y= %d\n",y);
//       printf("*ptr= %d\n\n",*ptr);
    break;
    default:
        break;
    }
    i++;
    printf("x = %d\ty = %d\n",x,y);
}

return 0;
}
Posted
Updated 30-Jun-20 4:39am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You are playing with two things here, neither of which I am sure you understand well.

Pre- and post-increment operations can be difficult to understand, because they are "side effect" operators: they change the value of variables without an obvious assignment.
In your case,
x = *(ptr)++;
is the equivelant of saying this:
x = *ptr;
ptr = ptr + 1;
And
y = ++*(ptr);
is the equivelant of this:
*ptr = *ptr + 1;
y = *ptr;

The second version changes the value in the array a instead of altering the pointer itself.

Run the code through the debugger and look closely at what is happening to the values of ptr and the array and you'll see what I mean.

It might be worth a quick look here: Why does x = ++x + x++ give me the wrong answer?[^] before you start diving into complicated pre- and post- increment statements!
   
v2
Comments
goksurahsan 30-Jun-20 10:37am
   
I understand pre and post-increment before. But I actually ask for this action why when i=3 post-increment didn't work for ptr. I used debugger but debeugger showed me post-increment passed
OriginalGriff 30-Jun-20 11:02am
   
It worked fine. It did exactly what you told it to.
goksurahsan 30-Jun-20 10:42am
   
For example I think these codes are same the before codes:

case 0:
x=*(ptr);
*ptr=*ptr+1;
//x=*ptr++;
// printf("X= %d\n",x);
// printf("*ptr= %d\n\n",*ptr);
break;
case 1:
*(ptr)=*(ptr)+1;
y=*(ptr);
//y=++(*ptr);
Richard MacCutchan 30-Jun-20 10:43am
   
No, see my comment below.
Richard MacCutchan 30-Jun-20 10:43am
   
Not quite. The second case is equivalent to:
y = *ptr;
y = y + 1;

ptr does not get incremented in this case.

Took me half an hour to figure it out.
goksurahsan 30-Jun-20 10:48am
   
But ++ near ptr. I should it change ptr. Is this true?
Richard MacCutchan 30-Jun-20 10:50am
   
No, the * is closest to ptr so it does the indirection operation first. i.e. it takes the value (*ptr) and increments that, it does not increment the pointer.
OriginalGriff 30-Jun-20 11:18am
   
Are you sure?
It's a pre-increment, and if I test it in VS:
int main()
{
int arr[5] = { 10, 21, 32, 43, 54 };
int* p = arr;
int x = ++ * p;
return 0;
}I get arr[0] == 11, x == 11, p == arr[0] as I'd expect.
Richard MacCutchan 30-Jun-20 11:28am
   
You are right, I looked at this three times and still managed to misread my own answers.
OriginalGriff 30-Jun-20 11:36am
   
They do my head in from time to time as well ... :laugh:
Richard MacCutchan 30-Jun-20 11:43am
   
And I used to write that sort of thing all the time back in my pure C days. Mind you, I doubt that I ever understood it properly.
OriginalGriff 30-Jun-20 12:03pm
   
Ditto.
And the worst bit is yet to come ... if the OP hasn't followed the article link I gave him, he's going to get really confused with his next try - they are implementation dependent side-effect operators as well ... :EvilLaughSmiley:
goksurahsan 30-Jun-20 10:44am
   
And while these codes work as I thought, why does the increment process don't work in the previous code when i = 3 in * (ptr) ++ section. Unfortunately this is the part I do not understand.
OriginalGriff 30-Jun-20 11:39am
   
It does. Read what we said above. ++*p changes the value that p points to, not the value of p.
But because your numbers in the array also increment by one, it's difficult to tell unless you look closely.
The debugger will show you that if you look at ptr and the array content.
Richard MacCutchan 30-Jun-20 11:41am
   
Because after the first case 0 a = { 1,2,3,4 ...} and ptr points at 2
We than go to case 1: ptr points at 2, which gets changed to 3. y gets 3.
so a now equals {1,3,3,4 ...};
Next time we get to case 0, x gets 3 (the first 3) and ptr gets changed to point to the second 3.
Next case 1, the second 3 gets changed to 4, so a = { 1,3,4,4, ...} and y gets the first 4.

Change the numbers in the array to be non-contiguous (e.g. { 3,7,15,22 } and it becomes clearer.

Note: Apologies for my previous 'solution' it was incorrect.
goksurahsan 30-Jun-20 12:23pm
   
Really thank you so much. I couldn't understand because I was new to C. I get it now. While the address indicated by the pointer changes in x = * ptr ++ section, the value of y changes according to the address indicated by ptr in y = ++ * ptr section.
OriginalGriff 30-Jun-20 12:29pm
   
Close - the value of what ptr is pointing to gets changed, and that affects what gets written into y.
goksurahsan 30-Jun-20 12:45pm
   
Yes, I understand now. Thank you again and again. I learned new thing today :)
OriginalGriff 30-Jun-20 14:36pm
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100