what is the cause of this error: Structure required on left side of . or .*?

橙三吉。 提交于 2019-12-11 08:14:28

问题


typedef struct tsk{
    int x;
    int n;
}TSK;

TSK trd[MAX];
 main(){
    while(life!=0){
 randomize();
     for(i=0;i<MAX;i++){
     trd[i].n=rand()%40+10;
         if(trd[i].n<10||trd[i].n>45)
         trd[i].n=rand()%40+10;
         trd[i].x=25;
     }
     for(m=0;m<MAX;m++){
     gotoxy(8,m);printf("%c%c",23,23);
     gotoxy(7,m);printf("%c",17);
     gotoxy(46,m);printf("%c%c",23,23);
     gotoxy(48,m);printf("%c",16);
     }
 for(j=0;j<MAX;j++){
 if(life==0)
     break;
 gotoxy(1,1);
 insline();
 gotoxy(trd[j].n,1);
 printf("%c",3);
 trd[j].x--;
 gotoxy(ply,25);
 printf("%c",1);
 gotoxy(1,26);
 delline();
 score++;
 life=hit(trd,life,ply);
 gotoxy(57,25);
 printf("SCORE: %0.0f  LIFE: %d",score,life);
 gotoxy(8,1);printf("%c%c",23,23);
    gotoxy(46,1);printf("%c%c",23,23);
    gotoxy(7,1);printf("%c",17);
    gotoxy(48,1);printf("%c",16);
    gotoxy(4,1);printf("%c",label[j]);
    delay(200);

        if(kbhit()){
            key=getch();
            key=toupper(key);
            if(key==27)
                life=0;
            if(key=='L')
            ply++;
                if(ply==46)
                ply--;
            else if(key=='K')
            ply--;
                if(ply==9)
                ply++;
            }
   }
  }
}

int hit(int trd[],int life, int ply){
int i;
    for(i=0;i<MAX;i++){
        if((trd[i].n==ply)&&(trd[i].x==0))
            life-=1;
        }
    return life;


  }

Can anyone help me how to correct this error: Structure required on left side of . or .*? when i run the program, it gives me this error and it is pointing to hit() function. i can't find what causes this error.pls help.thanks.


回答1:


This is a compile-time error, not a run-time error. It happens because you are attempting to access .x and .y on elements of an array of type int[]. Integers are not structs, they do not have fields x and y, so compiler tells you it's an error.

The most likely fix should be changing the type of trd array parameter of the hit function as follows:

int hit(TSK trd[],int life, int ply) {
    ...
}

Note that since you are using the function before declaring it, you should add a prototype at the top.




回答2:


trd is an int array. int has no member n or x.

Fix the function signature to int hit(TSK trd[], int life, int ply)




回答3:


In your hit function, the trd argument is an array of ints. Did you mean to make the argument of type TSK[] instead?



来源:https://stackoverflow.com/questions/14256739/what-is-the-cause-of-this-error-structure-required-on-left-side-of-or

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!