簡體   English   中英

錯誤分配使指針從整數開始而沒有強制轉換[默認啟用]

[英]Error Assignment makes pointer from integer without a cast [enabled by default]

我對C編程很陌生。 我在編譯程序時遇到此錯誤。 我試圖四處尋找解決方案,但無濟於事。 錯誤發生在num = rand()%20;

char* getParameter(char *name)
 {
   char *num;

   char *buffer;


   if(strcmp(name,"Trainingsprogramm")){
    srand (time(NULL));
     num = rand()%20;;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"Personnenummer")){
     srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"Tretzustand")){
     srand (time(NULL));
     srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"Tretleistung")){
     srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"Drehzahl")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"Geschwindigkeit")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"GefahreneDistanz")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"RealeKJoule")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"AktuellerPuls")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"MomentanerGang")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"RelaxBetrieb")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}else if(strcmp(name,"VerbrauchteKJoule")){srand (time(NULL));
      num = rand()%20;
    sprintf(buffer,"%d",num);
}

return buffer;

}

嘗試從num的聲明中刪除星形。

char *num; // <<< This is a pointer to char declaration
char num; // <<< This is a char

int num; // <<< But this is probably what you meant/wanted!

賦值使指針從整數...

 char *num;
 ...
 num = rand()%20;

該代碼嘗試將rand()的整數結果分配給字符指針num

根據到目前為止顯示的代碼,將num更改為整數,例如

 int num;

可以解決這個問題。

rand()返回一個int ,並且在其上應用%運算符還返回一個int您應將此結果保存為int ,而不是char*

int num;
char *buffer;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM