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? Then what is 'struct *A'? And I've seen someone writing 'int const * a', what does this mean?
相关问题
- Multiple sockets for clients to connect to
- Do the Java Integer and Double objects have unnece
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
They are all identical.
struct *A
=struct* A
=struct*A
=struct * A
.They're equivalent(ly wrong). C is a free-form language, whitespace doesn't matter.
No, it's (still) a syntax error (
struct
is a reserved keyword). If you substitute a valid structure name in there, then it will be one, yes.This declares
a
to be a pointer toconst int
.struct *A
,struct* A
andstruct * A
are all the same thing and all eqaully wrong since you're missing the struct's name.int const *a
is a the same asconst int *a
and it means pointer to a const integer.Aside:
int * const a
is different and it means const pointer and a non const integer.As others have already mentioned,
struct * A
et cetera are incorrect but identical.However, a structure and a pointer can be created in the following manner:
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: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'. 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". In your question above for
int const * a
you can say, "'a' is a pointer to a read-only 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!