2021年3月19日星期五

(C) I am trying to invert the rows of a 2d array using pointers [closed]

[Home work]

-Background I am creating a program that will take in an image file either .bmp or .ppm, and either make a copy, convert or shift the color of the overall image. I am currently stuck on flipping the image so that if converting from one to another, the image will not be upside down (.bmp data is stored from bottom to top, .ppm from top to bottom) I would just add another function that writes the file in opposite order, but we are told not to alter what was provided or add functions to the header files provided. I am reading the binary data from the file in order to get the data for each pixel, and the data structure I am using is a 2d array of Pixel structs.

-what I have:

void convertPixelArr(Pixel ***pixelArr, int height, int width){    int i;    int j = height - 1;    Pixel *temp = malloc(sizeof(Pixel)*width);    for(i=0; i<height; i++){      memcpy(temp, *pixelArr[i], sizeof(Pixel)*width);      memcpy(*pixelArr[i], *pixelArr[j], sizeof(Pixel)*width);      memcpy(*pixelArr[j], temp, sizeof(Pixel)*width);      j--;    }    free(temp);  }  

It seems that this is not copying the data, but it is removing the data. it results in SIGSEGV, and I can see the Pixel structs in the original array are NULL when I later go to write the file and my program crashes at that point. (at least thats what I can gather from the debugger.)

I did try to implent first without memcpy() by moving the rows, then by stepping through each pixel in nested for loops, moving each pixel one by one, and both behaved the same way as this, I also tried returning a Pixel** and the call to the convertPixelArr was made on the right hand side of an assignment statement, which i do not think is the right path, but I tried it anywoays.

https://stackoverflow.com/questions/66717256/c-i-am-trying-to-invert-the-rows-of-a-2d-array-using-pointers March 20, 2021 at 08:48AM

没有评论:

发表评论