0

I am newer to C language. Let's get started with the test code below.

struct node {
    int data;
    struct node *next;
};
int main() {
    struct node *n;
    struct node *n2;
    struct node *n3;
    struct node *n4;
    struct node *n5;
    printf("%p\n", n);
    printf("%p\n", n2);
    printf("%p\n", n3);
    printf("%p\n", n4);
    printf("%p\n", n5);
    return 0;
}

The output show below,

0x7fff57c42888
0x0
0x0
0x0
0x0

I cannot understand that the pointer address of variable n is not null. And, The other variables that have the same *node type are 0X0. Who can explain the reason for it? Thx.

wujunbin
  • 341
  • 1
  • 3
  • 16
  • 1
    The code invokes **undefined behavior**. – n. m. could be an AI Nov 20 '16 at 10:59
  • Variables local to a function are **not** zero-initialized in `c`. You have to do proper initialization yourself. BTW - when printing a pointer value, you shall cast to `(void*)` like `printf("%p\n", (void*)n5);` – Support Ukraine Nov 20 '16 at 11:04
  • @n.m.: No attempt to use any of the pointers in order to access memory. Why undefined behavior? – barak manos Nov 20 '16 at 11:07
  • @barakmanos use of uninitialized pointer value - I guess that is UB (note - there is no `&` in the print) – Support Ukraine Nov 20 '16 at 11:11
  • 1
    @barakmanos because that's what the standard says. As much as looking at an unitialized pointer is UB. Just copy one, bang! UB. No need to dereference at all. – n. m. could be an AI Nov 20 '16 at 11:11
  • @4386427: So what? You're only printing the (junk) value of the pointer. It doesn't have to point to a valid (properly allocated) memory block. Same as `int n;` followed by `printf("%d",n);`. – barak manos Nov 20 '16 at 11:13
  • 1
    @barakmanos - Your `int` example is UB as well, AFAIK. As I recall it the only uninitialized variable type that you may read without UB is `unsigned char`. – Support Ukraine Nov 20 '16 at 11:15
  • Thx for your reply, Can I think that using any unsigned type variables will not invokes undefined behaviour, because Unsigned types have rarely trap representations. I referred to [the post](http://stackoverflow.com/questions/11962457/why-is-using-an-uninitialized-variable-undefined-behavior-in-c) – wujunbin Nov 20 '16 at 15:55
  • If I declare struct node n, will the n variable be automatic initialized? In my test, the n variable was initialized without my explicit declaration in program. – wujunbin Nov 20 '16 at 16:09
  • Then I do a small test. the code as follows, `void test() { struct node n; printf("%p\n", &n); } int main() { test(); test(); test(); test(); }` the output show below, 0x7fff537ac840 0x7fff537ac840 0x7fff537ac840 0x7fff537ac840 I cannot understand that every time calling test() prints the same address. – wujunbin Nov 20 '16 at 16:13

1 Answers1

2

You simply print out the values of non-initialised pointers (n, .., n5). So there is no guarantee of the output.

Just like you print out an int variable without initialising it:

int i;
printf( "%d", i ); // what would you expect? - no guarantee, maybe 0, maybe 7
artm
  • 17,291
  • 6
  • 38
  • 54