我在C中有以下代码:

int main(int argc, char **argv) {

    ...

    redisContext *c;
    redisReply *reply;

    ...

    outer_function(...)

    return 0;

}

我想在Redis中使用outer_function变量。

我试图为此在struct之前添加一个main(...):
typedef struct {
    redisReply reply;
    redisContext c;
} redisStuff;

并在main中:
redisContext *c;
redisReply *reply;

redisStuff rs = { reply, c };

...

outer_function((u_char*)&rs, ...)

最后在我的outer_function中:
void outer_function(u_char *args, ...) {
    redisStuff *rs = (redisStuff *) args;
    reply = redisCommand(c, "MY-REDIS-COMMAND");
    ...
    return;
}

但是它失败了:
warning: incompatible pointer to integer conversion initializing 'int' with an expression of type 'redisReply *' (aka 'struct redisReply *')

最佳答案

您的结构体需要值,并且您正在传递一个指针,因此编译器无法将指针分配为redisContext

typedef struct {
    redisReply reply;  // <- expects value
    redisContext c;    // <- expects value
} redisStuff;

...

redisContext *c;
redisReply *reply;

redisStuff rs = { reply, c };  // <- reply and c are pointers

关于c - 将变量传递给C中的其他函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57688673/

10-16 12:38