微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

格式’%s’期望类型为’char *’的参数

#include <stdio.h>
int main(void)
{
    int i,j,k;
    char st;
    printf("enter string\n");
    scanf("%s",st);
    printf("the entered string is %s\n",st);
}

编译上面的程序给了我一个警告:

warning: format '%s' expects argument of type 'char *',but argument 2 has type 'int' [-Wformat]
palindrom.c:8:1: warning: format '%s' expects argument of type 'char *',but argument 2 has type 'int' [-Wformat]

在这里做错了什么?

这是当我运行它会发生什么:

$./a.out
enter string
kiaaa
the entered string is (null)

编辑:

这是代码的另一个版本(使char * st; into char * st):

#include <stdio.h>
int main(void)
{
    int i,k;
    char *st;
    printf("enter string\n");
    scanf("%s",st);
}

但是,它在运行时的行为相同.

解决方法

char st是一个单一的字符.根据您的其余代码,您可能打算声明一个字符数组:
char st[80];

原文地址:https://www.jb51.cc/c/112937.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐