简体   繁体   English

将结构传递给C中的泛型函数

[英]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". 我声明了一个名为“ Struct”的新结构。我有一个通用函数,该函数接受参数“ void * data”。

void Foo(void *data)

I pass an instance of "Struct" into the generic function. 我将“ Struct”的实例传递给泛型函数。

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. 我什至尝试首先将数据声明为struct类型,但出现错误。

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

I get "conversion to non-scalar type requested". 我得到“请求转换为非标量类型”。

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并使用struct以外的其他名称作为变量名:

Struct s;
Foo(&s);

Then, in Foo : 然后,在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. 另一方面,您不能像试图将非指针类型与void*进行相互转换,另一方面,将指针类型与void*也是有效的。

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;
}

You have to use -> operator when requesting structure member via pointer. 通过指针请求结构成员时,必须使用->运算符。

This should work: char *word = (char*) data->word; 这应该起作用: char *word = (char*) data->word;

You also have to pass the address of the structure to the function. 您还必须将结构的地址传递给函数。 Like this: Foo(&struct); 像这样: Foo(&struct); .

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. 现在,该函数必须知道您正在使用Struct (而不是其他东西)-可能是由于另一个参数,全局变量或某些其他原因。 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'... */
}

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. 如果您说Struct* myStruct = (Struct*) data ,那么世界一切都会好起来的。

You are mixing pointers and data. 您正在混合指针和数据。

Struct struct defines a data object Struct struct定义数据对象
void *data expects data to be a pointer. void *data期望data为指针。

Call Foo with a pointer to a Struct , and make other necessary changes 使用指向Struct的指针调用Foo并进行其他必要的更改

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;        
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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