如何修复“需要标量的结构类型值”?

时间:2013-04-24 20:34:00

标签: c if-statement return-value

我编译时不断收到此错误,我无法弄清楚原因。代码块是:

static int
run(int nm, int nf)
{
    int i, j;
    int err, n = nm + nf;
    pthread_t thread[n];

    for (i = 0; i < n; i++) {
        err = pthread_create(&thread[i], NULL,
                  i < nm ? male : female, &shared_bathroom);
        if (err) {
            fprintf(stderr, "%s: %s: unable to create thread %d: %d\n",
                    progname, __func__, i, err);
            return EXIT_FAILURE;
        }
    }

    for (j = 0; j < n; j++) {
        if (thread[j]) (void) pthread_join(thread[j], NULL);
    }

    return EXIT_SUCCESS;
}

第二个for循环下if语句的行出现错误。具体来说,我认为它指的是(thread[j])

2 个答案:

答案 0 :(得分:6)

表达式thread[j]返回结构pthread_t的实例,您不能在布尔上下文中使用该实例(例如,在if语句的上下文中)。无论如何,你没有理由需要if声明。只是说:

for (j = 0; j < n; j++) {
    pthread_join(thread[j], NULL);
}

答案 1 :(得分:0)

如果条件不能直接使用该值。你可以做两件事 或者你可以像这样使用 if(thread [j]!= NULL_PTR)或者你可以在结构中使用check for explicit变量来检查值。