Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how pointer assignment and increment is working in below example

Tags:

c

pointers

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?

like image 369
Jagrut Trivedi Avatar asked Dec 02 '16 10:12

Jagrut Trivedi


1 Answers

*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.

like image 161
cokceken Avatar answered Oct 06 '22 01:10

cokceken