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
  • Ok, I get it, and it works, until I added a void pointer to void pointer in the structure:

    typedef struct
    {
    int p1,p2;
    void **dynStructArray;
    } tDSA;
    
    tDSA *parent;
    

    Now i will create something similar to a tree:

    parent=malloc(sizeof(tDSA));
    parent->dynStructArray=malloc(sizeof(*(parent->dynStructArray)));
    *(parent->dynStructArray)=malloc(sizeof(tDSA));
    

    And I try to access a member of the structure 'in' the structure:
    A pointer in the structure tDSA points to an array of pointers, which one of them (for now only the first exists) points to another dynamically created structure tDSA.

    (*(struct tDSA*)(parent->dynStructArray))->p1=5; //dereferencing void pointer as tDSA pointer
    

    And it still doesn't work, because the compiler says: "incomplete type is not allowed"...

    Why is the tDSA structure incomplete? I think, it includes just two ints, and one pointer,
    so what is inclomplete...?

Reply
  • Ok, I get it, and it works, until I added a void pointer to void pointer in the structure:

    typedef struct
    {
    int p1,p2;
    void **dynStructArray;
    } tDSA;
    
    tDSA *parent;
    

    Now i will create something similar to a tree:

    parent=malloc(sizeof(tDSA));
    parent->dynStructArray=malloc(sizeof(*(parent->dynStructArray)));
    *(parent->dynStructArray)=malloc(sizeof(tDSA));
    

    And I try to access a member of the structure 'in' the structure:
    A pointer in the structure tDSA points to an array of pointers, which one of them (for now only the first exists) points to another dynamically created structure tDSA.

    (*(struct tDSA*)(parent->dynStructArray))->p1=5; //dereferencing void pointer as tDSA pointer
    

    And it still doesn't work, because the compiler says: "incomplete type is not allowed"...

    Why is the tDSA structure incomplete? I think, it includes just two ints, and one pointer,
    so what is inclomplete...?

Children
  • It's the same kind of mistake as the first time. As Andy said, if you want dynStructArray to be a pointer to an array of tDSA's, then why not declare it as such? It will save you many unnecessary type casts:

    struct DSA;
    
    typedef struct DSA tDSA;
    
    struct DSA
    {
    int p1,p2;
    tDSA **dynStructArray;
    };
    
    tDSA *parent;
    


    Now you allocate memory:

    parent=malloc(sizeof(tDSA));
    parent->dynStructArray=malloc(sizeof(*(parent->dynStructArray)));
    (parent->dynStructArray)[0]=malloc(sizeof(tDSA));
    


    Then you can access the only element of the array like this:

    (parent->dynStructArray)[0]->p1=5;
    

  • This term has a specific meaning in the context of the 'C' programming language - see your 'C' textbook for details!