[英]C Initialize a char array of a struct array
I can't understand how I can initialize a char array in an array struct. 我不明白如何在数组结构中初始化char数组。 I have written this code:
我写了这段代码:
typedef struct tomo
{
char titolo[100];
char autore[100];
int anno_pubblicazione;
float prezzo;
} t_libro;
main(){
t_libro biblio[2];
biblio[0] = {"Guida al C", "Fabrizio Ciacchi", 2003, 45.2};
biblio[1] = {"Harry Potter e la Pietra Filosofale", "J.K.Rowling", 2003, 12.5};
}
but when i compile, it says me that before '{' an expression is expected. 但是当我编译时,它告诉我在'{'之前需要一个表达式。 How i can solve it?
我该如何解决? These char arrays give me a lot of problems...
这些char数组给我带来很多问题...
PS Ive tried also to use PS Ive也尝试使用
biblio[0].titolo = "Guida al C";
and in this manner for the other fields of struct, but also in this manner I have an error. 并以这种方式用于struct的其他字段,但是以这种方式我有一个错误。
biblio[0] = {"Guida al C", "Fabrizio Ciacchi", 2003, 45.2};
That is not an initialization. 那不是初始化。 That is a simple assignment.
这是一个简单的任务。 You can only use initialization syntax in an initialization.
您只能在初始化中使用初始化语法。 It looks like this:
看起来像这样:
t_libro biblio[] = {
{"Guida al C", "Fabrizio Ciacchi", 2003, 45.2},
{"Harry Potter e la Pietra Filosofale", "J.K.Rowling", 2003, 12.5}
};
Your attempt to write 您的写作尝试
biblio[0].titolo = "Guida al C";
fails because you cannot assign to character arrays. 失败,因为您无法分配给字符数组。 You must either initialize them, or use a function like
strcpy
. 您必须初始化它们,或使用类似
strcpy
的函数。
Your main
declaration is wrong too. 您的
main
声明也是错误的。 It should be 它应该是
int main(void)
There is an other solution. 还有另一种解决方案。
define your char array as a typedef and you could initialize your Array like this. 将char数组定义为typedef,就可以像这样初始化Array。
typedef char T_STRING[100] ;
typedef struct tomo
{
T_STRING titolo;
T_STRING autore;
int anno_pubblicazione;
float prezzo;
} t_libro;
t_libro biblio[] = {
{"Guida al C", "Fabrizio Ciacchi", 2003, 45.2},
{"Harry Potter e la Pietra Filosofale", "J.K.Rowling", 2003, 12.5}
};
On preprocessing you'r compiler will be able to initialize your array 在预处理时,编译器将能够初始化数组
From the respons of David Hefferman 大卫·赫弗曼的回应
Your attempt to write
您的写作尝试
biblio[0].titolo = "Guida al C";
biblio [0] .titolo =“ Guida al C”; fails because you cannot assign to character arrays.
失败,因为您无法分配给字符数组。 You must either initialize them, or use a function like strcpy.
您必须初始化它们,或使用类似strcpy的函数。
to explain better, you have to initialize each caractere in the right memory zone. 为了更好地解释,您必须在正确的存储区中初始化每个角色。
For example 例如
biblio[0].titolo = "Guida al C";
biblio [0] .titolo =“ Guida al C”; must be in memory to work good :
必须记在内存中才能正常工作:
biblio[0].titolo[0] = 'G';
biblio[0].titolo[1] = 'u';
biblio[0].titolo[2] = 'i';
biblio[0].titolo[3] = 'd';
biblio[0].titolo[4] = 'a';
biblio[0].titolo[5] = ' ';
biblio[0].titolo[6] = 'a';
biblio[0].titolo[7] = 'l';
biblio[0].titolo[8] = ' ';
biblio[0].titolo[9] = 'C';
biblio[0].titolo[0] = '\0'; // (don't forget to initialize the end of your string)
this is what strcpy (or strncpy) do very well. 这就是strcpy(或strncpy)的出色表现。
Ps : main () { } ps:main(){}
gcc will automatically put int main () { } by default gcc默认会自动将int main(){}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.