简体   繁体   English

C - 初始化结构数组

[英]C - initialize array of structs

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 //添加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++;

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 student with an object of type student . 你都拿到“从兼容的指针类型初始化”因为你试图初始化一个指向student与类型的对象student

Unrelated to the compiler warnings, but your initial malloc is wrong; 与编译器警告无关,但您的初始malloc是错误的; you want: 你要:

malloc(sizeof(student *)* numStudents)

To allocate room for a total of 'numStudents' pointers to a student. 为学生分配总共'numStudents'指针的空间。 The line: 这条线:

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'. 你已经声明了一个未命名的结构,并将它定义为'student'。 Compare and contrast with: 比较和对比:

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. 这将创建一个“结构学生”类型,但要求你(在C中)明确地引用结构学生而不仅仅是其他地方的学生。 This rule is changed for C++, so your compiler may be a bit fuzzy about it. 对于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. 编辑:经过反思,我认为aaa可能比我有更多的概述。 Is it possible that you're inadvertently using an extra level of pointer dereference everywhere? 是否有可能无意中在任何地方使用额外的指针解除引用? So you'd want: 所以你想要:

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. 使数组不在newStudent范围之外使用。 Otherwise copying the pointers to strings is incorrect. 否则将指针复制到字符串是不正确的。

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;
}

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的指针就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM