What is the difference between int* ptr and int *ptr in C? [duplicate]

白昼怎懂夜的黑 提交于 2019-12-17 19:39:10

问题


I am fairly new at C and I don't know the difference between the following two variable declarations:

int* ptr;
int *ptr;

I think that in the declaration int* ptr;, ptr's value cannot be changed whereas it can be changed for the declaration, int *ptr;

I am not sure if that is it though.

What is the concept behind the two declarations?


回答1:


To the compiler, there is no difference between the two declarations.

To the human reader, the former may imply that the "int*" type applies to all declarations in the same statement. However, the * binds only to the following identifier.

For example, both of the following statements declare only one pointer.

int* ptr, foo, bar;
int *ptr, foo, bar;

This statement declares multiple pointers, which prevents using the "int*" spacing.

int *ptr1, *ptr2, *ptr3;



回答2:


Spaces in C are mostly insignificant. There are occasional cases where spaces are important, but these are few and far between. The two examples you posted are equivalent.




回答3:


Like the others said. There is no difference. If you want to understand more complex C type declaration you could find this link userful. Reading C declarations.




回答4:


It's called whitespace operator overloading, see here: http://www2.research.att.com/~bs/whitespace98.pdf




回答5:


int *p;

*p 


is no meaning for compiler, (int*) is a type named pointer.



来源:https://stackoverflow.com/questions/7799561/what-is-the-difference-between-int-ptr-and-int-ptr-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!