c - 关于C中指针的一些疑问

标签 c

1) 初始化我使用的指针:

int number, *Pnumber;
    Pnumber=&number;
    number=10;

我这样做对吗?

关于:

int *Pnumber;
*Pnumber=10;

当我编译它时,我得到:

RUN FAILED (exit value 1, total time: 858ms)

顺便说一句。我需要使用 free(Pnumber) 来释放内存吗?

最佳答案

Am I doing this right?

是的,你是。

what about:

  `int *Pnumber;  
   *Pnumber=10;`

Pnumber 是一个初始化指针。取消引用此指针会导致未定义的行为。 Pnumber 必须指向分配的内存(指向变量或动态分配的内存区域)。

btw. do I need to use free(Pnumber) to free the memory?

只要不使用malloc,就不要使用free

关于c - 关于C中指针的一些疑问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14547760/

相关文章:

c - 有人记得 Visual C 版本 5 中 CL.EXE 的选项吗?

c - 为什么在定义后更改函数原型(prototype)是合法的?

c - openMP:为什么我在使用 "#pragma omp parallel num_threads(4)"时没有得到不同的线程 ID

python - 量化文件更改

在 C 中将 int 转换为字节数组?

c - IP 地址 char* 变量在循环从 ifa_addr 收集地址时意外更改

c - 如何将整数分配给c中的指针?

c - Sleep() 函数在 C 中计时不正确

c++ - mremap 是否与 malloc 一起使用?

c - 如何下载 NCurses 并将其添加到适用于 Windows 10 的 Code::Blocks 16.01 IDE