Jagrut Trivedi
Jagrut Trivedi

Reputation: 1319

how pointer assignment and increment is working in below example

I am learning the pointers in C. I am little confused about how below program is working

int main()
{
    int x=30, *y, *z;
    y=&x; 
    z=y;
    *y++=*z++;
    x++;
    printf("x=%d, y=%p, z=%p\n", x, y, z);
    return 0;
}

output is

x=31, y=0x7ffd6c3e1e70, z=0x7ffd6c3e1e70

y and z are pointing to the next integer address of variable x. I am not able to understand how this line is working

*y++=*z++;

can someone please explain me how this one line is understood by C?

Upvotes: 5

Views: 535

Answers (1)

cokceken
cokceken

Reputation: 2076

*y++=*z++; actually means

*y = *z;
y += 1*(sizeof(int)); //because int pointers are incremented by 4bytes each time
z += 1*(sizeof(int)); //because int pointers are incremented by 4bytes each time

So pointed value does not affected, pointers are incremented by one.

Upvotes: 5

Related Questions