So the behavior is undefined only if the variable is modified through another pointer that's not the restricted pointer itself? What if the value is changed through the variable the restricted pointer points to?
For example:
Code:
#include <stdio.h>
int main(void) {
int i = 0;
int *ptr = &i;
int *restrict r_ptr = &i; /* Wouldn't this be undefined? Another pointer
points to that address */
// Accessing value through non-restricted pointer
(void) printf("%i\n", *ptr);
/* Modifying value through variable, and accessing value through
non-restricted pointer */
i = 1;
(void) printf("%i\n", *ptr);
/* Modifying and accessing value through restriced pointer */
*r_ptr = 2;
(void) printf("%i\n", *r_ptr);
/* Modifying value through variable, and accessing value through restricted
pointer */
i = 3;
(void) printf("%i\n", *r_ptr);
/* Modifying value through non-restricted pointer, and accessing value
through restricted pointer. Is this the only undefined case? */
*ptr = 4;
(void) printf("%i\n", *r_ptr);
return 0;
}
Wouldn't the assignment of the address to the restricted pointer cause undefined behavior? And if it doesn't, is the last scenario the only one that causes undefined behavior?
And if read-only variables are unaffected, why do some functions in the standard library declare parameters as restricted pointers to constants?
Code:
int printf(const char *restrict format, ...);
size_t fwrite(const void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream);
Os is this a safeguard just in case the pointer points to a string literal (in the case of printf) or a constant array (in the case of fwrite)?