This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

void pointer to struct pointer cast

I declared a void pointer

void *ptr;

and a typedef struct named testStructure, with a member 'unsigned int a'.

Now I will cast this pointer to a struct pointer:

ptr=(testStructure*)malloc(sizeof(testStructure));

Now I try to access the member of the struct...

ptr->a=5;

...and I get an error "expression must have a pointer-to-struct-or-union type"...
Why is 'ptr' still void? It seems, a void pointer cannot be casted to any other pointer type...
How I can do this properly?
I just need ONE pointer, which can point to different structures, that means, I need to cast a void pointer...

Parents
  • What you say is all good but I'm having a similar problem casting (elegantly) a common pointer at runtime.

    I have a common kernel which uses various dll's to implement a particular sub-system determined at runtime. Each sub-system requires a different data structure; the pointer to which must be stored at the kernel level to be within scope at runtime when I malloc() the required structure space.

    I therefore want a generic (void*) pointer in the kernel. I do not want to use a specific typedef as this requires the sub-system data structure to be defined in an include that the kernel has access to (this is really my problem).

    I am trying to make each sub-system (dll) self contained so wish to keep all the data definitions with the sub-system (i.e. not available to the kernel at build time).

    So what I require is a way to declare a pointer (presumably 'void') and then assign it a structure type at runtime so I don't need to cast each access to the structure.

    Doesn't seem a big ask but I can't see anyway around not using your

    (struct*)ptr->a
    

    syntax but this will be very messy for the many hundreds of references I'm making to the structure members.

    Any other ideas?

Reply
  • What you say is all good but I'm having a similar problem casting (elegantly) a common pointer at runtime.

    I have a common kernel which uses various dll's to implement a particular sub-system determined at runtime. Each sub-system requires a different data structure; the pointer to which must be stored at the kernel level to be within scope at runtime when I malloc() the required structure space.

    I therefore want a generic (void*) pointer in the kernel. I do not want to use a specific typedef as this requires the sub-system data structure to be defined in an include that the kernel has access to (this is really my problem).

    I am trying to make each sub-system (dll) self contained so wish to keep all the data definitions with the sub-system (i.e. not available to the kernel at build time).

    So what I require is a way to declare a pointer (presumably 'void') and then assign it a structure type at runtime so I don't need to cast each access to the structure.

    Doesn't seem a big ask but I can't see anyway around not using your

    (struct*)ptr->a
    

    syntax but this will be very messy for the many hundreds of references I'm making to the structure members.

    Any other ideas?

Children