简体   繁体   中英

c - void pointer to struct inside a struct

EDIT:

the exact error I get is : error: request for member 'q' in something not a structure or union

I corrected the typos I left in the code. It happened while formatting it for SO(camel case..).

context

Problem to set a void pointer to a struct.

My initial goal : I would like to point to a structure from a void pointer. pointMe.a will point to pointMe2 so that I can set pointMe2.q with an integer.

My final goal : being able to cast that void pointer to anything, while reusing my pointMe structure. Maybe I could point to a structure and soon after to a char or an integer. Polymorphism I think.

Failure

Apparently, in the 3) of the code below, q is not part of a struct or a union. Looking at the pointers addresses, i know I am close but not there yet.

My funky code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {

    void * a;

}pointMe;

typedef struct {

    int q;

}pointMe2;

int main(void)
{

    pointMe arrow;
    pointMe2 * flesh;

    flesh = malloc(sizeof(pointMe2));
    flesh->q = 4;

    printf("1)\n Value: %d Address: %p\n",flesh->q,flesh );

    arrow.a = flesh;
    printf("2)\n arrow.a's address: %p flesh's address: %p\n",arrow.a,flesh );

    printf("3)\n arrow.a's address: %p Value of q : %d\n",arrow.a, *(arrow.a)->q );

    free(flesh);
    return 0;
}
printf("3)\n arrow.a's address: %p Value of q : %p\n",arrow.a, *(arrow.a)->q );

The .a member is a void pointer. It is impossible to dereference a void pointer because there is no such thing as a void type. You have to cast the pointer to the correct type first:

printf("3)\n arrow.a's address: %p Value of q : %p\n",
       arrow.a,
       (arrow.a)->q);

Note also that the %p conversion specifier expects a void pointer to be passed. When printing the value of flesh you need to cast it to void * before passing it to printf :

printf("1)\n Value: %d Address: %p\n", flesh->q, flesh );

There are some errors [typos i think] in your code.

  1. pointme arrow; should be pointMe arrow; and so on. [Modified in below code]
  2. value of should be the value [ int type], so use %d with printf() .

Check the below code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {

    void * a;

}pointMe;

typedef struct {

    int q;

}pointMe2;

int main(void)
{

    pointMe arrow;            //corrected
    pointMe2 * flesh;         //corrected

    flesh = malloc(sizeof(pointMe2));
    flesh->q = 4;

    printf("1)\n Value: %d Address: %p\n",flesh->q,flesh );

    arrow.a = flesh;
    printf("2)\n arrow.a's address: %p flesh's address: %p\n",arrow.a,flesh );

    printf("3)\n arrow.a's address: %p Value of q : %d\n",arrow.a, ((pointMe2 *)(arrow.a))->q );  //corrected

    free(flesh);
    return 0;
}

使用*(arrow.a).q(arrow.a)->q

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM