C pointer initialization and dereferencing, what's wrong here?

Posted by randombits on Stack Overflow See other posts from Stack Overflow or by randombits
Published on 2010-06-10T23:53:20Z Indexed on 2010/06/11 0:02 UTC
Read the original article Hit count: 160

Filed under:
|
|
|

This should be super simple, but I'm not sure why the compiler is complaining here.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  int *n = 5;

  printf ("n: %d", *n);

  exit(0);
}

Getting the following complaints:

foo.c: In function ‘main’:
foo.c:6: warning: initialization makes pointer from integer without a cast

I just want to print the value that the pointer n references. I'm dereferencing it in the printf() statement and I get a segmentation fault. Compiling this with gcc -o foo foo.c.

© Stack Overflow or respective owner

Related posts about c

    Related posts about pointers