将C代码转换为仅使用goto

yar*_*on0 -4 c goto

我有以下C代码:

void BubbleSort(int a[], int array_size)
{
    int i, j, temp;
    for (i = 0; i < (array_size - 1); ++i)
    {
        for (j = 0; j < array_size - 1 - i; ++j)
        {
            if (a[j] > a[j+1])
            {
                temp = a[j+1];
                a[j+1] = a[j];
                a[j] = temp;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我如何goto仅根据这些代码重写此代码?

mya*_*aut 6

由于C++的语义whileforC语言的语义相比有所改变,我认为可以参考C++标准.它提供了关于如何forwhile可转换的直接规则goto(他们在6.5节中讨论过):

while (T t = x) statement 相当于

label:
{
    T t = x;
    if (t) {
        statement
        goto label;
    }
}
Run Code Online (Sandbox Code Playgroud)

对于

for (for-init-statement; condition; expression) statement 相当于

{
    for-init-statement
    while ( condition ) {
        statement
        expression ;
    }
}
Run Code Online (Sandbox Code Playgroud)

因此它相当于:

{
    for-init-statement
    label:
    if ( condition ) {
        statement
        expression ;
        goto label;
    }
}
Run Code Online (Sandbox Code Playgroud)