我想将 struct 的引用添加到 struct 数组中.但我只能遵循该 struct ,并将该 struct 的副本设置为索引.有没有办法为学生的索引提供传递到Add_Student函数中的学生 struct 的地址?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

typedef struct student {
    int age;
    char *name;
} Student;

Student *students;

int size = 0;

void add_student(Student *student) {
    Student *temp;
    size++;
    temp = realloc(students, sizeof(Student)*size);
    if(!temp) {
        printf("Cannot realloc memory\n");
        exit(1);
    }
    temp[size-1] = *student;
    students = temp;
}

int main(int argc, char **argv) {

    students = malloc(0);
    Student student;
    student.name = "name1";
    student.age = 11;
    add_student(&student);
    Student student2;
    student2.name = "name2";
    student2.age = 13;
    add_student(&student2);

    student2.age = 22;
    student2.name = "name33";
// I change the student2 and i would except that the Student struct gets changed in students[1]

    return 0;
}

推荐答案

struct数组包含struct个元素,而不是对struct个元素的引用.相反,您需要有一个指针数组,如

Student **students;

并在重新锁定时以这种方式继续,因此temp将具有相同的类型.然后你可以这样做:

temp[size-1] = student;

因为temp将是一个由Student个指针组成的数组,因此您将能够将引用存储为它的元素.

欲了解更多信息,请阅读https://www.geeksforgeeks.org/how-to-declare-and-initialize-an-array-of-pointers-to-a-structure-in-c/

它将其指定为此类动态 struct 指针数组的一般形式:

< structure_name >  ** < array_name > = <structure_name **> malloc ( sizeof( <structure_name> )* size ) ;

同一页提供了以下示例代码以供说明:

// C Program to implement
// Dynamic Array of structure
// pointer
#include <stdio.h>
#include <stdlib.h>
 
typedef struct node {
    int number;
    char character;
} node;
 
int main(void)
{
    // First pointer to structure
    node* structure_ptr1 = (node*)malloc(sizeof(node));
    // Second pointer to structure
    node* structure_ptr2 = (node*)malloc(sizeof(node));
    // Third pointer to structure
    node* structure_ptr3 = (node*)malloc(sizeof(node));
    // Fourth pointer to structure
    node* structure_ptr4 = (node*)malloc(sizeof(node));
 
    // Initializing structure pointers
 
    structure_ptr1->number = 100;
    structure_ptr1->character = 'a';
 
    structure_ptr2->number = 200;
    structure_ptr2->character = 'b';
 
    structure_ptr3->number = 300;
    structure_ptr3->character = 'c';
 
    structure_ptr4->number = 400;
    structure_ptr4->character = 'd';
 
    // Declaring the array dynamically for structure
    // pointers
 
    // Note that double pointer is
    // used for pointer to pointer
    node** structure_array
        = (node**)malloc(sizeof(node) * 4);
 
    // Initializing the array of structure pointers
 
    structure_array[0] = structure_ptr1;
    structure_array[1] = structure_ptr2;
    structure_array[2] = structure_ptr3;
    structure_array[3] = structure_ptr4;
 
    // Accessing dynamic 1D arrays - just like static 1D
    // arrays
    printf("Data:\n");
    for (int i = 0; i < 4; i++) {
        printf("%c - ", structure_array[i]->character);
        printf("%d\t\t", structure_array[i]->number);
        printf("\n");
    }
}

C++相关问答推荐

gcc已编译的可执行文件TSB是否同时暗示最低有效字节和最低有效位?

由Go调用E.C.引起的内存快速增长

Tiva TM4C123GXL的I2C通信

为什么STM32G474RE上没有启用RCC PLL

`#if`条件中是否允许`sizeof`?

S将C语言宏定义为自身的目的是什么?(在glibc标题中看到)

有什么方法可以将字符串与我们 Select 的子字符串分开吗?喜欢:SIN(LOG(10))

C指针概念分段故障

编译器如何处理具有更复杂值的枚举?

使用nmake for程序比Hello World稍微复杂一些

*S=0;正在优化中.可能是GCC 13号虫?或者是一些不明确的行为?

计算SIZE_MAX元素的长数组的大小

哪个首选包含第三个库S头文件?#INCLUDE;文件名或#INCLUDE<;文件名&>?

程序对大输入给出错误答案

如何使用calloc和snprintf

OMP并行嵌套循环

程序如何解释变量中的值

从文件到链表读取日期

共享内存未授予父进程权限

Linux memcpy 限制关键字语法