I'm attempting to solve this for myself as this is homework but I can't get past the hurdle. I have an array that dynamically holds the pixels of an image (chars) in its second dimension (first being the image number).
I first initialize my array variable...
unsigned char** imgSet;
I try to allocate the room when you call the load image function to hold room for just one image, I was thinking it was something like imgSet[0][] as in now I have one index pointing to the second...
imgSet = malloc((numImages+1)*(sizeof(unsigned char*)));
loadImg(imgSet,&numImages);
printf("File has %d rows and %d columns\n",imgSet[numImages][0]+1,imgSet[numImages][1]+1);
My loadImg function calls a function from a piece of code my teacher gave us (it actually converts bmp files into text:
void loadImg(unsigned char** images, int *n) {
char fileName[80];
printf("Enter filename: ");
scanf("%s",fileName);
readImage(fileName,images);
printf("File %s has %d rows and %d columns\n",fileName,images[*n][0]+1,images[*n][1]+1);
(*n)++;
}
This is he's function, or part with the malloc:
void readImage(const char *fname, unsigned char** img) {
//a lot of code taken out that just makes sure that the file is right
*img = (unsigned char *)malloc(sizeof(unsigned char)*(2+nRows*nCols)); /* allocate space for image */
}
The red is the part that gives me the segmentation fault. I do the same thing in my loadImg function after I call my teachers function and it works, but once I leave the function it fails. What's wrong with this?