I'm confused about how null pointer access a structure member in c language.
#include <stdio.h>
struct A
{
int i;
char str[30];
};
int main()
{
struct A *p = NULL;
printf("accessing str with null pointer %d\n", p->str);
printf("accessing i with null pointer %d\n", p->i);
printf("accessing str[0] with null pointer %d\n", p->str[0]);
return 0;
}
In above example, only first printf is executing. Other printf are crashing the program. I thought all of them would crash the program. but why p->str(array member) isn't causing any problem? From my search I found those questions NULL pointer able to access class member function and Accessing class members on a NULL pointer which is helpful but questions are related with member function. Can someone help me to understand, how null pointer access work for member variables (array and non-array).