If you initialize a structure pointer to NULL, and try to change its members, is that what you call undefined behaviour(UB)?
I have this code:
#include <stdio.h>
typedef struct aStructure {
int testInt;
}aStructure;
int main(void) {
aStructure * a=NULL;
a->testInt = 123;
printf("%d", a->testInt);
}
This happens when I run it:
I am wondering a little what goes on "under the hood" here? When I initialize a structure pointer without having initialized the structure, does C then "set something aside" with the correct members? Because it seems that I can initialize the members without having the structure intialized, only the pointer?
If you initialize a structure pointer to NULL, and try to change its members, is that what you call undefined behaviour(UB)?
Only a lot.
This happens when I run it:…
C is not one thing. The C standard specifies a base language. Compilers vary in how they implement and extend that language. When asking about what specific thing happens in your program with undefined behavior, you need to completely specify the C implementation you used, including the compiler, its version, the switches you compiled with, and the hardware and operating system you ran the program on.
It takes some seconds for it to stop.
This is unusual. In most C implementations, the program would either immediately crash or immediately print something and complete execution.
When I initialize a structure pointer without having initialized the structure, does C then "set something aside" with the correct members?
No.
Because it seems that I can initialize the members without having the structure intialized, only the pointer?
It is not correct to draw this conclusion from the behavior you report. Taking some seconds for the program to stop and printing nothing is not an indication that the program successfully initialized any member of a structure.