2014-12-03 107 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; 
}