can anyone explain in which order the following C statement is processed?:
char *p = malloc(1);
*p++ = argv[1][0];
The processing order for something like T min_value = *begin++;
from here is clear but what is the chronologic order if the pointer increment and dereference is on the left side of my assignment? I know ++
has a higher operator binding priority than *
(dereferencing). Does it mean that the pointer is incremented before the assignment of argv[1][0]
?
Patrick
I found the answer, my test code:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
char *start;
char *buf = malloc(2);
start = buf;
printf("Start of reserved memory: %p\n", buf);
buf[0] = 3;
printf("First char 3 at: %p\n", &buf[0]);
buf[1] = 4;
printf("Second char 4 at: %p\n", &buf[1]);
printf("'buf' address: %p\nDo assignment '*buf++ = 7'\n", buf);
*buf++ = 7;
printf("Content of %p: %d\n", &start[0], start[0]);
printf("Content of %p: %d\n", &start[1], start[1]);
return 0;
}
And the result was:
Start of reserved memory: 0x1672010
First char 3 at: 0x1672010
Second char 4 at: 0x1672011
'buf' address: 0x1672010
Do assignment '*buf++ = 7'
Content of 0x1672010: 7
Content of 0x1672011: 4
So I assume *buf++ = 7
is processed in this way:
*buf = 7;
buf++;
and NOT
buf++;
*buf = 7;
as one could assume because of operator binding priority. I compiled with gcc 5.3.