Let's assume I have a struct
like this:
typedef struct {
char* author;
uint32_t isbn;
} Book;
int
main( int arg_c; char* arg_v[] ) {
Book* b = malloc( sizeof *b );
char* author = "Miller";
//[...]
free(b);
return 0;
}
Now what kinda trips me up is line 8: Book* b = malloc( sizeof *b );
here, I'm creating a pointer b
of type Book
on the left side of the equals sign, but at the same time, I'm dereferencing b
on the right side to allocate memory for b
.
How does that work?
How can I define a pointer at the same time, as I use that pointer to allocate memory for it.
If they line were Book* b = malloc( sizeof (Book) );
that is immediately intuitive for me, since sizeof
interrogates the size of a type. But here, it's as if the identifier is used in two places at the same time, while it's being defined.
Is there some double-pass thing going on, that I'm missing?