将接收到的参数传递给回调函数

问题描述 投票:0回答:2

我正在C中从事Gtk项目。

从main.c中,我以整数地址作为参数调用function1

在那个function1中,我可以访问该第一个值,但是在那个function1的末尾(内部),我调用另一个function2(这是单击的回调函数)事件),并将我从function1参数获得的地址传递给它。

但是在function2

中,地址已更改,绝对无法弄清为什么 ...

我的项目看起来像这样:

[main.c]

int main(...) {

    int a = 50;
    function1(&a);

}

[function1.c]

void function1(int* nb) {
    ...
    g_signal_connect(G_OBJECT(button),"clicked", G_CALLBACK(function2), &nb);
    // I know that the 4th arg expects void*, but even though I give the address of that _nb_ parameter, still can't get that 50 in function2
}

[function2.c]

void function2(void* nb) {
    ...
    printf("should got 50 : %d ", *(int*)nb);
    // shows random 8 digits number like 60035152
}

编辑:忘记提及每个函数都在单独的文件中,只要执行包含并给出原型,我就不知道这是否重要...

谢谢你...

我正在C语言中的Gtk项目中工作。从main.c中,我将一个以int地址作为参数的function1调用。在该函数1中,我可以访问该第一个值,但随后可以访问该值的末尾(内部)...

c function callback arguments gtk
2个回答
0
投票

您有两个问题:


0
投票

您的代码中的问题是:-

© www.soinside.com 2019 - 2024. All rights reserved.