簡體   English   中英

使用C99進行編譯時出現“ ISO C90”錯誤,不允許VLA

[英]Getting 'ISO C90' error not allowing VLA while compiling with C99

我的編譯行:

gcc -Wextra -Wall -Wvla -std=c99 StringChange.c -o StringChange

我的程序:

#include <stdio.h>
#define MAX_STRING_LENGTH 51

char switchChar(char c)
{
    if((c>='A')&&(c<='Z'))
        {
        c = c + 32;

        }
     if((c >='a')&&(c<='z'))
        {
        c = c - 32;
        }
     if(c>'5')
        {
           c = 56;

        }
     if (c<'5')
        {
           c = 48;

        }
    return c;
}

int main(void) 
{   
    char temp;
    int i=0,j=0;
    char stringInput[MAX_STRING_LENGTH];
    printf("Please enter a valid string\n");
    scanf("%s",stringInput);

    while(stringInput[i]!='\0')
        {
            i++;
        }
    char newString[i];
    for(j = 0;j<i;j++)
    {
        temp = switchChar(stringInput[j]);
        newString[j] = temp;
    }
    printf("\"%s\"", stringInput);
    printf("->");
    printf(  "\"%s\"",newString);        

    return 0;
}

編譯時,即使我在命令行中使用C99進行編譯(其他stackexchange問​​題中的解決方案),我仍然收到錯誤消息“ ISO C90禁止使用可變長度數組newString”。

感謝您的幫助和任何其他評論。 謝謝

-Wvla選項提供該診斷。 gcc文檔

-Wvla
如果代碼中使用了可變長度數組,則發出警告。 -Wno-vla防止對可變長度數組使用-Wpedantic警告。

顯然錯誤消息有點誤導。 但是它主要用於VLA使用的舊代碼(在C99之前),其中VLA作為擴展支持。 由於您是在C99模式下進行編譯,因此只需-Wvla選項中刪除-Wvla

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM