引入
我們用兩個簡單(?)的問題作為引入,當(dāng)然我們整篇文章都會圍繞這兩個問題:
- 下面四種類型聲明等價嗎?
int* i;
int *i;
int * i;
int*i;
- 下面四種類型聲明中够掠,
j
擁有哪一個數(shù)據(jù)類型过咬?是int
或者int
的指針?int* i, j;
int *i, j;
int * i, j;
int*i, j;
當(dāng)然看到這里壳猜,如果你已經(jīng)有非常自信的答案虹曙,你當(dāng)然可以無視這一篇文章后面的部分。
第一個問題
第一個問題的答案是: 四種指針的類型聲明的等價的绒障。
很好吨凑,既然我們知道它們等價,或許已經(jīng)足夠了户辱。但是我們數(shù)據(jù)庫教授總是在課上提醒大家鸵钝,知道區(qū)別并不足夠,還需要問自己其中的哪一個更好庐镐,什么情況下用哪一種方式恩商。
哪一種寫法更好呢?
C++之父在他的一篇文章:Bjarne Stroustrup's C++ Style and Technique FAQ中提到:
The choice between
int* p;
andint *p;
is not about right and wrong, but about style and emphasis. C emphasized expressions; declarations were often considered little more than a necessary evil. C++, on the other hand, has a heavy emphasis on types.A
typical C programmer
writesint *p;
and explains it*p is what is the int
emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.A
typical C++ programmer
writesint* p;
and explains itp is a pointer to an int
emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.
</br>
Linux kernel coding style中的習(xí)慣規(guī)則是:
When declaring pointer data or a function that returns a pointer type, the
preferred use of*
is adjacent to the data name or function name and not
adjacent to the type name. Examples:
char *linux_banner;
unsigned long long memparse(char *ptr, char **retptr);
char *match_strdup(substring_t *s);
某種程度上它也擬合了C++之父Bjarne Stroustrup的想法必逆。
第二個問題
第二個問題的答案是: j
的類型是int
怠堪。
當(dāng)然這問題是編譯器的行為,或者語言自身的規(guī)定名眉,很難去闡述行為或規(guī)定背后的為什么粟矿。
我們繼續(xù)看Bjarne Stroustrup's C++ Style and Technique FAQ,這部分緊接問題一中對應(yīng)的引用:
The critical confusion comes (only) when people try to declare several pointers with a single declaration:
int* p, p1; // probable error: p1 is not an int*
Placing the * closer to the name does not make this kind of error significantly less likely.
int *p, p1; // probable error?
Declaring one name per declaration minimizes the problem - in particular when we initialize the variables. People are far less likely to write:
int* p = &i;
int p1 = p; // error: int initialized by int*
And if they do, the compiler will complain.
從他的這一部分文字中损拢,很容易可以看出我們這文章中的第一個問題和第二個問題完全是緊密結(jié)合陌粹。第二個問題中的四種寫法的確是等價的,但是無一能讓人輕松理解福压。從他的這一個觀點(diǎn)中:Declaring one name per declaration minimizes the problem
掏秩,我們可以對這篇文章下一個主觀的結(jié)論。
結(jié)論
針對第二個問題荆姆,對我來說最好的方式是:
int* i;
int j;
這種寫法更清晰蒙幻,也是多個Stackoverflow提問中所提倡的。
最后一點(diǎn)聲明是胞枕,無論怎么寫對編譯器來說其實(shí)都無所謂,它只是客觀地去檢查能否通過編譯然后生成對應(yīng)的代碼魏宽。這些等價寫法對應(yīng)的匯編語言甚至一樣腐泻,對應(yīng)的程序的效能自然也是一樣决乎。這些寫法迷惑的只是人類(編程的人和瀏覽代碼的人),我們需要有一個清晰的方式派桩,保證自己不出錯同時也讓代碼瀏覽者能快速理解构诚。
</br>
</br>
引用和推薦閱讀:
http://www.stroustrup.com/bs_faq2.html#whitespace
https://www.kernel.org/doc/Documentation/process/coding-style.rst
https://softwareengineering.stackexchange.com/questions/7305/int-i-or-int-i-or-int-i
https://stackoverflow.com/questions/6990726/correct-way-of-declaring-pointer-variables-in-c-c
https://stackoverflow.com/questions/13894228/where-to-put-the-star-in-c-and-c-pointer-notation
https://stackoverflow.com/questions/2704167/type-declaration-pointer-asterisk-position
https://stackoverflow.com/questions/180401/placement-of-the-asterisk-in-pointer-declarations
(Stackoverflow統(tǒng)統(tǒng)是在問同一件問題)
該文章遵循創(chuàng)作共用版權(quán)協(xié)議 CC BY-NC 4.0,要求署名铆惑、非商業(yè) 范嘱、保持一致。在滿足創(chuàng)作共用版權(quán)協(xié)議 CC BY-NC 4.0 的基礎(chǔ)上可以轉(zhuǎn)載员魏,但請以超鏈接形式注明出處丑蛤。文章僅代表作者的知識和看法,如有不同觀點(diǎn)撕阎,可以回復(fù)并討論受裹。