I am trying to create a pointer to an array of structs and pass it to a function where I will change values of the struct.
Could someone please explain me why my code seems to not correctly allocate the memory for the array as execution of the compiled version fails for 27pixels if I store both integer variables in the struct whereas it works for 27pixels if I only store one integer variable?
typedef struct Pixel
{
int label;
int area;
}Pixelmap;
int change_pixel(Pixelmap* pixelmap)
{
for (x = 0; x < dimx*dimy*dimz; x++)
{
pixelmap[x].label = x; /* or malloc and strcpy */
pixelmap[x].area = 1;
printf("%i\n",pixelmap[x].label);
}
return 1;
}
int main()
{
Pixelmap *pm;
pm = malloc(dimx*dimy*dimz * sizeof (struct Pixel));
change_pixel(&pm);
printf("End!\n");
free(pm);
return 0;
}
change_pixel(&pm);====>change_pixel(pm);and turn up your compiler warnings as high as they can go. you should have been notified of an incompatible type.change_pixelshould be renamedset_pixelsince the memory allocated bymallocis uninitialised.