[英]What is the difference between char * x,y,z;char* x,y,z;char (*)x,y,z;?
Okay I am getting lost in these pointers can somebody exactly tell me what is(are) the difference between char * x,y,z;
好吧,我迷失在这些指针中,有人可以确切地告诉我
char * x,y,z;
之间的区别是什么char * x,y,z;
, char* x,y,z;
,
char* x,y,z;
and char (*)x,y,z;
和
char (*)x,y,z;
? ? If you can please provide resources to your answer or something.
如果可以,请为您的答案提供资源。
The first two mean the same thing. 前两个意思相同。 They declare x as a pointer to a char, and y and z as char variables.
它们将x声明为指向char的指针,将y和z声明为char变量。 The third one will cause a syntax error and, as @danielfranca pointed out in the comments, will not compile.
第三个将导致语法错误,并且正如@danielfranca在注释中指出的那样,将无法编译。
These two records 这两个记录
char * x,y,z;
char* x,y,z;
are identical and equivalent to 是相同的,相当于
char *x;
char y;
char z;
Take into account that these declarations are equivalent 考虑到这些声明是等效的
char*x;
char* x;
char * x;
char *x;
All them declare variable x as pointer to char. 它们都将变量x声明为char的指针。
This record 这个记录
char (*)x,y,z;
is invalid and will not be compiled. 无效,不会被编译。
I think you mean the following 我想你的意思是以下几点
char (*x),y,z;
In this case declaration 在这种情况下声明
char ( *x );
is equivalent to 相当于
char *x;
You may enclose in parentheses a declarator. 您可以在括号中附上声明者。 So the above record you could write like
所以你可以写上面的记录
char ( *x ), ( y ), ( z );
If you are asking the question, it is probably because you were looking for a shorthand way of declaring x, y and z all to be pointers to characters. 如果你问这个问题,可能是因为你正在寻找一种简单的方法来声明x,y和z都是字符的指针。
In most cases, you should just be clear about it: 在大多数情况下,你应该清楚它:
char *x, *y, *z;
In more complicated situations where you will use it often, you can use a typedef: 在经常使用它的更复杂的情况下,您可以使用typedef:
typedef char *cp_t;
cp_t x, y, z;
Declare variables in the same way as you're gonna use them. 以与您将使用它们相同的方式声明变量。
char *x, y, z;
*x
is a char, y
is a char, z
is a char. *x
是char, y
是char, z
是char。 So x
is a pointer to a char. 所以
x
是指向char的指针。
void f(void), (*g)(void);
f
and *g
are functions without parameters or return value. f
和*g
是没有参数或返回值的函数。 So g
is a pointer to such a function. 所以
g
是指向这种函数的指针。 The parentheses around *g
are needed because (void)
binds more tightly than *
. 需要
*g
左右的括号,因为(void)
比*
更紧密地绑定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.