I'm working with pointers to structs and have the following set up which has been working.
/* Initialized. */
struct base { ... };
struct base **db;
db = malloc( base_max * sizeof *db );
/* When a new struct base is required. */
db[ i ] = malloc( sizeof( struct base ) );
Now, if possible, although not really essential, I'd like only db[0]
to point to a different struct, struct mem { ... }
. Is this possible and what is the proper way to do so?
I figured I can set db[0] = malloc( sizeof( struct mem ) )
; but db
is already declared to point to a pointer pointing to a struct base
; and the memory allocation of db
is based on that also.
I'm confused because I read pointers must have a type or pointer arithmetic won't work properly; but I also read you don't have to cast the pointers from malloc
even though it returns void pointers.
Regarding the duplicate question, although it discusses void type it doesn't answer my question of how to accomplish this or the risks of it. The comment by @4386427 appears to point to the real issue to be considered which isn't addressed in the other question or the one answer to this one. Thank you.