2014-12-03 129 views
-2

我試圖對fwrite()動態數組寫一個函數。 問題是fopen()內的指針。C - 通過函數動態數組寫指針,指針

雖然成功fwrite()動態數組文件,從主函數,當試圖移動fwrite()到一個單獨的函數與指針發生麻煩。具體指向數組的指針,它們位於函數內的fwrite()之內。

這裏是相關的代碼。

main() 
{ 
... 
    unsigned char **pixels_array = NULL; //write this array to file 
    allocateArray(&pixels_array); //prepare array 
    fillArray(&pixels_array); 
    writeFile(&pixels_array); 
    freeArray(&pixels_array); 
... 
} 

writeFile(unsigned char ***pixels_array) //param is pointer to double pointer array 
{ 
    ... 
    FILE *file = fopen(output_filename, "wb"); //open file 
    if (file == NULL) 
    { 
    printf(ERROR_OPEN_FILE_MSG); 
    return ERROR_OPEN_FILE; 
    } 

    for(i = 0; i < height; i++) //writing row by row of the array to file 
    { 
    //PROBLEM 
    //seg fault when running with current pointers to pixels_array in fwrite() 
    fwrite((&(*(*pixels_array)))[i], sizeof(unsigned char) * padded_width, 1, file); 
    } 
    fclose(file); 
} 

allocateArray(unsigned char ***pixels_array) 
{ 
    ... 
    *pixels_array = (unsigned char**)malloc(height * sizeof(unsigned char*)); //image y coord. 
    ... 
    for(i = 0; i < height; i++) 
    { 
    //(allocate scanlines) image x coord., no sizeof(unsigned char*) because == 1 
    (*pixels_array)[i] = (unsigned char*)malloc(width); 
    ... 
    } 
    ... 
} 
+1

爲什麼你不得不使用'(&(*(* pixels_array)))[i]'這個複雜的序列?不能變得更簡單,更直接? – 2014-12-03 07:11:44

+0

由於im限於c89標準,這種解決方案似乎是合適的。分配的指針數組用於存儲圖像的像素。圖像大小是可變的。 – 2014-12-03 07:30:05

+1

自從'c89'問你寫簡單工作的複雜陳述?你不能使用'pixels_array [i]'來代替嗎? – 2014-12-03 07:32:01

回答

0

當分配時出現一些錯誤時處理指針時會出現seg錯誤,那麼您是否也可以提供分配函數?

如果想寫一排,你可以簡單地使用文件:

fwrite((*(pixels_array))[i],rest is same); 
+0

我添加了分配,您的解決方案仍然存在seg故障。如果它的分配ID會感到驚訝,因爲當我從主函數寫入二進制文件時,所有的工作都很好,而不需要將函數放入單獨的函數中。 thx – 2014-12-03 08:15:37

+0

你的解決方案:fwrite((*(pixels_array))[i],其餘部分相同);出現了分段錯誤,因爲我使用了舊函數的骨架來爲fwrite提供兩個錯誤的寬度和高度值。 非常感謝所有:) – 2014-12-03 08:56:26

+0

這是我的第一個解決方案,所以我感覺很棒。很高興能有幫助:D – 2014-12-03 12:27:51

0

沒有什麼實際錯在您發佈的代碼,但它過於複雜。我會寫它如下:

unsigned char **allocateArray (int width, int height) 
{ 
    unsigned char **pa; 
    int i; 
    int nomem = 0; 

    pa = malloc (height * sizeof *pa); 
    if (pa) { 
    for (i = 0; i < height; i++) { 
     pa[i] = malloc (width * sizeof *pa[i]); 
     if (!pa[i]) { 
     nomem = 1; 
     } 
    } 

    if (nomem) { 
     for (i = 0; i < height; i++) { 
     free (pa[i]); 
     } 
     free (pa); 
     pa = NULL; 
    } 

    } 

    return pa; 
} 

void writeFile(unsigned char **pa) 
{ 
    int i; 

    FILE *file = fopen(output_filename, "wb"); 
    if (file == NULL) { 
    printf(ERROR_OPEN_FILE_MSG); 
    return ERROR_OPEN_FILE; 
    } 

    for(i = 0; i < height; i++) { 
    if (!fwrite(pa[i], padded_width * sizeof *pa[i], 1, file)) { 
     /* Error */ 
    } 
    } 
    fclose(file); 
} 

int main(void) 
{ 
    unsigned char **pixels_array = allocateArray(padded_width, height); 
    fillArray(pixels_array); 
    writeFile(pixels_array); 
    freeArray(pixels_array); 

    return 0; 
}