When (void *) p == (void *) *p - What does the Standard say about this?

Viewed 105

Example:

int a[99];
int (*p)[99] = &a;

// this prints 1
printf("%d\n", (void *) p == (void *) *p);

In general, if p is a pointer to an array, then both the object representations (i.e. the bit patterns) of p and *p are equal.

I'm just lost and completely unsure about the portability of this behaviour.

So, I'm curious whether this behaviour is guaranteed by the Standard. If so, could someone please quote all of the relevant paragraphs that guarantee it?

2 Answers

This comparison is guaranteed to be 1.

The relevant part of the C standard is section 6.5.9p6 regarding the equality operator and the comparison of pointers:

Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.

Take particular note of the passage in bold. This means two things: 1) a pointer to a struct and a pointer to its first member (suitable converted) will compare equal, and 2) a pointer to an array and a pointer to its first member (again, suitable converted) will compare equal.

In your particular case, p points to an array and *p is the array itself, and using *p in an expression yields a pointer to its first member. Both are converted to void * to give them a common type. So this comparison will always evaluate to 1.

In general, if p is a pointer to an array, then both the object representations (i.e. the bit patterns) of p and *p are equal.

If p is a pointer to an array, then *p is the array. The bit representation of the array is the concatenation of the bit representations of the elements of the array (because C 2018 6.2.5 20 says an array is made of contiguously allocated objects). The bits in the array are not generally equal to the bits in the pointer.

However, when an array is used in an expression other than as the operand of unary & or the operand of sizeof or as a string literal used to initialize an array, the array is automatically converted to a pointer to its first element. The first element of the array *p is (*p)[0], so *p is automatically converted to &(*p)[0].

Then the question is whether (void *) p equals (void *) &(*p)[0].

C 2018 6.3.2.3 1 tells us any pointer to an object type may be converted to void *. However, it does not tell us what the results of comparisons are while the pointer is void *. It does tell us that converting the void * back to its original type yields a pointer that compares equal to the original.

C 2018 6.5.9 6 tells us “Two pointers compare equal if and only if …, both are pointers to the same object (including a pointer to an object and a subobject at its beginning)…” (I elided some other cases that are not of concern here.) What are we to make of this given two void *? It seems the intent is for a pointer to “point to an object” even if it is currently in the form of a void *. Then (void *) p points to the array and (void *) &(*p)[0] points to a subobject at its beginning, so they compare equal.

The semantics would be clearer with (char *) p == (char *) *p because C 2018 6.3.2.3 7 tells us that converting to char * produces a pointer to the first byte of an object, and the first byte of an array is the same as the first byte of its first element.

Related