C 注:预计为';字符*';但参数的类型是';int*';
这是代码,这将输出正确的答案C 注:预计为';字符*';但参数的类型是';int*';,c,struct,types,C,Struct,Types,这是代码,这将输出正确的答案 #定义最大尺寸100 类型定义结构{ 国际标准书号[13]; }书; int main(){ 图书目录; strcpy(BookList.ISBN,“9780133432398”); printf(“图书的ISBN:%s\n”,BookList.ISBN); 返回0; } 此外,gcc表示有一个警告: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'i
#定义最大尺寸100
类型定义结构{
国际标准书号[13];
}书;
int main(){
图书目录;
strcpy(BookList.ISBN,“9780133432398”);
printf(“图书的ISBN:%s\n”,BookList.ISBN);
返回0;
}
此外,gcc表示有一个警告:
warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int *' [-Wformat=]
因此,我更改如下:BookList.ISBN=“9780133432398”代码>
但gcc会输出以下错误:
error: assignment to expression with array type
只是无法解决这个问题…使用strcpy()
不是问题所在(而是正确的做法),检查类型,这正是编译器所抱怨的
ISBN
变量在int
s数组中,当它真的应该是char
s数组时
也就是说,要将char
数组限定为字符串,它需要以空字符结尾。要保存该空字符,您需要在实际内容的上方为另一个字符
留出一个空格
因此,要保存13个字符的输入,数组的长度至少应为14
你需要改变
typedef struct {
int ISBN[13];
}Book;
到
虽然你可以像其他人建议的那样把你的ISBN改成一个
char[]
,但我发现用一个字符串来保存一个数字很奇怪。相反,我会选择一种无符号整数类型,它可以容纳足够多的数字来容纳13位的ISBN
#include <stdint.h>
#include <inttypes.h>
typedef struct {
uint_least64_t ISBN;
}Book;
int main() {
Book BookList = {.ISBN=9780133432398};
printf("ISBN of the book: %" PRIuLEAST64 "\n", BookList.ISBN);
return 0;
}
#包括
#包括
类型定义结构{
至少64个ISBN;
}书;
int main(){
图书目录={.ISBN=9780133432398};
printf(“本书的ISBN:%“PRIuLEAST64”\n”,BookList.ISBN);
返回0;
}
changeintisbn[13]代码>至字符ISBN[14]代码>取而代之。@Blaze是什么阻止你把它变成答案?“只要稍加解释,它甚至可能是一个可上溯的问题。”Yunnosch通常当我试图回答这样的问题时,它会在我写答案的中途以副本的形式关闭。
#include <stdint.h>
#include <inttypes.h>
typedef struct {
uint_least64_t ISBN;
}Book;
int main() {
Book BookList = {.ISBN=9780133432398};
printf("ISBN of the book: %" PRIuLEAST64 "\n", BookList.ISBN);
return 0;
}