在 C 语言中为 "char" 数组的 malloc 强制转换错误



使用结构变量和结构数组读取 bmp 图像的代码。请建议我正确方法对malloc进行类型转换(代码下面列出了错误(:

#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, we need to use 8bit datatype for each color
}bands;
int main()
{
FILE *bmpimage; //ptr to read image file
FILE *redpix,*greenpix,*bluepix; //ptr to create band/color wise file
unsigned short pix_x=223,pix_y=197; /*pix_x: no. of pixels in a row,   pix_y: no. of pixels in a column of input image*/
unsigned short n_pix=pix_x*pix_y;   /*variable to 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 a pointer to a memory block,'*readbuffer',
that has 'n_pix' no. of memory blocks each of size same as struct bands */  
bands *readbuffer=(char*)malloc(n_pix*sizeof(*readbuffer)); 
int n;
//Create memory for 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 to 54th byte to access pixelvalue data (since, 24bit bmp format) */
fseek(bmpimage,54,SEEK_SET);
/* Read 'n_pix' no. of 'bgr' blocks each of which are of the size same as "struct bands" */
fread(readbuffer,sizeof(bands),n_pix,bmpimage);  /*read 'n_pix' no. of 'bgr' blocks each of which are of the size same as "struct bands" to the 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 to print pixel values in correct order*/
for(i=pix_y;i>0;i--){   /*for accessing row data. Choose to print from bottom to top*/
 for(j=1;j<=pix_x;j++){ /*for accessing column data. Print from left to 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,"%dn",readbuffer[(i-1)*pix_x + j].r);
    fprintf(greenpix,"%dn",readbuffer[(i-1)*pix_x + j].g);
    fprintf(bluepix,"%dn",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;   
}

引用:如何在 C 中正确定位结构数组

malloc 结构指针数组与结构数组

错误列表:

bmpread_check.c:在函数"main"中: bmpread_check.c:24:19:警告:从不兼容的指针类型初始化>[默认启用] bands readbuffer=(char(malloc(n_pix*sizeof(*readbuffer((; ^ bmpread_check.c:29:33:错误:预期的"="、"、";"、"asm"或">属性">在"." 令牌之前" unsigned char readbuffer[n].b = (char(malloc(sizeof(readbuffer[n].b((; ^ bmpread_check.c:29:33:错误:"." 标记之前的预期表达式 bmpread_check.c:30:33:错误:预期的"="、"、";"、"asm"或">属性">在"." 令牌之前 unsigned char readbuffer[n].g = (char(malloc(sizeof(readbuffer[n].g((; ^ bmpread_check.c:30:33:错误:"." 标记之前的预期表达式 bmpread_check.c:31:33:错误:预期的"="、"、";"、"asm"或">属性">在"." 令牌
之前 unsigned char readbuffer[n].r = (char(malloc(sizeof(readbuffer[n].r((; ^ bmpread_check.c:31:33:错误:"." 标记之前的预期表达式 bmpread_check.c:69:5:警告:"free"传递参数 1 会使指针从>整数中产生不强制转换 [默认启用] free(readbuffer[n].b( ; ^ 在 bmpread_check.c:3:0 包含的文件中: C:\mingw\include\stdlib.h:357:38:注意:预期的"void",但参数>类型为"无符号字符" _CRTIMP空__cdecl __MINGW_NOTHROW自由(空(; ^ bmpread_check.c:70:5:警告:"free"的传递参数 1 会使指针从>整数中产生不强制转换 [默认启用] free(readbuffer[n].g( ; ^ 在 bmpread_check.c:3:0 包含的文件中: C:\mingw\include\stdlib.h:357:38:注意:预期的"void">,但参数是>类型"无符号字符"_CRTIMP void__cdecl __MINGW_NOTHROW自由(void(; ^ bmpread_check.c:71:5:警告:"free"传递参数 1 会使指针从没有强制转换的>整数 [默认启用] free(readbuffer[n].r( ; ^ 在 bmpread_check.c:3:0 包含的文件中: C:\mingw\include\stdlib.h:357:38:注意:预期的"void">,但参数的类型>"无符号字符" _CRTIMP空__cdecl __MINGW_NOTHROW自由(空(; ^

这个:

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

(注意:不是*readbuffer。这是bands(

已为所有n_pix频段分配内存。

无需为b, g, r分配内存,因为它们不是指针。

所以

//Create memory for each of 'n_pix' no. of pixel array of each color 
// And allocating using for loop

不需要。

变量

bgr不是指针,而是无符号的8位变量。因此,在这种情况下分配内存的正确方法是分配该结构的数组,其大小为像素总数,即宽度乘以图像的高度。

这可以通过动态分配结构指针来实现bands*如下所示。

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

该语句将分配结构n_pix次,以便您可以在每个单独的像素位置初始化和访问像素值bgr,如下所示。

readbuffer[i]-> b = 20;
readbuffer[i]-> g = 80;
readbuffer[i]-> r = 40;

i可以是从0n_pix-1的任何内容

相关内容

  • 没有找到相关文章

最新更新