I'm working on an assignment for class and having some issues with assigning values to an unsigned int ** variable that is in an object. For some reason, I can not assign it with malloc stating that there is an error with assignment to expression with array type. I'm not sure what this means, I have tried to use directly malloc it to image->pixmap, but have the same issue. This happens in both methods.
P.S. As this is an assignment I can NOT change the structs/object definitions, otherwise I would have.
I will do my best to explain the pixmap for PPMImage as an example:
pixmap: Three height x width, 2-dimensional pixel arrays, for ’R’, ‘G’, ‘B’ values
height: image height (number of rows)
width: image width (number of columns)
pixmax: maximum pixel value of image
//p->pixmap[0]: ‘R’ pixmap array
//p->pixmap[1][7]: 8th row of pixels of ‘G’ pixmap array
//p->pixmap[2][4][10]: 11th pixel in 5th row of ‘B’ pixmap array
typedef struct {
unsigned int ** pixmap[3];
unsigned int height, width, pixmax;
} PPMImage;
PPMImage * new_ppmimage( unsigned int w, unsigned int h, unsigned int m )
{
PPMImage *image;
image = (PPMImage *) malloc(sizeof(PPMImage));
image -> height = h;
image -> width = w;
unsigned int ** tempArray = malloc(sizeof(unsigned int *)*h);
for(int i = 0;i < h; i++){
tempArray[i] = malloc(sizeof(unsigned int) *w);
}
// issue here
image -> pixmap = tempArray;
return NULL;
}
=======================================
typedef struct {
unsigned int ** pixmap;
unsigned int height, width;
} PBMImage;
PBMImage * new_pbmimage( unsigned int w, unsigned int h )
{
PBMImage *image;
image = (PBMImage *) malloc(sizeof(PBMImage));
image -> height = h;
image -> width = w;
// issue here
image -> pixmap = malloc(sizeof(unsigned int *)*h);
for(int i = 0;i < h; i++){
image -> pixmap[i] = malloc(sizeof(unsigned int) *w);
}
return NULL;
}
unsigned int **; it isunsigned int **name[3];— which is pretty horrid as a type. Are you sure you need the array part? My brain hurts just looking at it.unsigned int**thePBMImage. and I am a little lost with the question of do I need the array part.and arrow->operators bind very tightly because they are postfix operators. They should not be written with spaces around them. Writingimage -> heightis not idiomatic C and indicates that the coder is a tyro (newbie). Useimage->height.