這是我嘗試運行的代碼(有一個我正在處理的 C 賦值,但這只是為了幫助我理解 C 中指標的語法。)
#include <stdio.h>
struct cow{
int moo;
};
void newm(struct cow *a){
*a.moo = 5;
}
int main() {
// Write C code here
printf("Hello world");
struct cow a;
newm(&a);
printf("hallo %i", a.moo);
return 0;
}
運行代碼時,我收到以下錯誤訊息:
gcc /tmp/2RZ9WOHWdH.c -lm
/tmp/2RZ9WOHWdH.c: In function 'newm':
/tmp/2RZ9WOHWdH.c:9:9: error: 'a' is a pointer; did you mean to use '->'?
9 | *(a).moo = 5;
| ^
| ->
uj5u.com熱心網友回復:
在這個運算式中
*a.moo = 5;
假設資料成員moo是一個被取消參考的指標。
但實際上它a是一個指標。
后綴成員訪問運算子.的優先級高于一元運算子*。
所以你需要寫
a->moo = 5;
或者
( *a ).moo = 5;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/537839.html
標籤:C指针运算符优先级解引用
