R
Robbie Brown
I've been reviewing what I've learned about pointers.
I thought I'd do a few tests just to consolidate what I thought I'd
learned and frankly .. I'm dumfounded.
int main(int argc, char *argv[]){
//declare a pointer to int
int *ip;
//print ... what exactly, prints 'nil'
printf("ip is %p\n", ip);
//dereference the pointer, seg fault
printf("*ip is %d\n", *ip);
}
the output is what I expected
ip is (nil)
Segmentation fault (core dumped)
I then add the following statement after the last printf
int *ip2;
compile and exec and get the same output
ip is (nil)
Segmentation fault (core dumped)
Now then, the next bit is a total head****
If I modify the last statement so that it reads
int *ip2 = NULL;
so the code is now
int main(int argc, char *argv[]){
//declare a pointer to int
int *ip;
//print ... what exactly, prints 'nil'
printf("ip is %p\n", ip);
//dereference the pointer, seg fault
printf("*ip is %d\n", *ip);
int *ip2 = NULL;
}
then compile and exec I get the following
ip is 0x7fff0dfeb230
*ip is 1
WTF!!! ... how does initalizing ip2 to NULL cause the
previous code to now display ... something.
Is this for real?
I mean seriously, this is just ... what
I have no idea
Dazed and confused.
I thought I'd do a few tests just to consolidate what I thought I'd
learned and frankly .. I'm dumfounded.
int main(int argc, char *argv[]){
//declare a pointer to int
int *ip;
//print ... what exactly, prints 'nil'
printf("ip is %p\n", ip);
//dereference the pointer, seg fault
printf("*ip is %d\n", *ip);
}
the output is what I expected
ip is (nil)
Segmentation fault (core dumped)
I then add the following statement after the last printf
int *ip2;
compile and exec and get the same output
ip is (nil)
Segmentation fault (core dumped)
Now then, the next bit is a total head****
If I modify the last statement so that it reads
int *ip2 = NULL;
so the code is now
int main(int argc, char *argv[]){
//declare a pointer to int
int *ip;
//print ... what exactly, prints 'nil'
printf("ip is %p\n", ip);
//dereference the pointer, seg fault
printf("*ip is %d\n", *ip);
int *ip2 = NULL;
}
then compile and exec I get the following
ip is 0x7fff0dfeb230
*ip is 1
WTF!!! ... how does initalizing ip2 to NULL cause the
previous code to now display ... something.
Is this for real?
I mean seriously, this is just ... what
I have no idea
Dazed and confused.