【发布时间】:2018-04-10 01:43:27
【问题描述】:
我正在尝试制作一个程序,为给定的int value 保留分隔符的数量:int amount_of_dividers 和这些分隔符的列表:int* dividers
这是代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int value;
int amount;
int* dividers;
} Divide;
int main(){
Divide ** tt;
read_dividers(tt,5);
}
/* the functions "amount_of_dividers(int g)" and "dividers_of(int g, int amount)"
used in void read_divider are working properly, they are not needed for this question */
void read_divider(Divide *g){
scanf("%d",&(g->value));
g->amount = amount_of_dividers(g->value);
g->dividers = dividers_of(g->value,g->amount);
}
/* assuming that read_divider works, what causes read_dividerS to crash? */
void read_dividers(Divide ** t, int amount){
int i = 0;
t = malloc(amount*sizeof(Divide*));
for(i = 0;i<amount;i++){
read_divider(t[i]);
}
}
Read_dividers 使用指针数组**t,我试图用指向Divide g 变量的指针填充该数组的每个元素。
编辑:在这种情况下,在 main() 中输入:“read_dividers(tt,5)”表示用户提供 5 个int,它们被转换为 5 个Divide 结构。
相反,在我给出第二个int
如果缺少更多信息,请随时询问!
【问题讨论】:
-
你的问题到底是什么...有什么不工作的?
-
请提供您提供的输入和您收到的输出与您期望收到的输出。
-
@Varun 查看代码下方的“编辑”,我是这个网站的新手;有点纠结于我需要提供多少信息,同时还要让问题尽可能简短。
-
它在哪里崩溃?已经在调试器下运行了吗?程序是否只是蒸发或报告某种错误?请查看stackoverflow.com/help/how-to-ask
-
顺便说一句:变量
tt正在使用而没有被初始化。
标签: c arrays pointers malloc dynamic-memory-allocation