2012-03-08 92 views
0

我一直在嘗試幾個小時才能使該函數正常工作。這裏的任務:動態分配的數組結構

Add: Request the part name, price, and quantity. Save the information to a dynamically allocated array of structs. You may allocate space for up to 3 structs at a time. You will need to create more memory dynamically as needed. Use this struct (you may use typedef if you want to):

到目前爲止我的代碼是

typedef struct { 
    char* name; 
    float price; 
    int quantity; 
}part; 


void add(part *item, int *part_count) 
{ 
    //char temp[100]; 

    if (!item){ 
     item = malloc(sizeof(part)*3); 
    } 
    else{ 
     item = realloc(item, sizeof(part) * ((*part_count*3) + 1)); 
    } 

    item[*part_count].name = malloc(sizeof(char)*64); // max of 64 characters 

    printf("Please enter item name: \n"); 
    //fgets(temp, strlen(temp), stdin); 
    //sscanf(temp, "%s", item[*part_count].name); 
    scanf("%64s", item[*part_count].name); 

    printf("Please enter item price: \n"); 
    //fgets(temp, strlen(temp), stdin); 
    //sscanf(temp, "%f", &item[*part_count].price); 
    scanf("%f", &item[*part_count].price); 

    printf("Please enter item quantity: \n"); 
    //fgets(temp, strlen(temp), stdin); 
    //sscanf(temp, "%d", &item[*part_count].quantity); 
    scanf("%d", &item[*part_count].quantity); 

    *part_count = *part_count+ 1; 
    } 

我曾試圖採取與fgets()sscanf()輸入,但使用的代碼它永遠不會允許用戶輸入數據,然後結束功能。

我相信問題在於我的內存分配,因爲當我嘗試對數組執行任何操作(例如打印內容)時出現分段錯誤。

+0

你會得到哪些線路故障? – 2012-03-08 02:13:13

+2

你如何計算'part_count'?如果是元素的數量,則無法訪問該元素。所以如果你的數組大小爲10,你不能訪問array [10]。 – prelic 2012-03-08 02:13:30

+0

我不知道哪條線我得到seg故障,但我有一個單獨的打印功能,當它運行時它seg故障。我曾問過,但顯然我的打印功能不是問題。 part_count也從0開始,每次調用add時都會增加。 – 2012-03-08 02:20:48

回答

1

推測,第一次調用add()時,項目將爲NULL,並且您將執行其初始分配;後續調用realloc,以便該數組是3倍所需的大小(我不認爲是你真正想要的)。

但是,匹配項的參數不會因調用add()而改變,因此它保持爲NULL,並且每次調用add()都會像初始調用一樣操作,爲3分配空間結構(當你添加第四個結構時會出現問題)。

您可以使item成爲一個**部分,並且在當前使用part的任何位置使用* part,以便保留指針的新值(您將傳遞* part的地址作爲參數)。或者使用item的新值作爲函數的返回值,這是一個更清潔的恕我直言。 (這是參考參數派上用場的地方,但C沒有這樣的東西。)

+0

我得到錯誤:請求成員'名稱'的東西不是結構。 這是我改變了項目結構的類型部分**,然後在函數調用中改變它。 – 2012-03-08 02:51:28

+0

沒有看到實際的代碼,不能幫上忙。 – 2012-03-08 14:23:12

1

你的函數有一個不可能的接口。它接受一個part *指針。這個指針通過值進入函數。在您分配給它的功能中,從mallocrealloc調用。但是調用者不會看到這個更新的值。當函數返回時,你分配的內存已經泄漏,並且調用者具有原始指針值(可能爲null)。另外,最好用動態數組封裝動態數組。你有這個「零件數量」變量,它本身就是鬆散的,它必須與數組一起傳遞以跟蹤其大小。如何包裝在一起:

typedef struct part_list { 
    struct part *part; 
    int count; 
} part_list; 

現在有一個函數來初始化空的部分名單。這個必須被所有想要使用其他part_list函數的人調用。

void part_list_init(part_list *pl) 
{ 
    pl->part = 0; 
    pl->count = 0; 
} 

然後編寫你的函數來添加零件。

int part_list_add(part_list *pl) 
{ 
    part_list *p; 
    int index = pl->count++; /* increment count, keep old value */ 

    /* realloc accepts a null pointer and then behaves like malloc */ 
    p = realloc(pl->part, sizeof *pl->part * pl->count); 
    if (p == 0) 
    return 0; /* failed to allocate/extend array */ 
    p1->part = p; 

    if ((pl->part[index].name = malloc(64)) == 0) { 
    pl->count = index; /* roll back the count: we didn't really allocate this part */ 
    return 0; 
    } 

    /* your code, updated with pl-> access */ 
    printf("Please enter item name: \n"); 
    scanf("%63s", pl->part[index].name); /* 63s not 64s!!! One byte for NUL char! */ 

    printf("Please enter item price: \n"); 
    scanf("%f", &pl->part[index].price); /* check the return value of scanf!!! */ 

    printf("Please enter item quantity: \n"); 
    scanf("%d", &pl->part[index].quantity); 

    return 1; /* 1 means success */ 
} 
+0

我不能使用鏈表來解決問題,它看起來像你做了什麼。 – 2012-03-08 02:42:22

+0

這裏暗示的數據表示不是鏈表。有一個'part_list'包含一個指向數組的指針。 'part_list'結構封裝了數組和大小,使得它們易於作爲一個單元傳遞。 'part_list_add'函數可以通過更新指定結構中的指針來輕鬆分配和擴展數組。 – Kaz 2012-03-08 03:08:25