简体   繁体   English

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

[英]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 and struct * A ? struc* Astruct *Astruct * 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* Astruct* 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 . 这声明aconst 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 *Astruct* Astruct * A都是一样的东西,并且都完全错误,因为您缺少struct的名称。

int const *a is a the same as const int *a and it means pointer to a const integer. int const *aconst 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.

相关问题 struct {...}和struct {union {struct {...}}}之间有什么区别? - What's the difference between struct { … } and struct { union { struct { … } } }? char []和char *在struct中有什么区别? - What's the difference between char [] and char * in struct? `typedef struct X {}`和`typedef struct {} X`有什么区别? - What's the difference between `typedef struct X { }` and `typedef struct { } X`? C 中的 struct 和 typedef struct 之间的真正区别是什么? - What is the real difference between struct and typedef struct in C? 是否将结构初始化为指针有什么区别? - What's the difference between intializating a struct as pointer or not? 使用指向结构的指针序列化结构 - Serialize struct with a pointer to a struc struct node和struct node *之间的'->'有什么区别? - What is the difference in '->' between struct node and struct node*? struct addrinfo和struct sockaddr有什么区别 - What is the difference between struct addrinfo and struct sockaddr 使用 malloc 为结构指针分配内存和将结构指针指向结构的内存地址有什么区别? - What is the difference between using malloc to allocate memory to a struct pointer and pointing a struct pointer to a struct's memory address? 以下C代码-结构和指针有什么问题 - What's wrong with the following C code- Struct and pointers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM