我了解到可以用 3 種不同的方式宣告指標:
int* a;
int *b;
int * c;
我更喜歡:
int* a;
在宣告一個指向結構的指標時,像下面這樣寫是否正確:
struct Card {
int a;
};
struct Card my_card = { 3, 7 };
struct Card* p = &my_card;
(*p).a = 8;
我很困惑,因為我發現的所有地方都宣告如下:
struct Card *p = &my_card;
提前致謝。
uj5u.com熱心網友回復:
你把空格放在哪里并不重要。 struct Card* p并且struct Card *p同樣有效;至于你喜歡哪一個,這只是風格問題。
我同意第二種形式更常見,但最重要的是您在整個代碼中始終使用一種形式。您的老板/老師/其他開發人員也可能有指定使用哪種形式的編碼風格標準。
uj5u.com熱心網友回復:
如果T是某個型別說明符,則T可以通過以下任何一種方式宣告指向該型別物件的指標
T*p;
T* p;
T *p;
T * p;
例如,如果T是int *那么指標宣告可以看起來像
int **p;
int ** p;
int * *p;
int * * p;
與您可以宣告指向結構的指標相同的方式
struct Card*p = &my_card;
struct Card* p = &my_card;
struct Card *p = &my_card;
struct Card * p = &my_card;
注意你可能會寫
T ( *p );
但你可能不會寫
( T* ) p;
此外,還有另一個微妙之處。如果你會寫例如
int* p1, p2
那么變數p1具有型別,int *而變數p2具有型別int而不是int *.
但是如果你會寫
typedef int * T;
當在這個宣告中
T p1, p2;
這兩個變數都有型別int *。
uj5u.com熱心網友回復:
IMOint *a比int* a. 在C中,*a可以讀作'content of a',所以int *a可以讀作'content of ais an integer',同樣的int a讀作' ais an integer'。正如其他答案所清除的那樣,空格在這里并不重要——它們對編譯器來說都是一樣的。
在宣告多個指標變數(如int *a, *b. 很容易在撰寫int* a, b和期望兩者a以及b成為 int 指標時出錯。
uj5u.com熱心網友回復:
就編譯器而言,
T *a;
T* a;
T*a;
T * a;
都意味著完全相同的事情 - 都被解釋為T (*a)(a型別為“指向T”)。在這種情況下,空格并不重要。有了您的struct型別,struct Card *p并struct Card* p做完全一樣的東西,都被解釋為struct Card (*p)。
從語法上講,*始終系結到宣告符(更多內容見下文)。這意味著像這樣的宣告
T* a, b;
被解釋為
T (*a), b;
并且僅宣告a為指向T-的指標b是常規T. 這是我建議不要使用該T* p樣式的眾多原因之一(我將在下面給出更多原因)。
在C中,宣告有兩個主要部分-的序列宣告說明符(型別說明符,struct,union,和enum說明符,存盤類說明符,型別限定符等),接著以逗號分隔的串列說明符。在像這樣的宣告中
static unsigned long int a[10], *p, f(void);
宣告說明符static unsigned long int和說明符a[10],*p和f(void)。
宣告符引入了被宣告事物的名稱(a、p、 和f)以及關于該事物的陣列性、指標性和函式性的資訊。每個專案的型別完全由宣告說明符和宣告符的組合指定。
宣告符的結構與代碼中運算式的結構相匹配 - 如果您有一個指向的指標陣列int并且想要訪問特定int值,則需要對陣列進行索引并使用一元運算符取消參考結果*:
printf( "%d\n", *ap[i] );
的表達 *ap[i]有型別int的,所以的宣告ap陣列是
int *ap[N];
宣告*ap[N]符匹配運算式的結構*ap[i]。
在宣告中,*and[]和()運算子僅用于指示型別 - 您實際上并沒有取消參考或索引或呼叫任何東西。但是,它們確實遵循與在運算式中相同的優先級規則。后綴運算子的優先級高于一元運算子,因此[]和()“系結”之前*:
T *a[N]; // parsed as *(a[N]) -- a is an array of pointers
T *f(void); // parsed as *(f(void)) - f is function returning a pointer
要宣告指向陣列或函式的指標,必須將*運算子與陣列或函式運算式顯式分組:
T (*a)[N]; // a is a pointer to an array
T (*f)(void); // f is a pointer to a function
現在對于這個答案的編輯部分(你可以隨意忽略)......
Over time I've become more militant about discouraging the T* p style of pointer declarations. It's the preferred style among C programmers, but the more you think about it the less sense it makes, and in my experience it just causes problems.
Its stated purpose - emphasizing the "pointer-ness" of the variable - is spurious. You can't emphasize the "array-ness" of a variable by declaring it as
T[N] a; // syntax error
or the "function-ness" as
T(void) f; // syntax error
because the operands of the postfix [] and () operators are a and f, not T. Similarly, in a declaration like
T* p; // parsed as T (*p);
the operand of the unary * operator is p, not T. And the * operator is unary (prefix), not postfix, so T* just looks wrong anyway. Because it's unary and because whitespace doesn't matter T* p will work as expected, but you're kind of ignoring the rules of the language when you do it.
If nothing else, it's inconsistent with declaring pointers to arrays or pointers to functions:
T (*ap)[N]; // ap is a pointer to an N-element array of T
T (*fp)(void); // fp is a pointer to a function returning T
and declaring a pointer to an array of pointers like
T* (*ap)[N]; // ap is a pointer to an array of pointers to T
is ugly and just indicates confused thinking, and like I said earlier you can't follow that convention with array or function declarations:
T[N] a; // syntax error
T(void) f; // syntax error
and if you use it you will inevitably screw up and write
T* a, b;
when you meant to write
T *a, *b;
Now, the inevitable response to that objection is to put separate declarations on separate lines:
T* a;
T* b;
and yes, there are plenty of good reasons to have one declaration per line, but working around bad practice isn't one of them. Write those declarations as
T *a;
T *b;
instead.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315261.html
上一篇:通過JSP使用commons-fileupload決議本地驅動器的檔案(空List<FileItem>)。
下一篇:指向在函式內創建的區域變數的指標
