繁体   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