【问题标题】:Warning with sscanf formatsscanf 格式的警告
【发布时间】:2015-05-06 16:40:24
【问题描述】:

我在尝试编写只允许数字字符串的程序时收到以下警告:

warning: format ‘%s’ expects argument of type char *’, but argument 3 has type ‘char (*)[100]’

为什么我会收到此警告,这是什么意思/后果?运行代码没有任何问题。

程序:

char check[100];
char line[100];
int i;
int true = 0;
int main
{
fgets(line, sizeof(line), stdin);
sscanf(line, "%s" , &check);
for(i=0; i<3; i++)
    {
    if(isdigit(check[i]) == 0)
        {
        true++;
        }
    else
        continue;
    }

if(true>0)
    printf("Not a number.\n");
else
    printf("Is a number.\n");
return(0);
}

【问题讨论】:

  • 标题具有误导性;警告与isdigit 无关。
  • sscanf(lne, "%s", &amp;check); 中取出&amp; 此外,在代码行之间留一些空格,遵循样式指南等。您的int main 应该是int main(void)

标签: c string fgets scanf


【解决方案1】:

编译器抱怨在以下行中使用了&amp;check

sscanf(line, "%s" , &check);

预期的参数只是check

sscanf(line, "%s" , check);

【讨论】:

  • 啊,我从没想过它可能是 sscanf 函数。感谢您的宝贵时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多