我希望能够在c中打印出charcter的值,如下所示:
fprintf("%c", alphabet[val]);
,其中字母表初始化为
for(i = 0; i < 26; i++){
alphabet[i] = i + 65;
}
但是,此行会出现以下错误:
encode.c: In function ‘main’:
encode.c:76:13: warning: passing argument 1 of ‘fprintf’ from incompatible pointer type [-Wincompatible-pointer-types]
fprintf("%c", alphabet[val]);
^
In file included from encode.c:1:0:
/usr/include/stdio.h:356:12: note: expected ‘FILE * restrict {aka struct _IO_FILE * restrict}’ but argument is of type ‘char *’
extern int fprintf (FILE *__restrict __stream,
^
encode.c:76:19: warning: passing argument 2 of ‘fprintf’ makes pointer from integer without a cast [-Wint-conversion]
fprintf("%c", alphabet[val]);
^
In file included from encode.c:1:0:
/usr/include/stdio.h:356:12: note: expected ‘const char * restrict’ but argument is of type ‘char’
extern int fprintf (FILE *__restrict __stream,
^
encode.c:76:5: warning: format not a string literal and no format arguments [-Wformat-security]
fprintf("%c", alphabet[val]);
^
如何打印出这样的角色?
答案 0 :(得分:5)
仔细检查您的密码。对于声明
fprintf("%c", alphabet[val]);
文件指针在哪里?它不见了。您必须提供要查看输出的文件指针,如
fprintf(fp, "%c", alphabet[val]);
其中,fp
是文件指针(类型FILE *
),通常由fopen()
返回,请查看man page以获取更多相关详细信息。
如果您希望打印件到达stdout
,即标准输出,请使用printf()
。