임 fwrite()
함수를 사용하는 동적 배열. 문제는 fopen()
내부의 포인터입니다.C - fwrite 함수에 의한 동적 배열, 포인터
성공적으로 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);
...
}
...
}
왜이 복잡한 시퀀스를 (? (* pixels_array)) [i]'사용해야 했습니까? 더 간단하고 직선적으로 만들 수 없습니까? –
메신저가 c89 표준으로 제한되어 있으므로이 솔루션이 적합합니다. 할당 된 포인터 배열은 이미지의 픽셀을 저장하는 데 사용됩니다. 이미지 크기는 가변적이다. –
'c89 '가 간단한 작업을위한 복잡한 문장을 쓸 것을 요구할 때부터? 대신'pixels_array [i]'를 사용할 수 없습니까? –