C语言 C - 初始化结构数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4173518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C - initialize array of structs
提问by pmg
I am having a problem initializing an array of structs. I'm not sure if I am doing it right because I get "initialization from incompatible pointer type" & "assignment from incompatible pointer type". I added in the code where I get these warnings, and when I try to print the data from the struct I just get garbage such as @@###
我在初始化结构数组时遇到问题。我不确定我是否做得对,因为我得到了“从不兼容的指针类型初始化”和“从不兼容的指针类型赋值”。我在代码中添加了这些警告,当我尝试从结构中打印数据时,我只会得到垃圾,例如@@###
typedef struct
{
char* firstName;
char* lastName;
int day;
int month;
int year;
}student;
//initialize array
//初始化数组
student** students = malloc(sizeof(student));
int x;
for(x = 0; x < numStudents; x++)
{
//here I get: "assignment from incompatible pointer type"
students[x] = (struct student*)malloc(sizeof(student));
}
int arrayIndex = 0;
//add struct
//添加结构体
//create student struct
//here I get: "initialization from incompatible pointer type"
student* newStudent = {"john", "smith", 1, 12, 1983};
//add it to the array
students[arrayIndex] = newStudent;
arrayIndex++;
回答by pmg
This is incorrect:
这是不正确的:
student** students = malloc(sizeof(student));
You do not want a **. You want a *and enough space for how ever many students you need
你不想要一个**. 您需要*足够的空间来容纳您需要的学生数量
student *students = malloc(numStudents * sizeof *students); // or sizeof (student)
for (x = 0; x < numStudents; x++)
{
students[x].firstName = "John"; /* or malloc and strcpy */
students[x].lastName = "Smith"; /* or malloc and strcpy */
students[x].day = 1;
students[x].month = 12;
students[x].year = 1983;
}
If you still want to use the code in your "//add struct" section, you'll need to change the line:
如果您仍想使用“//add struct”部分中的代码,则需要更改该行:
student* newStudent = {"john", "smith", 1, 12, 1983};
to
到
student newStudent = {"john", "smith", 1, 12, 1983};
You were getting "initialization from incompatible pointer type" because you were attempting to initialize a pointer to studentwith an object of type student.
您正在“从不兼容的指针类型初始化”,因为您试图student用类型为的对象初始化指向的指针student。
回答by Tommy
Unrelated to the compiler warnings, but your initial malloc is wrong; you want:
与编译器警告无关,但您的初始 malloc 是错误的;你要:
malloc(sizeof(student *)* numStudents)
To allocate room for a total of 'numStudents' pointers to a student. The line:
为一个学生的总共 'numStudents' 指针分配空间。线路:
students[x] = (struct student*)malloc(sizeof(student));
Should be:
应该:
students[x] = (student*)malloc(sizeof(student));
There's no such thing as 'struct student'. You've declared an unnamed struct and typedef'd it to 'student'. Compare and contrast with:
没有“结构学生”这样的东西。您已经声明了一个未命名的结构并将其 typedef 为 'student'。比较和对比:
struct student
{
char* firstName;
char* lastName;
int day;
int month;
int year;
};
Which would create a 'struct student' type but require you (in C) to explicitly refer to struct student rather than merely student elsewhere. This rule is changed for C++, so your compiler may be a bit fuzzy about it.
这将创建一个“struct student”类型,但要求您(在 C 中)明确引用 struct student 而不仅仅是其他地方的 student 。此规则已针对 C++ 更改,因此您的编译器可能对此有点模糊。
As for:
至于:
student* newStudent = {"john", "smith", 1, 12, 1983};
That should be:
那应该是:
student newStudent = {"john", "smith", 1, 12, 1983};
As the curly brace syntax is a direct literal, not something somewhere else that you need to point to.
由于花括号语法是直接文字,而不是您需要指向的其他地方。
EDIT: on reflection, I think aaa may have taken more of an overview of this than I have. Is it possible that you're inadvertently using an extra level of pointer dereference everywhere? So you'd want:
编辑:经过反思,我认为 aaa 可能比我更了解这一点。您是否可能无意中在任何地方都使用了额外级别的指针取消引用?所以你会想要:
student* students = malloc(sizeof(student) * numStudents);
/* no need for this stuff: */
/*int x;
for(x = 0; x < numStudents; x++)
{
//here I get: "assignment from incompatible pointer type"
students[x] = (struct student*)malloc(sizeof(student));
}*/
int arrayIndex = 0;
And:
和:
student newStudent = {"john", "smith", 1, 12, 1983};
//add it to the array
students[arrayIndex] = newStudent;
arrayIndex++;
Subject the array not being used outside of scope of newStudent. Otherwise copying the pointers to strings is incorrect.
主题未在 newStudent 范围之外使用的数组。否则将指针复制到字符串是不正确的。
回答by Anycorn
student* students = malloc(sizeof(student)*numStudents);
int x;
for(x = 0; x < numStudents; x++)
{
student newStudent = {"john", "smith", 1, 12, 1983}; // string copy are wrong still
students[x] = newStudent;
}
回答by frarees
When initializing, shoudn't it be like this?
初始化的时候,不应该是这样吗?
student** students = (struct student**)malloc(sizeof(student*)*numStudents);
However, why pointer to a pointer? Just with a pointer to struct is enough I think.
但是,为什么要指向指针呢?我认为只要有一个指向 struct 的指针就足够了。

