#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;
}
能否请有经验的人告诉我这段代码哪里出了问题?从注释可以看出,我只是想实现指针和指针变量的使用。除了其他错误之外,在 (*addressOfVariable=10) 代码中我得到了一个 "非法间接访问错误"。
谢谢您的帮助。
conio.h
/_getch
,我的编译器可以顺利编译。你用的是哪个编译器/环境? - Maty
,它会显示10
。 - Barmar{
并在其匹配的}
之前)。在函数体的开头声明所有变量,MSVC 将编译代码。 - user539810