I'm new to C and I'm wondering what the difference is between these two:
char* name;
struct book *abook;
I know the struct constructs a book, but with the char how come the *
is place before the variable name?
I'm new to C and I'm wondering what the difference is between these two:
char* name;
struct book *abook;
I know the struct constructs a book, but with the char how come the *
is place before the variable name?
TYPE * variableName, TYPE* variableName and TYPE *variableName are all syntactically the same. It's a matter of notation how you use them.
Personally I prefer the last form. It's simply because the star operator works on the token to the right of it. In a declaration like
TYPE* foo, bar;
only foo is a pointer but not bar. Therefore it looks more logical to put the star right next to the variable name.
The C compiler will ignore whitespace between symbols so:
char* name;
is the same as
char *name;
char * name;
char*name;
and
struct book *abook;
is the same as
struct book* abook;
struct book * abook;
struct book*abook;
The asterisk is probably best placed next to the variable name, as it only applies to the variable it is in front of, and not the type. So if you do:
int* a1, b1;
You have defined a1 as an int pointer and b1 as an int so this is a little clearer:
int *a1, b1;
As far as the internal difference between a pointer to a char and a pointer to a struct book, they both are 8 bytes (on a 64 bit system - 4 bytes on 32 bit system). That is,
sizeof (char *) is the same as sizeof(struct book *).
But the compiler keeps information about what each points to so it knows how to handle things like when you increment one ( a++ ) or dereference one ( *b ). That is, it needs to know how many bytes are involved, in addition to the first one who's address is stored in the pointer variable.
If a1 points to an int, a1++ should now point four bytes further (the address integer value in a1 should be 4 higher). While if a1 points to a char, a1++ should now point only one byte further.
struct book
and char
are both types.
So struct book* abook
is a pointer to a structure of the type book with the pointer being named abook.
The char* name
declaration is the same. It is a pointer to the char type.
This way of defining pointers is often used to pass text strings to functions without filling the local stack heap to much. It's also less CPU intensive than copying a whole char array. So mostly this char pointer will point to the first char in an array (example: char name[30];
)
If you want to create multiple instances of an structure you need to tag declaration of the structure. If the declaration is tagged, the tag can be used later in definitions of instances of the structure. For example:
struct book b;
defines a variable pt which is a structure of type struct point.
struct book
is your type. Now you can define pointers to this type.
If it still is confusing for you, use typedef
to define a new type for this structure :
typedef struct book Book;
typedef struct book* BookPtr;
Just remember that BookPtr
will only affect first variable.