我试图用一个时间循环替换我的'for‘循环,但是我得到了一个不同的输出。
for循环:
for(int row = mlx->shot->height - 1; row >= 0; row--)
{
y = 0;
for(int col = 0; col < mlx->shot->width; col++)
{
int red = (mlx->tex.img_data[x * w + y] >> 16) & 0xFF;
int green = (mlx->tex.img_data[x * w + y] >> 8) & 0xFF;
int blue = mlx->tex.img_data[x * w + y] & 0xFF;
buf[row * mlx->shot->width_in_bytes + col * 3 + 0] = blue;
buf[row * mlx->shot->width_in_bytes + col * 3 + 1] = green;
buf[row * mlx->shot->width_in_bytes + col * 3 + 2] = red;
y++;
}
x++;
}在这里输出是完美的
My循环:
int row;
row = mlx->shot->height - 1;
while (row >= 0)
{
y = 0;
col = 0;
while (col < mlx->shot->width)
{
int red = (mlx->tex.img_data[x * w + y] >> 16) & 0xFF;
int green = (mlx->tex.img_data[x * w + y] >> 8) & 0xFF;
int blue = mlx->tex.img_data[x * w + y] & 0xFF;
buf[row * mlx->shot->width_in_bytes + col * 3 + 0] = blue;
buf[row * mlx->shot->width_in_bytes + col * 3 + 1] = green;
buf[row * mlx->shot->width_in_bytes + col * 3 + 2] = red;
col++;
y++;
}
row--;
x++;
}但是这里我得到了一个:1163 segmentation fault
编辑:其余的变量已经声明了。这是孔函数:
void screno(t_mlx *mlx)
{
int x ;
int y ;
int row;
int col;
x = 0;
y = 0;
row = mlx->shot->height - 1;
col = 0;
unsigned char* buf = malloc(mlx->shot->imagesize);
while (row >= 0)
{
y = 0;
col = 0;
while (col < mlx->shot->width)
{
int red = (mlx->tex.img_data[x * w + y] >> 16) & 0xFF;
int green = (mlx->tex.img_data[x * w + y] >> 8) & 0xFF;
int blue = mlx->tex.img_data[x * w + y] & 0xFF;
buf[row * mlx->shot->width_in_bytes + col * 3 + 0] = blue;
buf[row * mlx->shot->width_in_bytes + col * 3 + 1] = green;
buf[row * mlx->shot->width_in_bytes + col * 3 + 2] = red;
col++;
y++;
}
row--;
x++;
}
/*for(int row = mlx->shot->height - 1; row >= 0; row--)
{
y = 0;
for(int col = 0; col < mlx->shot->width; col++)
{
int red = (mlx->tex.img_data[x * w + y] >> 16) & 0xFF;
int green = (mlx->tex.img_data[x * w + y] >> 8) & 0xFF;
int blue = mlx->tex.img_data[x * w + y] & 0xFF;
buf[row * mlx->shot->width_in_bytes + col * 3 + 0] = blue;
buf[row * mlx->shot->width_in_bytes + col * 3 + 1] = green;
buf[row * mlx->shot->width_in_bytes + col * 3 + 2] = red;
y++;
}
x++;
}*/
ft_printf("Taking ScreenShoot....\n");
FILE *fout = fopen("screenshot.bmp", "wb");
ft_printf("ScreenShot Has been saved under The name 'screenshot.bmp']\n");
fwrite(mlx->shot->header, 1, 54, fout);
fwrite((char*)buf, 1, mlx->shot->imagesize, fout);
fclose(fout);
free(buf);
}当我使用for循环时,代码工作得非常好。但是,使用while循环,我会得到一个错误,或者将图像保存为空。
发布于 2020-10-16 08:05:20
要在while循环中使用的变量首先要初始化,然后可以在while循环中使用它,然后编写循环的主体,然后相应地减少/增加变量。
同时循环格式
initialization;
while(condition)
{
body
decrement/increment variable
} 要在for循环中使用的变量可以在循环本身中初始化。
循环格式
for(initialization;condition;decrement/increment)
{
body
} 您刚刚声明了未初始化的变量。只需将int row;更改为int row = mlx->shot->height - 1;
发布于 2020-10-16 07:49:19
只需在开头添加以下一行:
int row = mlx->shot->height - 1;在循环之前,应该检查零值。
发布于 2020-10-16 07:54:50
在这段代码中,您不初始化row。仅仅声明一个变量就会留下一个不确定的内容,这可能是一个非常大的数字,当您将它用作数组索引时,比如在buf[row * mlx->shot->width_in_bytes + col * 3 + 0]中,您将访问数组超出界限,这是一种未定义的行为,通常会导致分段错误。
int row;
while (row >= 0) // row contains an indetermined value here你需要这个:
int row = mlx->shot->height - 1;
while (row >= 0)一般说来,for循环或多或少是一个while循环的语法糖:
for (x;y;z)
{
...
}大致相当于:
x;
while (y)
{
...
z;
}https://stackoverflow.com/questions/64384932
复制相似问题