C struct to void * pointer

I have a structure defined as:

typedef struct {
   int type;
   void* info;
} Data;

and then I have several other structures that I want to assign void * using the following function:

Data* insert_data(int t, void* s)
{
    Data * d = (Data*)malloc(sizeof(Data));
    d->type = t;
    d->info = s;

    return d;
}

struct {
   ...
} Struct#;

then i just call

insert_data(1, variable_of_type_Struct#);

When I compile this, a warning is issued

warning: assignment from incompatible pointer type

I tried applying the variable in the insert to (void *), but did not work

insert_data(1, (void *) variable_of_type_Struct#);

How can I get rid of this warning?

thank

+3
source share
5 answers

Go to the address of the structure, not its copy (i.e. not passed by value):

insert_data(1, &variable_of_type_Struct);
+6
source

Pass a pointer to a struct object:

struct your_struct_type bla;

insert_data(1, &bla);
+5
source

, !

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

typedef struct {
   int type;
   void* info;
} Data;

typedef struct {
    int i;
    char a;
    float f;
    double d;  
}info;

Data* insert_data(int t, void* s)
{
    Data * d = (Data*)malloc(sizeof(Data));
    d->type = t;
    d->info = s;

    return d;
}

int main()
{
    info in; 
    Data * d;
    d = insert_data(10, &in);

    return 0;
}
+3

, :

struct {
   ...
} Struct#;

, , insert_data(1, &variable_of_type_Struct);

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

typedef struct {
    int type;
    void* info;
} Data;

Data* insert_data(int t, void* s);

Data variable_of_type_Struct;

Data* insert_data(int t, void* s)
{
    Data * d = (Data*)malloc(sizeof(Data));
    d->type = t;
    d->info = s;

    return d;
}

void test()
{
    insert_data(1, &variable_of_type_Struct);
}
+2

insert_data void*, Data.

insert_data(1, &variable_of_type_Struct#);

.

0

All Articles