You are doing it wrong on multiple counts:
- You're trying to set a variable
handle->ab
, but handle
is a void *
, not a structure type pointer.
- You need to show your call, but there's likely to be problems — why do you think a
void *
argument is a good idea?
- You want to allocate structures, so the
sizeof()
operands should be xyz_t
and not xyz_t *
; repeat for abc_t
.
You should probably use:
int Lookup(const char *lookup, xyz_t **handle)
{
...
*handle = (xyz_t *)malloc(sizeof(xyz_t));
(*handle)->ab = (abc_t *)malloc(sizeof(abc_t));
...
}
Don't forget to check the result of malloc()
.
There are those who will castigate you for using casts on malloc()
. I won't. When I learned C (a long time ago, years before there was a C standard), on a machine where the int *
value for an address was not the same bit pattern as the char *
address for the same memory location, where malloc()
had to be declared char *malloc()
or all hell broke loose, the casts were necessary. But — and this is the major issue that people are concerned about — it is crucial that you compile with compiler options such that if you invoke a function without a prototype in scope, you will get a compilation error, or a warning that you will pay attention to. The concern is that if you do not have a declaration for malloc()
in scope, you will get incorrect results from using the cast which the compiler would diagnose if you don't.
On the whole, though, I think you should separate your lookup code from your 'create xyz_t
' code — your function is doing two jobs and it complicates the interface to your function.
xyz_t *Create_xyz(void);
int Lookup(const char *lookup, const xyz_t *handle);