When using a restrict
Pointer in C, is it OK to change the variable using its initial Identifier? For example:
int foo = 0;
int * restrict fooPtr = &foo;
++(*fooPtr); // Part 1: foo is 1 (OK)
++foo; // Part 2: foo is 2 (Is this OK?)
int * fooPtr2 = &foo;
++(*fooPtr2); // Part 3: foo is 3 (BAD: You shouldn't access via a second pointer)
...I changed the value of foo through foo after the restrict
fooPtr was created.
Part 1 looks OK to me. I'm confused about Part 2. And from what I understand about restrict
, Part 3 is bad (compiler allows it, but its behavior is undefined, and it's up to the programmer not to do that).