C指针:struc * A,struct * A和struct * A之间有什么区别?
我正在做一些研究以更好地理解C中的指针,但我很难理解这些:’struct * A’是结构上的指针吗? 那么什么是’struct * A’? 而且我看到有人写’int const * a’,这是什么意思?
struc* A
,struct *A
和struct * A
之间有什么区别?
他们是等同的(错误的)。 C是一种自由forms的语言,空白并不重要。
struct* A
是struct* A
的指针吗?
不,它(仍)是语法错误( struct
是保留关键字)。 如果你在那里替换有效的结构名称,那么它将是一个,是的。
int const * a
,这是什么意思?
这声明a
一个指向const int
的指针。
struct *A
, struct* A
和struct * A
都是一样的,并且因为缺少结构名称而全部都是错误的。
int const *a
与const int *a
相同,它表示指向const整数的指针。
旁白: int * const a
是不同的,它表示const指针和非const整数。
它们都是相同的。
struct *A
= struct* A
= struct*A
= struct * A
正如其他人已经提到的那样, struct * A
等等不正确但相同。
但是,可以按以下方式创建结构和指针:
/* 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;
另外,关于指定声明指针的不同方式的第二个问题,“什么是int const * a
?”,这是一个我改编自C编程的例子,作者: Stephen G. Kochan :
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;
当我第一次开始时,我会发现从右到左阅读定义很有帮助,用’只读’代替’const’。 例如,在最后一个指针中,我只想说,“constant_ptr_to_constant_char是一个只读指向char的只读指针”。 在上面针对int const * a
问题中,您可以说,“’a’是指向只读int的指针”。 似乎很愚蠢,但它确实有效。
有一些变化,但当你遇到它们时,你可以通过搜索这个网站找到更多的例子。 希望有所帮助!