'void' 类型的操作数,其中需要算术或指针类型 - C
operand of type 'void' where arithmetic or pointer type is required - C
我正在使用这个方法
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;
}
我想获取colm数组中的值到主程序中。所以我使用下面的行来这样做。基本上 colm 数组存储的是 arr1 的列索引,根据数独规则这是无效的。 (不重要)。
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];
当我尝试获取 cols1 to the variable "value". What am i doing wrong ? any suggestions? Full code here
中的值时出现错误 "operand of type 'void' where arithmetic or pointer type is required"
在 (int)cols[1]
中,(int)
部分的优先级低于 [1]
部分,因此编译器首先尝试计算 cols[1]
。
然后编译器无法计算cols[1]
,因为void*
没有指向已知大小的项目。如果编译器不知道 cols[0]
有多大,那么它怎么知道 cols[1]
在哪里?
我不确定你想做什么,但你可能想要的是int value = ((int*)cols)[1];
@Mike Nakis 已经提供了一个很好的答案,我会修复你的一个语法错误。
当您将 colm 声明为整数列矩阵的指针时,您做错了。 malloc
定义为:
void* malloc( size_t size );
你只分配了 9 个连续的字节,如果你想要 9 个连续的整数字节,你必须这样做:
int *colm = malloc(sizeof(int)*9);
或:
int *colm = calloc(9, sizeof(int));
在我看来,后者更可取。但是要么做同样的事情,除了 calloc 还将分配的存储中的所有字节初始化为零。
我正在使用这个方法
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;
}
我想获取colm数组中的值到主程序中。所以我使用下面的行来这样做。基本上 colm 数组存储的是 arr1 的列索引,根据数独规则这是无效的。 (不重要)。
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];
当我尝试获取 cols1 to the variable "value". What am i doing wrong ? any suggestions? Full code here
中的值时出现错误 "operand of type 'void' where arithmetic or pointer type is required"在 (int)cols[1]
中,(int)
部分的优先级低于 [1]
部分,因此编译器首先尝试计算 cols[1]
。
然后编译器无法计算cols[1]
,因为void*
没有指向已知大小的项目。如果编译器不知道 cols[0]
有多大,那么它怎么知道 cols[1]
在哪里?
我不确定你想做什么,但你可能想要的是int value = ((int*)cols)[1];
@Mike Nakis 已经提供了一个很好的答案,我会修复你的一个语法错误。
当您将 colm 声明为整数列矩阵的指针时,您做错了。 malloc
定义为:
void* malloc( size_t size );
你只分配了 9 个连续的字节,如果你想要 9 个连续的整数字节,你必须这样做:
int *colm = malloc(sizeof(int)*9);
或:
int *colm = calloc(9, sizeof(int));
在我看来,后者更可取。但是要么做同样的事情,除了 calloc 还将分配的存储中的所有字节初始化为零。