Ghost Iscuming
Ghost Iscuming

Reputation: 189

I can't understand the output of this program

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

Answers (8)

pb2q
pb2q

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:

  1. Assigns to the value pointed to by y the same value pointed to by z: *y = *z.
  2. Increments the y pointer. Now y points to the next int in memory.
  3. Increments the 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

Lelanthran
Lelanthran

Reputation: 1529

*y++=*z++;  //wat  does this mean?

The above results in undefined behaviour (multiple modifications before the next sequence point).

Upvotes: 0

Prashant Singh
Prashant Singh

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

rashok
rashok

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

thelazyenginerd
thelazyenginerd

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

mathematician1975
mathematician1975

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

Michael Graczyk
Michael Graczyk

Reputation: 4965

That is the same as:

*y = *z;
++z;
++y;

Upvotes: 0

cnicutar
cnicutar

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.


  • You should be using %p instead of %d when printing pointers
  • You're moving beyond the known universe when you increment those pointers

Upvotes: 3

Related Questions