gpt4 book ai didi

c - 赋值从指针生成整数,无需强制转换(C 语言。)

转载 作者:行者123 更新时间:2023-11-30 20:19:33 24 4
gpt4 key购买 nike

您好,我收到此错误:“赋值从指针生成整数而不进行强制转换”

我所做的是将不同类型数组中的许多值放入一个指针数组中。结果,我在将整数分配给我的 char 指针数组的代码中收到此警告。

这是我的代码:

int id[10] = {120, 121, 122, 123,124,125, 126, 127, 128,129 };
char *from[5] = {"value1", "value2", "value3", "value4", "value5" };
char *to[5] = {"value1", "value2", "value3", "value4", "value5" };
int date[5][3] = {{10,6,2018},{15,6,2018},{20,6,2018},{1,7,2018},{15,7,2018}};


char *flight[30];
int i = 2;


int counter = 0;
flight[0]="Flight ID:";
flight[1]= id[i]; //warning in this line.

flight[2]=" From:";
flight[3]=from[i];

flight[4]=" to:";
flight[5]=to[i];

flight[6]=" Depart on:";
flight[7]=date[i][0];//warning in this line.
flight[8]="/";
flight[9]=date[i][1]; //warning in this line.
flight[10]="/";
flight[11]=date[i][2]; //warning in this line.

最佳答案

您收到警告是因为您尝试将 int 值分配给 char *。这些类型彼此不兼容。

您应该将 flight 设为一个结构数组,而不是一个 char * 数组,该数组将保存其中的各种数据:

struct flight {
int id;
char *from;
char *to;
int departMon;
int departDay;
int departYear;
};

struct flight flight[30];

...

flight[counter].id = id[i];
flight[counter].from = from[i];
flight[counter].to = to[i];
flight[counter].departMon=date[i][0];
flight[counter].departDay=date[i][1];
flight[counter].departYear=date[i][2];
counter++;

关于c - 赋值从指针生成整数,无需强制转换(C 语言。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49762522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com