N1570 claims this behavior is undefined:
Β§J.2 / 1 The value of an object with automatic storage duration is used while it is undefined (6.2.4, 6.7.9, 6.8).
And in this case, our pointer has an undefined value:
Β§6.7.9 / 10 If an object with automatic storage duration is not initialized explicitly, its value is undefined. If an object that has a static or storage duration of threads is not initialized explicitly, then:
- if it has a pointer type, it is initialized with a null pointer;
Then I assume that the following test program demonstrates undefined behavior:
#include <stdio.h> int main(void) { char * ptr; printf("%p", (void*)&ptr); }
My motivating concern is the strtol function. First, let me quote the parts of the N1570 related to the endptr parameter:
Β§7.22.1.4 / 5 If the sequence of objects has the expected shape and the value of the base is zero, the sequence of characters starting with the first digit is interpreted as an integer constant in accordance with rule 6.4.4.1. [...] The pointer to the final string is stored in the object pointed to by endptr , provided that endptr not a null pointer.
Β§7.22.1.4 / 7 If the sequence of items is empty or does not have the expected shape, the conversion is not performed; the nptr value is equally stored in the object pointed to by endptr , provided that endptr not a null pointer.
This means that endptr must point to an object, and that endptr at some point. For example, this implementation does this :
if (endptr != 0) *endptr = (char *)(any ? s - 1 : nptr);
However, this highly endptr answer , as well as this manual page and show endptr passed to endptr is uninitialized. Is there an exception that makes this non-undefined behavior?