-1

the output of the next code is:

40
30
20
10

 unsigned char numbers[] = {10,20,30,40};
 unsigned char* ptr = numbers;
 printf("%d\n%d\n%d\n%d",*ptr, *(ptr++), *(ptr++), *(ptr++) );

I think ++ operators are done first, so I understand why the first value printed is 40, but how it comes to print 30, 20 and 10 after it ? It's going backwards this way!

A.Fahmy
  • 7
  • 1

1 Answers1

0

What you invoke is undefined behavior.

In C there is no sequence point between the evaluation of function parameters.

You have to write:

printf("%d\n%d\n%d\n%d",*ptr, *(ptr), *(ptr+1), *(ptr+2) );
ptr += 3;
Osiris
  • 2,783
  • 9
  • 17