Operand of type 'void' where arithmetic or pointer type is required - C

Im using this method

void * col_check(void * params) {
parameters * data = (parameters *) params;
int startRow = data->row;
int startCol = data->col;
int *colm = malloc(9);
for (int i = startCol; i < 9; ++i) {
    int col[10] = {0};
    for (int j = startRow; j < 9; ++j) {
        int val = data->arr1[j][i];
        if (col[val] != 0) {
            colm[i]=i;
        }
        else{
            col[val] = 1;
        }
    }
}
return colm;    
}

      

I want to get values ​​in colm array in main program. So I am using the following lines for this. basically what is stored in the colm array is the indexes of the arr1 columns which are not valid according to sudoku rules. (no matter).

parameters * param10 = (parameters *) malloc(sizeof(parameters));
    param10->row = 0;
    param10->col = 0;
    param10->arr1 = arr1;

void * cols;

pthread_create(&thread_10, NULL, col_check, (void *) param10);
pthread_join(thread_10, &cols);

printf("Calculating column validity please wait.\n");
    sleep(mdelay);

int c;
int value= (int)cols[1];

      

I get the error "operand is of type 'void' where arithmetic or pointer type is required" when I try to get the value in columns 1 to the variable "value". What am I doing wrong? any suggestions? Full code here

+3


source to share


2 answers


The (int)cols[1]

part (int)

has a lower priority than the part [1]

, so the compiler tries to evaluate first cols[1]

.

Then the compiler cannot compute cols[1]

because it void*

does not point to an element with a known size. If the compiler doesn't know how big cols[0]

, how do you determine where cols[1]

?



I'm not sure what you are trying to do, but what you probably want is int value = ((int*)cols)[1];

+3


source


@Mike Nakis has already provided a good answer, I'll fix one of your syntax errors.

When you declare colm as a column chain pointer, you are doing it wrong. malloc

defined as:

void* malloc( size_t size );

You only allocate 9 consecutive bytes, if you need 9 consecutive int bytes, you would need to either:



int *colm = malloc(sizeof(int)*9);

or

int *colm = calloc(9, sizeof(int));

The latter is preferable in my opinion. But either do the same, except that calloc also initializes all bytes in the allocated storage to zero.

+1


source







All Articles