[英]C pointers: what's the difference between struc* A, struct *A and struct * A?
I'm doing some researches to better understand pointers in C, but I'm having a hard time understanding these: Is 'struct* A' a pointer on a structure? 我正在做一些研究以更好地理解C中的指针,但是我很难理解这些内容:“ struct * A”是结构上的指针吗? Then what is 'struct *A'?
那么“ struct * A”是什么? And I've seen someone writing 'int const * a', what does this mean?
我见过有人在写“ int const * a”,这是什么意思?
what's the difference between
struc* A
,struct *A
andstruct * A
?struc* A
,struct *A
和struct * A
什么区别?
They're equivalent(ly wrong). 它们是等效的(完全错误)。 C is a free-form language, whitespace doesn't matter.
C是一种自由格式的语言,空格无关紧要。
Is
struct* A
a pointer on a structure?struct* A
是struct* A
的指针吗?
No, it's (still) a syntax error ( struct
is a reserved keyword). 不,这是(仍然)语法错误(
struct
是保留关键字)。 If you substitute a valid structure name in there, then it will be one, yes. 如果在其中替换一个有效的结构名称,那么它将是一个,是的。
int const * a
, what does this mean?int const * a
,这是什么意思?
This declares a
to be a pointer to const int
. 这声明
a
为const int
的指针。
struct *A
, struct* A
and struct * A
are all the same thing and all eqaully wrong since you're missing the struct's name. struct *A
, struct* A
和struct * A
都是一样的东西,并且都完全错误,因为您缺少struct的名称。
int const *a
is a the same as const int *a
and it means pointer to a const integer. int const *a
与const int *a
相同,它表示指向const整数的指针。
Aside: int * const a
is different and it means const pointer and a non const integer. 另外:
int * const a
是不同的,它表示const指针和非const整数。
They are all identical. 它们都是相同的。
struct *A
= struct* A
= struct*A
= struct * A
. struct *A
= struct* A
= struct*A
= struct * A
As others have already mentioned, struct * A
et cetera are incorrect but identical. 正如其他人已经提到的,
struct * A
等是不正确的,但相同。
However, a structure and a pointer can be created in the following manner: 但是,可以通过以下方式创建结构和指针:
/* Structure definition. */
struct Date
{
int month;
int day;
int year;
};
/* Declaring the structure of type Date named today. */
struct Date today;
/* Declaring a pointer to a Date structure (named procrastinate). */
struct Date * procrastinate;
/* The pointer 'procrastinate' now points to the structure 'today' */
procrastinate = &today;
Also, for your secondary question about the different ways in which pointers are declared, "What is int const * a
?", here is an example that I adapted from Programming in C, by Stephen G. Kochan : 另外,对于关于指针声明的不同方法的第二个问题,“什么是
int const * a
?”,这是我从Stephen G. Kochan改编自C的示例:
char my_char = 'X';
/* This pointer will always point to my_char. */
char * const constant_pointer_to_char = &my_char;
/* This pointer will never change the value of my_char. */
const char * pointer_to_a_constant_char = &my_char;
/* This pointer will always point to my_char and never change its value. */
const char * const constant_ptr_to_constant_char = &my_char;
When I first got started, I would find it helpful to read the definition from right to left, substituting the word 'read-only' for 'const'. 当我第一次开始时,我会发现从右到左阅读定义很有帮助,用“只读”代替“ const”。 For example, in the last pointer I would simply say, "constant_ptr_to_constant_char is a read-only pointer to a char that is read-only".
例如,在最后一个指针中,我将简单地说:“ constant_ptr_to_constant_char是指向只读char的只读指针”。 In your question above for
int const * a
you can say, "'a' is a pointer to a read-only int". 在上面关于
int const * a
问题中,您可以说,“'a'是指向只读int的指针”。 Seems dumb, but it works. 似乎很愚蠢,但可以。
There are some variations but when you run into them, you can find more examples by searching around this site. 有一些变体,但是当您遇到它们时,可以通过在此站点附近搜索找到更多示例。 Hope that helps!
希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.