C语言 向指针数组添加指针



我正在尝试制作一个程序,为给定的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

崩溃

如果缺少更多信息,请随时询问!

您正在将未初始化的t[i]传递给read_dividert应该是指向 Divide 的指针,而不是指向 Divide 的指针,您可能只是在第一次传递时很幸运,但我怀疑它在第一次调用时就失败了。

最新更新