【发布时间】:2025-12-14 04:00:01
【问题描述】:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
int y = 4; //This is a variable stored in the stack
printf("\n Address of variable y is :%p\n", &y); // This is the address of the variable y
int *addressOfVariable = &y; //This is a pointer variable, a P.V stores the memory address of a variable
//Read the value stored in a memory address
int memoryValue = *addressOfVariable; //* is a dereference operator, it reads the value stored in a memory address and stores it in another variable
//Update the value stored in the memory address
*addressOfVariable = 10;
_getch();
return 0;
}
谁能告诉我这段代码有什么问题?从 cmets 可以清楚地看出,我只是想实现指针和指针变量的使用。在其他错误中,我在 (*addressOfVariable=10) 代码中收到“非法间接错误”。
感谢您的帮助。
【问题讨论】:
-
除了
conio.h/_getch对我来说编译干净。你的编译器/环境是什么? -
对我也很好。如果我在最后打印
y,它会显示10。 -
我在 Windows 上使用 MS Visual Studio 2012。我还收到“addressOfVariable undeclared identifier”错误。不知道为什么!
-
@BLUEPIXY:你能详细说明一下吗?
-
@self @user2981518: @BLUEPIXY 表示 MSVC 只允许 C89/C90 代码,其中语句和变量声明不能混用。函数体内的所有变量声明都必须位于新作用域的开头(紧接在
{之后和与之匹配的}之前)。在函数体的开头声明所有变量,MSVC会编译代码。
标签: c pointers dereference