簡體   English   中英

指針地址/解除引用運算符

[英]Pointer Address/Dereferencing operator

#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)代碼中遇到“非法間接錯誤”。

謝謝您的幫助。

這里指針或解除引用運算符( * )沒有任何問題。 您似乎沒有在C99模式下編譯代碼。 在C89中,不允許混合類型聲明。

編輯:正如OP在他的評論中所說他正在使用MS Visual Studio 2012,MSVC不支持C99(基本上它是一個C ++編譯器)。 您無法在C99模式下編譯代碼。 現在聲明代碼開頭的所有變量,如C89;

int y=4;
int *addressOfVariable=&y;
int memoryValue=*addressOfVariable; 
....   

試試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM