struct - Casting errors in malloc for "char" array in C language -


code read bmp image using struct variables , struct array. kindly suggest me correct way typecasting malloc(errors listed below code):

#include<stdio.h> #include<stdlib.h>  typedef struct bands{ /* .bmp image store pixel colors in "bgr" sequence */ unsigned char b,g,r; //in 24bit bmp, need use 8bit datatype each color }bands;  int main() { file *bmpimage; //ptr read image file file *redpix,*greenpix,*bluepix; //ptr create band/color wise file unsigned short pix_x=223,pix_y=197; /*pix_x: no. of pixels in row,   pix_y: no. of pixels in column of input image*/ unsigned short n_pix=pix_x*pix_y;   /*variable count total no. of  pixels*/  bmpimage=fopen("blocks223x197.bmp","r"); //24 bit bmpimage redpix=fopen("redpixels.txt","w"); greenpix=fopen("greenpixels.txt","w"); bluepix=fopen("bluepixels.txt","w");  /*  define pointer memory block,'*readbuffer', has 'n_pix' no. of memory blocks each of size same struct bands */   bands *readbuffer=(char*)malloc(n_pix*sizeof(*readbuffer));   int n; //create memory each of 'n_pix' no. of pixel array of each color  for(n=0;n<n_pix;n++){     unsigned char *readbuffer[n].b =  (char*) malloc(sizeof(readbuffer[n].b));     unsigned char *readbuffer[n].g = (char*) malloc(sizeof(readbuffer[n].g));     unsigned char *readbuffer[n].r = (char*) malloc(sizeof(readbuffer[n].r)); }  if(!bmpimage){printf("error reading bmpimage!");return 1;} if(readbuffer==null){printf("null buffer"); exit(1);}  /* go 54th byte access pixelvalue data (since, 24bit bmp format) */ fseek(bmpimage,54,seek_set);  /* read 'n_pix' no. of 'bgr' blocks each of of size same "struct bands" */ fread(readbuffer,sizeof(bands),n_pix,bmpimage);  /*read 'n_pix' no. of 'bgr' blocks each of of size same "struct bands" memory address, 'readbuffer' or '&readbuffer[0]' */       int n_blocks=(sizeof(readbuffer)/sizeof(bands)); printf("no. of blocks read= %d, n_pix=%d",n_blocks,n_pix);   int i,j; int count; count=0; /* logic print pixel values in correct order*/  for(i=pix_y;i>0;i--){   /*for accessing row data. choose print bottom top*/  for(j=1;j<=pix_x;j++){ /*for accessing column data. print left right*/      if(j!=pix_x){       fprintf(redpix,"%d,",readbuffer[(i-1)*pix_x + j].r);     fprintf(greenpix,"%d,",readbuffer[(i-1)*pix_x + j].g);     fprintf(bluepix,"%d,",readbuffer[(i-1)*pix_x + j].b);     }     else{         count++;     fprintf(redpix,"%d\n",readbuffer[(i-1)*pix_x + j].r);     fprintf(greenpix,"%d\n",readbuffer[(i-1)*pix_x + j].g);     fprintf(bluepix,"%d\n",readbuffer[(i-1)*pix_x + j].b);     }   } }  // free allocated memory  for(n=0;n<n_pix;n++){     free(readbuffer[n].b) ;     free(readbuffer[n].g) ;     free(readbuffer[n].r) ; }   fclose(bmpimage);fclose(redpix);fclose(bluepix);fclose(greenpix);  return 0;     } 

references: how malloc array of struct in c

malloc array of struct pointers vs array of structs

list of errors:

bmpread_check.c: in function 'main': bmpread_check.c:24:19: warning: initialization incompatible pointer type >[enabled default] bands readbuffer=(char)malloc(n_pix*sizeof(*readbuffer)); ^ bmpread_check.c:29:33: error: expected '=', ',', ';', 'asm' or 'attribute' >before '.' token unsigned char readbuffer[n].b = (char)malloc(sizeof(readbuffer[n].b)); ^ bmpread_check.c:29:33: error: expected expression before '.' token bmpread_check.c:30:33: error: expected '=', ',', ';', 'asm' or 'attribute' >before '.' token unsigned char readbuffer[n].g = (char)malloc(sizeof(readbuffer[n].g)); ^ bmpread_check.c:30:33: error: expected expression before '.' token bmpread_check.c:31:33: error: expected '=', ',', ';', 'asm' or 'attribute' >before '.' token
unsigned char readbuffer[n].r = (char)malloc(sizeof(readbuffer[n].r)); ^ bmpread_check.c:31:33: error: expected expression before '.' token bmpread_check.c:69:5: warning: passing argument 1 of 'free' makes pointer >integer without cast [enabled default] free(readbuffer[n].b) ; ^ in file included bmpread_check.c:3:0: c:\mingw\include\stdlib.h:357:38: note: expected 'void ' argument of >type 'unsigned char' _crtimp void __cdecl __mingw_nothrow free (void); ^ bmpread_check.c:70:5: warning: passing argument 1 of 'free' makes pointer >integer without cast [enabled default] free(readbuffer[n].g) ; ^ in file included bmpread_check.c:3:0: c:\mingw\include\stdlib.h:357:38: note: expected 'void ' argument of >type 'unsigned char' _crtimp void __cdecl __mingw_nothrow free (void); ^ bmpread_check.c:71:5: warning: passing argument 1 of 'free' makes pointer >integer without cast [enabled default] free(readbuffer[n].r) ; ^ in file included bmpread_check.c:3:0: c:\mingw\include\stdlib.h:357:38: note: expected 'void ' argument of type >'unsigned char' _crtimp void __cdecl __mingw_nothrow free (void); ^

this:

bands *readbuffer=(bands*)malloc(n_pix*sizeof(bands)); 

(note: not *readbuffer. it's bands)

has allocated memory n_pix bands.

there no need allocate memory b, g, r not pointers.

so,

//create memory each of 'n_pix' no. of pixel array of each color  // , allocating using loop 

is not needed.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -