gpt4 book ai didi

c - C 中的 malloc 二维数组

转载 作者:太空宇宙 更新时间:2023-11-04 06:30:56 25 4
gpt4 key购买 nike

<分区>

我有一个在二维数组中存储信息的结构:

struct slopes {
int size;
int ** slope_array;
};

我为结构分配了所需的内存(数组的维度为 s*s):

struct slopes * slope=malloc(sizeof(struct slopes));
slope->size=s;
slope->slope_array=malloc(sizeof(int *)*s);
int i;
for(i=0;i<s;i++) {
slope->slope_array=malloc(sizeof(int)*s);
}

但是像这样的行似乎会引发段错误:

slope->slope_array[0][0]=3;

有人能看出我做错了什么吗?

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