C指针:什么';struc*A、struct*A和struct*A的区别是什么?
我正在做一些研究以更好地理解C中的指针,但我很难理解这些: “struct*A”是结构上的指针吗? 那么什么是“结构*A”?C指针:什么';struc*A、struct*A和struct*A的区别是什么?,c,pointers,struct,C,Pointers,Struct,我正在做一些研究以更好地理解C中的指针,但我很难理解这些: “struct*A”是结构上的指针吗? 那么什么是“结构*A”? 我见过有人写‘int const*a’,这是什么意思 它们都是相同的 struct*A=struct*A=struct*A=struct*Astruct*A,struct*A和struct*A都是一样的东西,而且都是完全错误的,因为您缺少结构的名称 int-const*a与const-int*a相同,表示指向常量整数的指针 旁白:int*常量a不同,它表示常量指针和非常量
我见过有人写‘int const*a’,这是什么意思 它们都是相同的
struct*A
=struct*A
=struct*A
=struct*Astruct*A
,struct*A
和struct*A
都是一样的东西,而且都是完全错误的,因为您缺少结构的名称
int-const*a
与const-int*a
相同,表示指向常量整数的指针
旁白:int*常量a
不同,它表示常量指针和非常量整数
struc*A
、struct*A
和struct*A
之间有什么区别
他们完全错了。C是一种自由形式的语言,空格不重要
struct*A
是结构上的指针吗
不,它(仍然)是一个语法错误(struct
是一个保留关键字)。如果您在其中替换了一个有效的结构名称,那么它将是一个,是的
int const*a
,这是什么意思
这将a
声明为指向const int
的指针,正如其他人已经提到的,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
?”,下面是我根据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;
当我第一次开始时,我会发现从右到左阅读定义很有帮助,用“只读”代替“常量”。例如,在最后一个指针中,我只想说,“constant_ptr_to_constant_char是指向只读字符的只读指针”。在上面关于int const*a
的问题中,您可以说,“'a'是指向只读int的指针”。看起来很蠢,但它起作用了
有一些变化,但当你遇到它们时,你可以通过搜索这个网站找到更多的例子。希望有帮助 struct*A==struct*A
struct*A
(带任何空格)无法编译,struct标记丢失。对于const
限定对象,没有必要的“常量”。@Carl:是的。但是有一个指向-const
的指针并不意味着对象是const
限定的。@BenVoigt,我完全同意你的第二句话,但是const
只是意味着“你不能修改它”,而不是说它是常数。它很可能会改变值。@卡尔:如果它应用于对象本身(在定义的意义上),那么这个值实际上是不可变的。(试图修改const
对象,例如通过丢弃const
,会导致未定义的行为)我明白你们的意思,所以我删除了常量一词以避免混淆。空格确实很重要。考虑<代码>无符号长桥;<代码>vs无符号长桥
@BenVoigt和“hello world”
与“helloworld”
不一样,当然。@Pete:是的,没错。重要的是代币序列。