c - 如何修复两个警告“警告:格式指定类型'char *'但参数的类型为'char (*)[256]' [-Wformat]”
问题描述
这是我已经尝试修复它很长一段时间的代码..它仍然拒绝工作我的朋友联系我,看看我是否可以帮助她使用她的代码(如下)如果有人能告诉我它有什么问题,非常感谢.
#include <stdio.h>
#include <math.h>
int main() {
//declarations
double originlat,originlong,deslat,deslong;
double rad1,rad2,rad3,rad4;
char originplace[256], desplace[256];
//input
printf("Enter origin latitude, longitude, and place.\n");
scanf("%lf %lf %[^\n]s",&originlat,&originlong,&originplace);
printf("You entered latitude %0.2lf, longitude %0.2lf, place \"%s\"\n\n", originlat,originlong,originplace);
printf("Enter destination latitude, longitude, and place.\n");
scanf("%lf %lf %[^\n]s",&deslat,&deslong,&desplace);
printf("You entered latitude %0.2lf, longitude %0.2lf, place \"%s\"\n\n",deslat,deslong,desplace);
printf("Origin: %s\n",originplace);
printf("\t%0.2lf degrees is %0.4lf radians (latitude)\n",originlat,rad1);
//process
rad1 = originlat/(180/M_PI);
//output
printf("\t%0.2lf degrees is %0.4lf radians (latitude)\n\n",originlong,rad2);
printf("Destination: %s\n",desplace);
printf("\t%0.2lf degrees is radians (latitude)\n",deslat);
printf("\t%0.2lf degrees is radians (latitude)\n\n",deslong);
printf("The distance from %s to %s is miles.",originplace,desplace);
///:END ToDo
return 0;
}
解决方案
对于%[^\n]s
,scanf
应该传递一个指向 a 的指针char
,它是可以写入读取字符char
的数组中的第一个。对于该转换,代码具有参数。是一个 256 的数组,所以它的地址 ,是一个指向 256 的数组的指针。char
scanf
&originplace
originplace
char
&originplace
char
与其传递originplace
,不如传递其第一个元素的地址 ,&originplace[0]
。
为方便起见,您可以简单地编写originplace
. 根据 C 的规则,这将自动转换为指向其第一个元素的指针。(无论何时在表达式中使用数组,它都会自动转换为指向其第一个元素的指针,除非它是 的操作sizeof
数、一元的操作数&
或用于初始化数组的字符串文字。)
另请注意rad1
和rad2
在初始化之前打印。
推荐阅读
- reactjs - React Stateless Components:与它们的输出和外观交互
- pywinauto - 如何使用 Pywinauto 在 Windows 10 中访问设备管理器
- android - MediaCodec 原始 H264 解码问题 - 超时并且 decoder.dequeueOutputBuffer 始终返回 -1
- r - 如何在数据框中删除具有相似名称的多个列
- crash - 如何分析受 vmprotect 保护的 EXE/DLL 文件的故障转储
- html - 删除标题和 div 之间的空格
- python - Django:如何在视图中获取用户坐标(纬度,经度)?
- google-drive-api - Google colab 和 google drive:将文件从 colab 复制到 Google Drive
- swift - 代码没有完成功能,它正在中途结束执行
- r - 如果两列匹配,则返回 row(i)