Reputation: 189
My program is given below:
#include<stdio.h>
main()
{
int x=30,*y,*z;
y=&x;
z=y;
*y++=*z++; //what does this mean?
x++;
printf("%d %d",y,z);
return 0;
}
What is the meaning of this statement: *y++=*z++
?
Upvotes: 1
Views: 802
Reputation: 59677
y
and z
are pointers, and the expressions y++
and z++
are using the postfix operators, so both increments will happen after the assignment.
This statement does 3 things:
y
the same value pointed to by z
: *y = *z
.y
pointer. Now y
points to the next int
in memory.z
pointer. Now z
points to the next int
in memory. The last two would be bad if y
and z
were used after the statement: y
and z
now point to uninitialized memory that doesn't belong to the code.
Upvotes: 3
Reputation: 1529
*y++=*z++; //wat does this mean?
The above results in undefined behaviour (multiple modifications before the next sequence point).
Upvotes: 0
Reputation: 3793
It will simply assign the value of the object pointed to by y to that of the object pointed to by z, and finally it increments both pointers.
Upvotes: 0
Reputation: 13504
*y++=*z++;
- This is just assigning the values stored in pointer z
to the *y
. After this its incrementing both y
and z
pointer. After this statement dereferencing of both z
and y
may leads to crash(an undefined behaviour).
These kind of statement is used in string copy implementation
void my_strcpy(char *dest, char* src)
{
while((*dest++ = *src++));
}
Upvotes: 1
Reputation: 404
Variables y
and z
are pointers to integers, and in this case, point to the address of variable x.
The following lines set the value of y
to the address of x
(i.e. &x
gives you the address of `x).
y=&x;
z=y
The next line, *y++=*z++;
doesn't exactly make sense and may or may not compile depending on the compiler used. In the case of GCC 4.3.2, it gives the following error.
foo.c:7: error: invalid operands to binary * (have ‘int *’ and ‘int *’)
It looks like you are trying to do pointer arithmetic, but got the operator precedence a little mixed up. Are you trying to increment the value pointed to by z
after assigning the pre-increment value to a location?
Upvotes: 0
Reputation: 21351
This simply assigns the value of the object pointed to by y
to that of the object pointed to by z
, then increments both pointers.
Upvotes: 0
Reputation: 182794
It's equivalent to:
*y = *z;
y++;
z++;
I cant understand the output of this program
You're printing pointers, there's not much to understand.
%p
instead of %d
when printing pointersUpvotes: 3