首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将for循环的表单切换到c中的while循环?

如何将for循环的表单切换到c中的while循环?
EN

Stack Overflow用户
提问于 2020-10-16 07:43:25
回答 3查看 43关注 0票数 0

我试图用一个时间循环替换我的'for‘循环,但是我得到了一个不同的输出。

for循环:

代码语言:javascript
复制
    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循环:

代码语言:javascript
复制
    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

编辑:其余的变量已经声明了。这是孔函数:

代码语言:javascript
复制
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循环,我会得到一个错误,或者将图像保存为空。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-10-16 08:05:20

要在while循环中使用的变量首先要初始化,然后可以在while循环中使用它,然后编写循环的主体,然后相应地减少/增加变量。

同时循环格式

代码语言:javascript
复制
initialization; 
while(condition) 
{ 
body 
decrement/increment variable 
} 

要在for循环中使用的变量可以在循环本身中初始化。

循环格式

代码语言:javascript
复制
for(initialization;condition;decrement/increment)
{ 
body 
}  

您刚刚声明了未初始化的变量。只需将int row;更改为int row = mlx->shot->height - 1;

票数 0
EN

Stack Overflow用户

发布于 2020-10-16 07:49:19

只需在开头添加以下一行:

代码语言:javascript
复制
int row = mlx->shot->height - 1;

在循环之前,应该检查零值。

票数 1
EN

Stack Overflow用户

发布于 2020-10-16 07:54:50

在这段代码中,您不初始化row。仅仅声明一个变量就会留下一个不确定的内容,这可能是一个非常大的数字,当您将它用作数组索引时,比如在buf[row * mlx->shot->width_in_bytes + col * 3 + 0]中,您将访问数组超出界限,这是一种未定义的行为,通常会导致分段错误。

代码语言:javascript
复制
int row;          
while (row >= 0)  // row contains an indetermined value here

你需要这个:

代码语言:javascript
复制
int row = mlx->shot->height - 1;
while (row >= 0)

一般说来,for循环或多或少是一个while循环的语法糖:

代码语言:javascript
复制
for (x;y;z)
{
  ...  
}

大致相当于:

代码语言:javascript
复制
x;
while (y)
{
  ...  
  z;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64384932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档