我对此运算符有一点不便,在编译代码时我将在下面显示给出以下错误:错误#2112:' - >'的左操作数有不相容的类型'人'不明白,因为它发生。
#include <stdio.h>
#include <stdlib.h>
typedef struct human{
int *works;
}person;
int main(){
int i,j,cont=1;
int quantity_person=8;
int quantity_works=4;
person *first;
first=(person *)malloc(sizeof(person)*quantity_person);
first->works=(int *)malloc(sizeof(int)*quantity_works);
for (i = 0; i < quantity_person; i++){
for (j = 0; j < quantity_works; j++){
first[i]->works[j]=cont;
cont++;
}
}
for (i = 0; i < quantity_person; i++){
for (j = 0; j < quantity_works; j++){
printf("%d ",first[i]->works[j]);
}
printf("\n");
}
return 0;
}
非常感谢
答案 0 :(得分:0)
你的行
first[i]->works[j]
应该是
first[i].works[j]
因为first [i]不是指针
答案 1 :(得分:0)
first[i]
不是指针,要访问其字段,您必须使用.
而不是->
。first
数组这是您的固定代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct human{
int *works;
}person;
int main(){
int i,j,cont=1;
int quantity_person=8;
int quantity_works=4;
person *first;
first=(person *)malloc(sizeof(person)*quantity_person);
for (i = 0; i < quantity_person; i++){
first[i].works=(int *)malloc(sizeof(int)*quantity_works);
}
for (i = 0; i < quantity_person; i++){
for (j = 0; j < quantity_works; j++){
first[i].works[j]=cont;
cont++;
}
}
for (i = 0; i < quantity_person; i++){
for (j = 0; j < quantity_works; j++){
printf("%d ",first[i].works[j]);
}
printf("\n");
}
return 0;
}