简体   繁体   English

传递struct数组以使用malloc运行

[英]Pass struct array to function with malloc

I'am writing a program where I need to add students to a array of structs. 我正在编写一个程序,我需要将学生添加到一系列结构中。

Well, at the beginning I have the following struct array: 好吧,一开始我有以下结构数组:

struct student *students[4];

After the declaration I add students like this to the array (just to have examples ...): 在声明之后,我将这样的学生添加到数组中(只是为了有例子......):

  students[0] = malloc(sizeof(struct student));
  students[0]->firstname = "Max";
  students[0]->secondname = "Taler";
  students[0]->number = 123456l;

... ...

Now I have to write a additional function, where I can pass the array as a parameter and add a new student. 现在我必须编写一个附加函数,我可以将数组作为参数传递并添加一个新学生。

So my function looks like this: 所以我的函数看起来像这样:

void add_student(struct student *students[],char *fristname, char *secondname, long number)
{
    int i=0;
    int new_position=0;
    int return_value = 0;
    // Search for the first available position for a new student 
    for(i=0; i<sizeof(students); i++)
    {
        if(students[i]==NULL)
        {
            new_position=i;
            return_value = 1;
            break;
        }
    }

    struct student *new_student = malloc(sizeof(struct student));
    students[new_position] = new_student;

    students[new_position]->fristname = fristname;
    students[new_position]->secondname = secondname;
    students[new_position]->number = number;

   return return_value; 
}

I call the function with this code: 我用这段代码调用函数:

add_student(students, "Anni", "Karls", 123232);

But now my issue : In my "add_student" function I get an array that has a strange structur: it includes itself as the first element and every other element is shifted by 1 element. 但现在我的问题是 :在我的“add_student”函数中,我得到一个具有奇怪结构的数组:它将自身包含为第一个元素,并将每个其他元素移位1个元素。

Cant figure out what the problem is ... Can somebody please help? 无法弄清问题是什么......有人可以帮忙吗?

EDIT: Somebody asked me, how this can be. 编辑:有人问我,这是怎么回事。 How the array can include "itself" as the first element. 数组如何包含“本身”作为第一个元素。

Well, here are screenshots of the debugger (xCode): 好吧,这是调试器(xCode)的屏幕截图:

Before entering "add_student": 在输入“add_student”之前:

在输入“add_student”之前

After entering "add_student" -> IN the function "add_student": 输入“add_student” - > IN函数“add_student”后:

输入“add_student” - > IN函数“add_student”后

As you see, "students" has now 5 elements ... 如你所见,“学生”现在有5个元素......

Pass the length of the array to the function and change this 将数组的长度传递给函数并更改它

for(i=0; i<sizeof(students); i++)

to this 对此

for(i=0; i<arrayLen; i++)

It seems you are inserting an element to some position in an array which has been allocated before hand; 看来你正在将一个元素插入到一个已预先分配的数组中的某个位置; inserting is maybe more proper term than adding, but that is opinion. 插入可能比添加更合适,但这是意见。

You were applying sizeof operator on array type inside function - this will likely give you size of pointer in bytes because array name decays to pointer. 你在函数内部的数组类型上应用了sizeof运算符 - 这可能会给你指针的大小,因为数组名称会衰减到指针。

It's a little strange to statically allocate the array but then dynamically allocate the individual structures. 静态分配数组然后动态分配各个结构有点奇怪。 You might consider dynamically allocating them all: 您可以考虑动态分配它们:

int num_students = 4;
struct student **students = malloc(sizeof(struct student) * num_students);

And then change your add_student function to look like this: 然后将add_student函数更改为如下所示:

void add_student(struct student *students[], int arr_size, char *firstname, char *secondname, long number)

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

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