C11 6.7.3.1:
1 Let D be a declaration of an ordinary identifier that provides a
means of designating an
object P as a restrict-qualified pointer to type T.2 If D appears inside a block and does not have storage class extern, let B denote the
block. If D appears in the list of parameter declarations of a function definition, let B
denote the associated block. Otherwise, let B denote the block of main (or the block of
whatever function is called at program startup in a freestanding environment).3 In what follows, a pointer expression E is said to be based on object P if (at some
sequence point in the execution of B prior to the evaluation of E) modifying P to point to
a copy of the array object into which it formerly pointed would change the value of E.
137)
Note that ‘‘based’’ is defined only for expressions with pointer types.4 During each execution of B, let L be any lvalue that has &L based on P. If L is used to
access the value of the object X that it designates, and X is also modified (by any means),
then the following requirements apply: T shall not be const-qualified. Every other lvalue
used to access the value of X shall also have its address based on P.Every access that
modifies X shall be considered also to modify P, for the purposes of this subclause…
Based on this sentence, I conceived a piece of code:
int main()
{
int A = 5;
int* restrict P;
P = &A;
*P = 10;
int *N = P;
*N = 20;
}
My doubt comes from the sentence “Every access that modifies X shall be considered also to modify P, for the purposes of this subclause.“
First of all, I think this piece of code is strictly in line with the standard and there is no undefined behavior.
Secondly, *N
belongs to “Every access that modifies X“,What does this have to do with modifying P
?P
is a pointer object, and X
is the object it points to. Does modifying the object pointed to by the pointer also count as modifying the pointer?
The above is my question.
Thank you for your reading.