2017-10-28 185 views
0

我試圖做一個程序,對於給定的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變量的陣列。 ():「read_dividers(tt,5)」表示用戶給出了5個int,它們被轉換爲5個Divide結構體。編輯:在這種情況下在main()中輸入:「read_dividers(tt,5)」表示用戶給出5個int的結構體。 然而,我發現第二個程序崩潰後會發生什麼情況int

如果缺少更多信息,請不要猶豫,問問!

+1

什麼是你的問題...有什麼不工作? – Varun

+0

請提供您正在提供的輸入以及您收到的輸出與您期望作爲輸出接收的輸出。 – stringsn88keys

+0

@Varun請參閱下面的代碼「編輯」,我是新來的這個網站;在儘量縮短問題的同時儘量減少我需要提供的信息。 –

回答

0

您正在將未初始化的t[i]傳遞給read_dividert應該是指向Divide指針的指針,而不是指向Divide的指針,您可能在第一次通過時剛好幸運,但我懷疑它在第一次通話時失敗。