【问题标题】:Casting errors in malloc for "char" array in C language [closed]C语言中“char”数组的malloc转换错误[关闭]
【发布时间】:2016-02-08 06:14:27
【问题描述】:

使用结构变量和结构数组读取 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,"%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;   

}

参考资料: How to properly malloc for array of struct in C

malloc an array of struct pointers vs array of structs

错误列表:

bmpread_check.c:在函数'main'中: bmpread_check.c:24:19:警告:从不兼容的指针类型初始化> [默认启用] 乐队 readbuffer=(char)malloc(n_pix*sizeof(*readbuffer)); ^ bmpread_check.c:29:33: 错误: 预期 '=', ',', ';', 'asm' or 'attribute' >before '.'令牌 无符号字符 readbuffer[n].b = (char)malloc(sizeof(readbuffer[n].b)); ^ bmpread_check.c:29:33:错误:“。”之前的预期表达式令牌 bmpread_check.c:30:33: 错误: 预期 '=', ',', ';', 'asm' or 'attribute' >before '.'令牌 无符号字符 readbuffer[n].g = (char)malloc(sizeof(readbuffer[n].g)); ^ bmpread_check.c:30:33:错误:“。”之前的预期表达式令牌 bmpread_check.c:31:33: 错误: 预期 '=', ',', ';', 'asm' or 'attribute' >before '.'令牌
无符号字符 readbuffer[n].r = (char)malloc(sizeof(readbuffer[n].r)); ^ bmpread_check.c:31:33:错误:“。”之前的预期表达式令牌 bmpread_check.c:69:5:警告:传递 'free' 的参数 1 使来自 >integer 的指针没有强制转换 [默认启用] 空闲(读取缓冲区[n].b); ^ 在 bmpread_check.c:3:0 包含的文件中: c:\mingw\include\stdlib.h:357:38: 注意:预期为 'void ' 但参数是 >type 'unsigned char' _CRTIMP void __cdecl __MINGW_NOTHROW free (void); ^ bmpread_check.c:70:5:警告:传递 'free' 的参数 1 使来自 >integer 的指针没有强制转换 [默认启用] 空闲(读取缓冲区[n].g); ^ 在 bmpread_check.c:3:0 包含的文件中: c:\mingw\include\stdlib.h:357:38: 注意:预期为 'void ' 但参数是 >type 'unsigned char' _CRTIMP void __cdecl __MINGW_NOTHROW free (void); ^ bmpread_check.c:71:5:警告:传递 'free' 的参数 1 使来自 >integer 的指针没有强制转换 [默认启用] 空闲(读取缓冲区[n].r); ^ 在 bmpread_check.c:3:0 包含的文件中: c:\mingw\include\stdlib.h:357:38: 注意:预期为 'void ' 但参数类型为 >'unsigned char' _CRTIMP void __cdecl __MINGW_NOTHROW free (void); ^

【问题讨论】:

  • 你的问题是什么?
  • 请查看图片“错误列表”的链接
  • bands *readbuffer=(bands *)malloc(n_pix*sizeof(*readbuffer)); 或更好的bands *readbuffer=malloc(n_pix*sizeof(*readbuffer));。成员 b,g,r 不是指针,不需要为它们分配。
  • 在您的问题中包含错误列表作为文本而不是作为图像。
  • @Keith。我无法从命令提示符复制这些错误列表。因此我附上了它的截图。

标签: c struct malloc bmp


【解决方案1】:

这个:

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

不需要。

【讨论】:

  • 并且类型转换必须要么更改为(bands*),要么完全删除。
  • @GNKeshava 根据我的验证,我使用 sizeof(bands) 还是 sizeof(*readbuffer) 都无关紧要,因为它们的大小相同。
  • 你的建议很有用 Martin Zabel。
  • @GN Keshava 是的,“删除用于为数组中的每个元素分配内存的循环”是正确的步骤,因为程序开始正确执行。但是讨论“stackoverflow.com/questions/12334343/…”在这些行中说了一些我无法正确解释的内容。
  • @SaiKrishna ,该链接讨论了分配指针和分配指向指针的指针之间的区别。您的案例只是分配一个指针。在您给出链接的答案中,他正在演示如何分配,1.指向指针(指向结构)的指针和 2.指向(指向结构)的指针。所以他需要多个malloc。但是在您的情况下,结构不包含指针。所以,不需要多个分配..
【解决方案2】:

变量 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 的任何值

【讨论】:

    猜你喜欢
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多