簡體   English   中英

警告:賦值使 integer 從沒有強制轉換的指針 [-Wint-conversion] 在 C

[英]warning: assignment makes integer from pointer without a cast [-Wint-conversion] in C

這不是我的整個程序,但我認為這些是您將需要的唯一部分,因為這些是唯一使用的功能。

typedef struct
{
char name[64];
int balance;
int gain;
}
Player;

Player Players[10];
FILE *fp;

Player GetPlayerData(const char* name, Player p){
  char First[100];
  int i = 0;
  for (;;)
  {
      if (strcmp(name, p[i].name) == 0)
      {
        return p[i];
      }
     /*else{
        printf("The name you inputed is not on the list.\n"
        "Here are the names that are: \n");
        while(1){
          fscanf(fp, "%s", &First);
          printf("\n%s %d", First);
          if(feof(fp) == 1){
            break;
          }
        }
      }
      i++;*/
  }
}

void TopBal(){
  char name[64];
  int Add[100];

  printf("Enter your name: ");
  scanf("%s", &name);
  Player p = GetPlayerData(name, Players);
  printf("How much money would you like to add: ");
  scanf("%d", &Add);
  p.balance = p.balance + Add;
  printf("Your balance is now %d", p.balance);
  //PushPlayerData(FILE_NAME, name);
}

這是警告。 ./Project.c: In function 'TopBal': ./Project.c:108:15: warning: assignment makes integer from pointer without a cast [-Wint-conversion] (p).balance = (p).balance +添加;

Add是一個數組,它在表達式中被轉換為一個指針(不包括一些例外,如sizeof的操作數)。

你沒有使用數組Add的多元素特性,所以它應該是簡單的變量int Add; , 不是int Add[100]; .

您還應該在scanf("%s", &name);中刪除& 因為它會導致類型不匹配(傳遞了指向數組char(*)[64]的指針,而需要指向字符char*的指針),這會調用未定義的行為 再次注意,表達式中的大多數 arrays 都轉換為指針。

p.balance // This is int
 = p.balance // again int
 + Add; // but this is int array / pointer

您可能打算尊重 Add。 例如

 p.balance = p.balance + Add[0];

暫無
暫無

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

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