鉴于以下结构,我想知道我写的指针的相等表达式是否正确
struct data{
char fname[30];
char lname[30];
float grade;
int trials;
int year;
};
struct data base[N];
fgets(base[i].lname,30,stdin)
base[i].fname[length-1] == '\n'
scanf(" %f",&(base[i].grade));
if(base[i].grade<0 || base[i].grade>10)
指针的等同表达
struct data{
char *fname;
char *lname;
float grade;
int trials;
int year;
};
struct data *base=malloc(N*sizeof(struct data));
for(i=0;i<N;i++){
(base+i)->fname = malloc(30*sizeof(char));
(base+i)->lname = malloc(30*sizeof(char));
}
fgets((base+i)->lname,30,stdin)
(base+i)->(fname+length-1) == '\n'
scanf(" %f",&(base+i)->grade));
if((base+i)->grade<0 || (base+i)->grade>10)
答案 0 :(得分:1)
base[i].fname[length-1]
相当于*(((base+i)->fname)+length-1)
另外,正如编译器告诉你的那样,你不能把malloc放在初始化器中 - 初始化器必须是编译时常量。