C pointer question, dereferencing crash

Posted by skynorth on Stack Overflow See other posts from Stack Overflow or by skynorth
Published on 2010-05-23T22:05:19Z Indexed on 2010/05/23 22:10 UTC
Read the original article Hit count: 175

Filed under:
|

Why do this work?

    int *var;

while(scanf("%d", &var) && *var != 0)
        printf("%d \n", var);

While this does not?

    int *var;

while(scanf("%d", &var) && var != 0)
    printf("%d \n", var);

Doesn't * (dereference operator) give you the value pointed by the pointer? So why does *var != 0 crash the program, while var != 0 does not?

© Stack Overflow or respective owner

Related posts about c

    Related posts about pointers