指针地址/解引用运算符 #包括 #包括 #包括 int main() { int y=4;//这是存储在堆栈中的变量 printf(“\n变量y的地址是:%p\n”,&y);//这是变量y的地址 int*addressOfVariable=&y;//这是一个指针变量,P.V存储变量的内存地址 //读取存储在内存地址中的值 int memoryValue=*addressOfVariable;//*是一个解引用运算符,它读取存储在内存地址中的值,并将其存储在另一个变量中 //更新存储在内存地址中的值 *addressOfVariable=10; _getch(); 返回0; }

指针地址/解引用运算符 #包括 #包括 #包括 int main() { int y=4;//这是存储在堆栈中的变量 printf(“\n变量y的地址是:%p\n”,&y);//这是变量y的地址 int*addressOfVariable=&y;//这是一个指针变量,P.V存储变量的内存地址 //读取存储在内存地址中的值 int memoryValue=*addressOfVariable;//*是一个解引用运算符,它读取存储在内存地址中的值,并将其存储在另一个变量中 //更新存储在内存地址中的值 *addressOfVariable=10; _getch(); 返回0; },c,pointers,dereference,C,Pointers,Dereference,有人能告诉我这个代码有什么问题吗?从评论中可以清楚地看到,我只是试图实现指针和指针变量的使用。在其他错误中,我在(*addressOfVariable=10)代码中得到了一个“非法间接错误” 谢谢你的帮助。试试这个 #include <stdio.h> #include <conio.h> #include <stdlib.h> int main() { int y = 4; //This is a variable stored in the stac

有人能告诉我这个代码有什么问题吗?从评论中可以清楚地看到,我只是试图实现指针和指针变量的使用。在其他错误中,我在(*addressOfVariable=10)代码中得到了一个“非法间接错误”

谢谢你的帮助。

试试这个

#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;
}

指针或解引用运算符(
*
)在这里没有问题。您似乎不是在C99模式下编译代码。在C89中,不允许混合类型声明

编辑:正如他在评论中所说的,他使用MS VisualStudio 2012,MSVC不支持C99(基本上它是C++编译器)。无法在C99模式下编译代码。现在声明代码开头的所有变量,如C89

    int y=4;
    int *addressOfVariable=&y;
    int memoryValue=*addressOfVariable;
    printf("\n Address of variable y is :%p\n",&y);
    *addressOfVariable=10;
    _getch();

除了
conio.h
/
\u getch
为我干净地编译。你的编译器/环境是什么?对我来说也很好。如果我在最后打印
y
,它会显示
10
。我正在Windows上使用MS Visual Studio 2012。我还得到了一个“addressOfVariable undeclared identifier”错误。不知道为什么@BLUEPIXY:您能再详细说明一下吗?@self@user2981518:@BLUEPIXY意味着MSVC只允许C89/C90代码,其中语句和变量声明不能混合。函数体中的所有变量声明都必须位于新范围的开始处(紧跟在
{
之后和匹配的
}
之前)。在函数体的开头声明所有变量,MSVC将编译代码。我不会反对投票,但我希望你意识到这不是一个完整的答案。它给出了正确的代码,但没有解释为什么与问题中的代码相比它是正确的。@ChronoKitsune我已经描述过了。但他不明白。所以显示了具体的代码。我不知道想想微软。为什么C89会出现在2013年?@ChronoKitsune我会这么想,因为我认为它遇到了它。但是它不同。@BLUEPIXY-没有任何解释,这个答案根本没有帮助。
int y=4;
int *addressOfVariable=&y;
int memoryValue=*addressOfVariable; 
....