小编KMM*_*KMM的帖子

使用C中另一个结构内的2-d结构数组编码问题

我正在使用二维结构数组,这是另一个结构的一部分.这不是我做了很多事情所以我遇到了问题.在接近结束的"测试"for-loop之后,此函数最终失败.它在出现故障之前正确打印出一行.

我的代码中将数据读入虚拟2-d结构数组的部分工作正常,因此必须将我的赋值数组作为另一个结构(imageStruct)的一部分.

任何帮助将不胜感激!

/*the structure of each pixel*/
typedef struct
{
 int R,G,B;
}pixelStruct;

/*data for each image*/
typedef struct
{ 
 int height;
 int width;
 pixelStruct *arr; /*pointer to 2-d array of  pixels*/
} imageStruct;


imageStruct ReadImage(char * filename)
{
 FILE *image=fopen(filename,"r");
 imageStruct thisImage;

        /*get header data from image*/

        /*make a 2-d array of of pixels*/
 pixelStruct imageArr[thisImage.height][thisImage.width];

        /*Read in the image. */

        /*I know this works because I after storing the image data in the
          imageArr array, I printed …
Run Code Online (Sandbox Code Playgroud)

c arrays struct

6
推荐指数
1
解决办法
2573
查看次数

C:比较两个字节以查看一个是否在另一个的某个范围内

我正在编写代码,比较代表整数的2个字节.我想看看字节R是否与G的+ -10相同.我对代码的问题是在结尾附近的if-statment中进行比较.字节永远不会超出范围,即使它们应该.我确定问题来自于我如何添加/减去error_range,但我不知道有任何其他方法可以做到这一点.

我首先考虑将字节转换为整数但我在网上找不到任何帮助.如果这比我在这里做的更好,请告诉我该怎么做.

任何帮助表示赞赏!

const char ERROR_RANGE = 0x1010; //warning: overflow in implicit constant conversion
char R, G; /2 separate bytes
char buffer; //enough space for 1 byte

image = fopen(fileName,"r"); //open file

fread(&buffer, 1, 1, image); //read 1 byte  
memcpy (&R,&buffer,1); //store it as R

fread(&buffer, 1, 1, image); //read 1 byte   
memcpy (&G,&buffer,1); //store it as G

fclose(image);

if((R >= (G + ERROR_RANGE)) && (R <= (G - ERROR_RANGE)))
{
    printf("Outside of range!\n");
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

c byte

0
推荐指数
2
解决办法
2293
查看次数

标签 统计

c ×2

arrays ×1

byte ×1

struct ×1