繁体   English   中英

C指针:struc * A,struct * A和struct * A有什么区别?

[英]C pointers: what's the difference between struc* A, struct *A and struct * A?

我正在做一些研究以更好地理解C中的指针,但是我很难理解这些内容:“ struct * A”是结构上的指针吗? 那么“ struct * A”是什么? 我见过有人在写“ int const * a”,这是什么意思?

struc* Astruct *Astruct * A什么区别?

它们是等效的(完全错误)。 C是一种自由格式的语言,空格无关紧要。

struct* Astruct* A的指针吗?

不,这是(仍然)语法错误( struct是保留关键字)。 如果在其中替换一个有效的结构名称,那么它将是一个,是的。

int const * a ,这是什么意思?

这声明aconst int的指针。

struct *Astruct* Astruct * A都是一样的东西,并且都完全错误,因为您缺少struct的名称。

int const *aconst 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 ?”,这是我从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; 

当我第一次开始时,我会发现从右到左阅读定义很有帮助,用“只读”代替“ const”。 例如,在最后一个指针中,我将简单地说:“ constant_ptr_to_constant_char是指向只读char的只读指针”。 在上面关于int const * a问题中,您可以说,“'a'是指向只读int的指针”。 似乎很愚蠢,但可以。

有一些变体,但是当您遇到它们时,可以通过在此站点附近搜索找到更多示例。 希望有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM