hut123
hut123

Reputation: 455

Passing a struct into a generic function in C

I declare a new struct with the name of "Struct" I have a generic function that takes in an argument "void *data".

void Foo(void *data)

I pass an instance of "Struct" into the generic function.

Struct s;
Foo(&s);

I want to access one of the properties of the struct in the function.

void Foo(void *data) {
    char *word = (char*) data.word;
}

It's not allowed because it doesn't recognize data as a valid struct.

I even try to declare the data as the struct type first, and I get an error.

void Foo(void *data) {
    Struct s = (Struct) data;
    char *word = s.word;
}

I get "conversion to non-scalar type requested".

Upvotes: 0

Views: 4096

Answers (7)

mu is too short
mu is too short

Reputation: 434695

First of all, you should turn on your compiler's warning flags (all of them). Then you should pass a pointer to your Struct and use something other than struct as a variable name:

Struct s;
Foo(&s);

Then, in Foo:

void Foo(void *data) {
    Struct *s    = data;
    char   *word = s->word;
}

You can't convert non-pointer types to and from void* like you're trying to, converting pointer types to and from void* is, on the other hand, valid.

Upvotes: 3

psmears
psmears

Reputation: 28040

Firstly you need to call the function correctly:

Struct s;
Foo(&s);

Notice you're now passing a pointer to the structure.

Now, the function has to know that you're using a Struct (as opposed to something else) - perhaps because of another parameter, or a global variable, or some other reason. Then inside the function you can do:

void Foo(void *data) {
    Struct *structpointer = p; /* Note - no need for a cast here */


    /* (determine whether data does refer to a pointer then...) */
    char *word = structpointer->word;
    /* ... then use 'word'... */
}

Upvotes: 1

Athabaska Dick
Athabaska Dick

Reputation: 4281

You have to use -> operator when requesting structure member via pointer.

This should work: char *word = (char*) data->word;

You also have to pass the address of the structure to the function. Like this: Foo(&struct);.

Upvotes: 1

Chris Eberle
Chris Eberle

Reputation: 48785

Struct struct;
Foo((void*)&struct);

void Foo(void *data) {
    Struct *struct = (Struct*)data;
    char *word = struct->word;
}

or the more compact form:

Struct struct;
Foo((void*)&struct);

void Foo(void *data) {
    char *word = ((Struct*)data)->word;        
}

Upvotes: 0

joce
joce

Reputation: 9892

You need to pass a pointer to you struct and get a pointer to the struct inside the function:

Struct struct;
Foo(&struct);

void Foo(void *data) {
    Struct* struct = (Struct*) data;
    char *word = struct->word;
}

Upvotes: 2

pmg
pmg

Reputation: 108938

You are mixing pointers and data.

Struct struct defines a data object
void *data expects data to be a pointer.

Call Foo with a pointer to a Struct, and make other necessary changes

Upvotes: 0

Zach Rattner
Zach Rattner

Reputation: 21353

Data is pointer, so whatever you cast it to must also be a pointer. If you said Struct* myStruct = (Struct*) data, all would be well with the world.

Upvotes: 0

Related Questions