gpt4 book ai didi

在结构中创建指向结构的指针数组

转载 作者:太空宇宙 更新时间:2023-11-04 08:13:13 24 4
gpt4 key购买 nike

基本上,我有一个这样定义的结构:

struct D_Array
{
int Capacity;
int Cur_Size;
struct Student* List;
};

现在,在创建结构之后;

struct D_Array* T_Array;
if (T_Array = malloc(sizeof(struct D_Array)) == NULL)
{
printf("There has been a problem with memory allocation. Exiting the program.");
exit (21);
}

我在这个结构中创建学生列表时遇到问题。我需要列表是一个指针数组,因为我应该制作某种 ADT 程序,所以我尝试将其制作成这样:

T_Array->Capacity = 10;
T_Array->Cur_Size = 0;
T_Array->List[10]= (struct Student*) malloc(sizeof (struct Student*));

无论我如何尝试更改它,我都会遇到同样的错误:从类型“struct Student *”分配给类型“struct Student”时类型不兼容 |

我正在尝试创建一个数组,我将能够执行类似的操作;

T_Array->List[1]=Student_Pointer;

谢谢!

最佳答案

将数组的类型更改为 Student** 结构,这将是指针数组:

struct student * --> 指向学生的指针。struct student ** --> 指向学生指针的指针(你需要的)。

struct D_Array
{
int Capacity;
int Cur_Size;
struct Student** List;
}

和分配:

T_Array->List = malloc (sizeof (struct Student*) *  lengthofarray);

然后:

T_Array->List[1]=Student_Pointer;

关于在结构中创建指向结构的指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37629136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com