0

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.

Community
  • 1
  • 1
PSaR
  • 23
  • 1
  • 4
  • 2
    What happened when you tried it? – Martin James Apr 16 '16 at 00:30
  • Try `char *buf = malloc(2); buf[0]=7; buf[1]=8; char *p = buf; *p++ = 3; printf("%d %d\n", buf[0], buf[1]);` – user3386109 Apr 16 '16 at 00:32
  • 1
    ^^^^ see, it's quicker to try it than ask an SO question. – Martin James Apr 16 '16 at 00:33
  • The words "the pointer" are confused and vague. There are several pointers here. One is the variable `p`. Another is the operand of the `*` operator. The value of the variable `p` changes in the course of the program, whereas the `*` operator takes one single, specific operand value (namely the result of the expression `p++`). – Kerrek SB Apr 16 '16 at 00:39
  • `p` may or may not be incremented before the assignment of `argv[1][0]` to the character to which `p` points before the increment operation is applied. The implementation is free to reorder things as it wishes as long as it conforms to the standard's specified behavior. Whether the effects are applied before or after the assignment is irrelevant because the increment operation is applied after the current value of `p` results, which is then dereferenced, leading to equally valid code such as `*p = argv[1][0]; ++p;` or `char *oldp = p; ++p; *oldp = argv[1][0];`. Both achieve the same result. –  Apr 16 '16 at 01:17
  • @MartinJames That's not how you resolve this kind of question. It could be undefined behaviour in which case it doesn't matter what OP observes. – fuz Apr 16 '16 at 01:36
  • Wrong duplicate, how can a C question be a duplicate of a C++ question; C++ could and can have an operator overload on `++` and do whatever. – Antti Haapala -- Слава Україні Apr 16 '16 at 10:16

0 Answers0