I have the following code:
#include <stdio.h>
typedef struct {
int* arg1;
int arg2;
} data;
int main(int argc, char** argv) {
data d;
printf("arg1: %p | arg2: %d
", d.arg1, d.arg2);
}
The output ends up being that d.arg1
is not NULL
and d.arg2
is 0. For example:
arg1: 0x7fff84b3d660 | arg2: 0
When I add a pointer to main, nothing changes. However, when I print that pointer:
#include <stdio.h>
typedef struct {
int* arg1;
int arg2;
} data;
int main(int argc, char** argv) {
data d;
int* test;
printf("arg1: %p | arg2: %d | test: %p
", d.arg1, d.arg2, test);
}
the output always results in:
arg1: (nil) | arg2: 4195264 | test: (nil)
Why am I experiencing this behaviour? I don't understand how printing another pointer value changes the value of a different pointer to NULL. Note that the compiler I am using is GCC 4.8.2.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…