Taking issues one at a time...
Category *categoryList;
The type of the variable categoryList
is Category *
( pointer to Category
).
Category *ap = &categoryList;
The type of the variable ap
is Category *
, but the type of the expression &categoryList
is Category **
, or "pointer to pointer to Category
". This is your first type mismatch; the assignment should be written
Category *ap = categoryList;
Finally,
*ap = (Category *)malloc(sizeof(Category));
The expression *ap
has type Category
; malloc
returns a void *
, which you are casting to Category *
. You cannot assign a pointer value to a non-pointer type, which is where your compiler error is coming from. That line should be written
ap = malloc( sizeof *ap );
The cast is unnecessary1, and since the expression *ap
has type Category
, sizeof *ap
will give the same result as sizeof (Category)
2.
1. The cast is necessary in C++ and in very old versions of C predating the 1989 standard.
2. sizeof
is an operator, not a function; the only time parentheses are required is when the operand is a type name.