C指针初始化和解除引用,这里有什么问题?

ran*_*its 1 c pointers dereference

这应该是超级简单的,但我不确定为什么编译器在这里抱怨.

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

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

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

  exit(0);
}
Run Code Online (Sandbox Code Playgroud)

得到以下投诉:

foo.c:在函数'main'中:
foo.c:6:警告:初始化使得整数指针没有强制转换

我只想打印指针n引用的值.我在printf()语句中取消引用它,我得到一个分段错误.用gcc -o foo foo.c编译它.

sth*_*sth 7

您将指针设置为内存地址5,以便它指向地址5可能是的任何内容.您可能希望将其指向5存储值的地址.例如:

int v = 5;    // Store the value 5 in a normal variable
int *n = &v;  // Make n contain the address of v, so that it points to the 
              // contents of v
Run Code Online (Sandbox Code Playgroud)