添加指向数组的指针

时间:2017-10-28 16:50:43

标签: c arrays pointers malloc dynamic-memory-allocation

我试图制作一个程序,以便给定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():&#34; read_dividers(tt,5)&#34;表示用户提供5 int个,转换为5个Divide结构。 相反,在我给出第二个int

之后,程序崩溃了

如果缺少更多信息,请不要犹豫!

1 个答案:

答案 0 :(得分:0)

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