简体   繁体   English

文件范围和下标值的可变修改数组既不是数组也不是指针

[英]Variably modified array at file scope and subscripted value is neither array nor pointer

I have the following code for calculating n-queen puzzle using pthreads. 我有以下代码用于使用pthreads计算n-queen拼图。 But when I try to compile that code I get the following error message: 但是当我尝试编译该代码时,我收到以下错误消息:

wikithread.c:7:5: error: variably modified 'hist' at file scope wikithread.c:7:5:错误:在文件范围内修改了'hist'

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>

int NTHREADS, SIZE; 
int hist[SIZE];
int count = 0;

int solve(int col, int tid)
{
    int start = tid * SIZE/NTHREADS;
    int end = (tid+1) * (SIZE/NTHREADS) - 1;
    int i, j;
    if (col == SIZE) 
    {
        count++;
    }

    #define attack(i, j) (hist[j] == i || abs(hist[j] - i) == col - j)
    for (i = start; i <= end; i++) {
        for (j = 0; j < col && !attack(i, j); j++);
        if (j < col) continue;

        hist[col] = i;
        solve(col + 1, tid);
    }

    return count;
}

void *worker(void *arg)
{
    int tid = (int)arg;
    solve(0, tid);
}

int main(int argc, char* argv[])
{
    pthread_t* threads;
    int rc, i;

    // checking whether user has provided the needed arguments
    if(argc != 3)
    {
        printf("Usage: %s <number_of_queens> <number_of_threads>\n", argv[0]);
        exit(1);
    }


    // passing the provided arguments to the SIZE and NTHREADS 
    // variable, initializing matrices, and allocating space 
    // for the threads
    SIZE = atoi(argv[1]);
    NTHREADS = atoi(argv[2]);
    threads = (pthread_t*)malloc(NTHREADS * sizeof(pthread_t));

    // declaring the needed variables for calculating the running time
    struct timespec begin, end;
    double time_spent;

    // starting the run time
    clock_gettime(CLOCK_MONOTONIC, &begin);

    for(i = 0; i < NTHREADS; i++) {
        rc = pthread_create(&threads[i], NULL, worker, (void *)i);
        assert(rc == 0); // checking whether thread creating was successfull
    }

    for(i = 0; i < NTHREADS; i++) {
        rc = pthread_join(threads[i], NULL);
        assert(rc == 0); // checking whether thread join was successfull
    }

    // ending the run time
    clock_gettime(CLOCK_MONOTONIC, &end);

    // calculating time spent during the calculation and printing it
    time_spent = end.tv_sec - begin.tv_sec;
    time_spent += (end.tv_nsec - begin.tv_nsec) / 1000000000.0;
    printf("Elapsed time: %.2lf seconds.\n", time_spent);

    printf("\nNumber of solutions: %d\n", count);

    return 0;
}

If I change the upper part, and dynamically allocate memory for the array, I get the following error: 如果我更改上部,并为数组动态分配内存,我会收到以下错误:

int NTHREADS, SIZE; 
int *hist;
hist = (int*)malloc(SIZE * sizeof(int));

Then I get the following errors: 然后我收到以下错误:

wikithread.c:8:1: warning: data definition has no type or storage class [enabled by default] wikithread.c:8:1: error: conflicting types for 'hist' wikithread.c:7:6: note: previous declaration of 'hist' was here wikithread.c:8:1: error: initializer element is not constant wikithread.c: In function 'solve': wikithread.c:23:27: error: subscripted value is neither array nor pointer nor vector wikithread.c:23:27: error: subscripted value is neither array nor pointer nor vector wikithread.c:26:7: error: subscripted value is neither array nor pointer nor vector wikithread.c:8:1:警告:数据定义没有类型或存储类[默认启用] wikithread.c:8:1:错误:'hist'的冲突类型wikithread.c:7:6:注意:上一个'hist'的声明在这里wikithread.c:8:1:错误:初始化元素不是常量wikithread.c:在函数'solve'中:wikithread.c:23:27:错误:下标值既不是数组也不是指针也不是vector wikithread.c:23:27:错误:下标值既不是数组也不是指针也不是向量wikithread.c:26:7:错误:下标值既不是数组也不是指针也不是向量

Anyone, can help me solve the problem? 任何人,可以帮我解决问题吗?

You use SIZE to initialize an array without having defined it-- 你使用SIZE初始化一个数组而没有定义它 -

int NTHREADS, SIZE; 
int hist[SIZE];

Undoubtedly this is causing problems. 毫无疑问,这会造成问题。

As for your second error, you have this at file scope: 至于你的第二个错误,你有这个在文件范围:

hist = (int*)malloc(SIZE * sizeof(int));

but statements are not allowed outside a function body, just declarations. 但是声明不允许在函数体外部,只是声明。

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

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