UPDATE:
This always seems to happen to me. Pointers are the devil. So i believe my issue is the fact that the return value that's being stored into my new array images[] is the same address.
I ended up printing the address for each index of the images[]. All addresses are different. However, when I print the next level images[i][0] all addresses are the same.
I'll put my thoughts in the what I've done section
I don't work in C/C++ often, so I always have to do a refresher of pointers.
ORIGINAL:
I have a program that locates a list of bmp files within a directory. From there I process all of them one at a time and store them into a array. Each image is made up of a struct associated with R,G,B. When I process a single image I insert it into my array of images at the next available index. However, when I've finished opening all bmp images and print out the data at each index they are the same.
here is the process
first the struct that is being used for the images is
typedef struct pixel {
uint8_t r;
uint8_t g;
uint8_t b;
} pixel;
pixel** img;
struct pixel** images[num_images];
img = (pixel**)malloc (IMG_Y * sizeof (pixel*));
for (i = 0; i < IMG_Y; i++)
{
img[i] = (pixel*)malloc (IMG_X * sizeof (pixel));
}
for (i = 0; i < num_images; i++)
{
if (open_bmp (*(image_names + i), img) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
images[i] = img;
}
}
just for background here is the open_bmp()
<pre>int open_bmp (char * file_name, pixel** img) {
long offset = 0;
unsigned char buff[100];
int x = 0, y = 0;
FILE *f = fopen (file_name, "rb");
if (verify_bmp (f, &offset, file_name) == EXIT_FAILURE) {
printf ("Unable to proceed\n");
return EXIT_FAILURE;
}
fseek (f, offset, SEEK_SET); while ((fread (&buff, 3, 1, f) >= 1) && y < IMG_Y) {
img[y][x].r = buff[2];
img[y][x].g = buff[1];
img[y][x].b = buff[0];
x++; if (x >= IMG_X) { y++;
x = 0;
}
}
if (PRINTBMPDATA) {
for (y = 0; y < IMG_Y; y++) {
for (x = 0; x < IMG_X; x++) {
printf ("%02x%02x%02x ", img[y][x].b, img[y][x].g, img[y][x].r);
}
printf ("\n");
}
printf ("\n");
}
if (fclose (f)) {
printf ("Unable to close image file\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
What I have tried:
I've done print statements in several locations to verify different parts work. For instance, "if (PRINTBMPDATA)" I changed to a value of 1 so that it would always print the results of open_bmp(). I verified that each image sent to open_bmp() is in fact different.
I also inserted a print loop immediately after the call to open_bmp() and printed the output variable "img" The return value is different each time.
However, when I loop "images[]", no matter what index I'm looking at it is exactly the same as the next.
I'm not sure what I'm missing. Any suggestions would be appreciated.
UPDATE:
So pointers. in this case I'm passing the variable img as a double pointer, yea thats new to me, so I'm assuming it works just like a point and really just passes the address of where I want data to be stored. So when I say the return value of open_bmp() I'm actually not thinking very straight. However, I'm assuming when I assign img to images[], I'm actually assigning the address of img, which never changes from one file to the next.
How do I pass the value of img and not the address when we are speaking of double pointers?