Joel Shevin
Joel Shevin

Reputation: 103

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 the values in the colm array to the main program. So im using the below lines to do so. basically what the colm array stores are the column indexes of the arr1 which is not valid according to sudoku rules. (not important).

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 of type 'void' where arithmetic or pointer type is required" when i try to get the value in cols1 to the variable "value". What am i doing wrong ? any suggestions? Full code here

Upvotes: 4

Views: 12248

Answers (2)

ranu
ranu

Reputation: 652

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

When you are declaring colm as a pointer of integers column matrix you are doing it wrong. malloc is defined as:

void* malloc( size_t size );

You only allocates 9 consecutive bytes, if you want 9 consecutive bytes of ints, you would have to do either:

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

or:

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

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

Upvotes: 1

Mike Nakis
Mike Nakis

Reputation: 62045

In (int)cols[1] the (int) part has lower precedence than the [1] part, so the compiler tries to evaluate cols[1] first.

Then the compiler cannot calculate cols[1] because void* does not point to an item of a known size. If the compiler does not know how large cols[0] is, then how can it figure out where cols[1] is?

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

Upvotes: 10

Related Questions