【问题标题】:how can I switch form for loop to while loop in c?如何在c中将形式for循环切换为while循环?
【发布时间】:2020-10-16 07:43:25
【问题描述】:

我正在尝试用 'while' 循环替换我的 '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++;
     }

这里的输出很完美

我的 while 循环:

    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 循环,我得到一个错误或图像保存为空。

【问题讨论】:

  • int row; -> int row = mlx-&gt;shot-&gt;height - 1;。在您的代码中,您永远不会初始化 row
  • 但不管怎样,for的版本风格更好,没有理由用while循环代替for循环。
  • 我必须用while循环替换它,因为在项目的主题中for循环是被禁止的

标签: c for-loop while-loop


【解决方案1】:

只需在开头添加以下行:

int row = mlx->shot->height - 1;

你应该在循环之前检查零值

【讨论】:

  • @The Night King,那和col 的缺失声明是唯一的区别。如果您对这些修复有疑问,则问题出在其他地方;它与您选择的循环无关。
  • 我开始认为问题出在其他地方。我会再次检查我的代码。谢谢@ikegami
  • @DarkSide77,确保始终启用警告(例如,您可以使用 -Wall -Wextra -pedanticgcc/clang)。此外,-fsanitize=addresss 可用于帮助追查问题。
【解决方案2】:

在此代码中,您不会初始化 row。仅声明一个变量就会使变量的内容不确定,可能是一个非常大的数字,当您将其用作数组索引(如buf[row * mlx-&gt;shot-&gt;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;
}

【讨论】:

    【解决方案3】:

    你想在while循环中使用的变量首先被初始化,然后你可以在while循环中使用它并编写循环体,然后相应地递减/递增变量。

    while循环格式

    initialization; 
    while(condition) 
    { 
    body 
    decrement/increment variable 
    } 
    

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

    for循环格式

    for(initialization;condition;decrement/increment)
    { 
    body 
    }  
    

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

    【讨论】:

    • decrement/increment 是一种特殊情况,你可以做任何你想做的事情,例如:p = p-&gt;next 甚至什么都不做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 2018-03-19
    • 2018-02-22
    • 2017-04-26
    • 2018-08-19
    • 1970-01-01
    相关资源
    最近更新 更多