【问题标题】:How do I fix the two warnings "warning: format specifies type 'char *' but the argument has type 'char (*)[256]' [-Wformat]" [closed]如何修复两个警告“警告:格式指定类型'char *'但参数的类型为'char(*)[256]'[-Wformat]”[关闭]
【发布时间】:2020-05-29 08:39:39
【问题描述】:

这是我试图修复它很长一段时间的代码.. 它仍然拒绝工作我的朋友联系我,看看我是否可以帮助她解决她的代码(如下)如果有人能告诉我它有什么问题非常感谢。

#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;
}

【问题讨论】:

  • &amp;desplace -> desplace。投票结束这是一个简单的错字。
  • @Lundin:这是一个想法,而不是错字。
  • @EricPostpischil 不管怎样,未来的读者可能不感兴趣。
  • @EricPostpischil thinko ????

标签: c arrays pointers scanf c-strings


【解决方案1】:

对于%[^\n]sscanf 应该传递一个指向char 的指针,charchar 数组中的第一个scanf 读取的字符可以写入其中。对于该转换,代码具有参数&amp;originplaceoriginplace 是一个由 256 个 char 组成的数组,所以它的地址 &amp;originplace 是一个指向 256 个数组的指针 char

不传递originplace,而是传递其第一个元素的地址&amp;originplace[0]

为方便起见,您可以简单地写originplace。根据 C 的规则,这将自动转换为指向其第一个元素的指针。 (无论何时在表达式中使用数组,它都会自动转换为指向其第一个元素的指针,除非它是 sizeof 的操作数,是一元 &amp; 的操作数,或者是用于初始化的字符串文字一个数组。)

还要注意rad1rad2 在初始化之前就打印出来了。

【讨论】:

    【解决方案2】:

    在0this语句中

    scanf("%lf %lf %[^\n]s",&originlat,&originlong,&originplace);
                                                   ^^^^^^^^^^^^
    

    删除符号“&”。

    否则,表达式&amp;originplace 的类型为char( * )[256],而不是预期的char * 类型的表达式。

    格式也一样

    "%lf %lf %[^\n]s"
                  ^^^ 
    

    删除最后一个符号's'。

    【讨论】:

      【解决方案3】:

      删除&amp;(固定类型),s 并使用宽度,检查返回

      // scanf("%lf %lf %[^\n]s",&originlat,&originlong,&originplace);
      //                 ^    ^                         ^
      
      if (scanf("%lf %lf %255[^\n]", &originlat, &originlong, originplace) != 3) {
        puts("Fail");
      }
      

      【讨论】:

        【解决方案4】:

        您需要删除 originplacedesplace 数组前面的 &amp; 和号运算符,这些运算符作为最后一个参数传递给各自的 scanf() 函数,因为在没有特定元素索引 [i] 的情况下使用数组, 确实已经衰减为指向数组第一个元素的指针。


        变化:

        scanf("%lf %lf %[^\n]s",&originlat,&originlong,&originplace);
                                                       ____________
        

        scanf("%lf %lf %[^\n]",&originlat,&originlong,originplace);
                                                      ___________
        

        scanf("%lf %lf %[^\n]s",&deslat,&deslong,&desplace);
                                                 _________
        

        scanf("%lf %lf %[^\n]",&deslat,&deslong,desplace);
                                                ________
        

        它应该被正确编译。

        我还删除了%[^\n]s 处的s,因为%[ 格式说明符与%s 格式说明符完全不同。

        【讨论】:

          【解决方案5】:

          数组衰减为指针但是:

          char array[20];

          array - 衰减到 char * 类型的指针 &amp;array[0] - 衰减到 char * 类型的指针 &amp;array - 衰减为 char (*)[20] 类型的指针

          指针引用内存中的相同位置,但类型不同。

          【讨论】:

            猜你喜欢
            • 2021-12-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-05-10
            • 2018-11-03
            • 1970-01-01
            • 2020-08-02
            • 2012-03-10
            相关资源
            最近更新 更多