【问题标题】:makecontext function pointer [error: invalid use of void expression]makecontext 函数指针 [错误:无效表达式的使用]
【发布时间】:2015-05-23 23:58:17
【问题描述】:

我有以下代码(它用于一个类的线程库):

int tcb_context_create(TCB *thread, void (*start)(void*), void *arg) {
    if (!makecontext( &(thread->context), (void (*) (void)) start, 1, arg)) {
        errno = EAGAIN;
        return -1;
    }

    return 0;
}

但是我的编译器一直给出'错误:无效使用无效表达式'我到处搜索并且找不到使用makecontext函数的正确方法。

更正:

int tcb_context_create(TCB *thread, void (*start)(void*), void *arg) {
    makecontext( &(thread->context), (void (*) (void)) start, 1, arg);

    return 0;
}

【问题讨论】:

    标签: c multithreading pointers function-pointers void-pointers


    【解决方案1】:

    makecontext 的返回类型是void。在void 上使用! 是不正确的。

    此外,您在 if 块之外没有 return 语句。由于函数的返回类型为int,因此需要添加适当的return 语句。否则,您将遇到未定义的行为。

    【讨论】:

    • 不好意思最后忘记复制return 0,但是问题出在if,我放不进去,不知道怎么没看到,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2019-11-04
    相关资源
    最近更新 更多