Hello,
I am learning C by reading the second edition of "The C Programming Language" by K&R but am struggling to understand this bit of code on page 145:
struct nlist *np;
if ((np = lookup(name)) == NULL){
np = (struct nlist *) malloc(sizeof(*np));
.....
}
Lookup returns a pointer to a struct object with the given name value or NULL if not found. My understanding of sizeof is that it returns the size of a given object or type.
Given that the statement within the if block is only executed if np is assigned to NULL, I don't understand why you are allowed to dereference np to get to a non-existent struct nlist object before invoking sizeof on it?
Thanks for your help.
Gavin