简体   繁体   中英

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

My compile line:

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

My program:

#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;
}

When compiling I keep getting the error 'ISO C90 forbids variable length array newString' even though I'm compiling with C99 in the command line (the solution in other stackexchange questions).

Would appreciate your help and any additional comments. Thank you

The -Wvla option provides that diagnostic. From gcc documentation :

-Wvla
Warn if variable length array is used in the code. -Wno-vla prevents the -Wpedantic warning of the variable length array.

Obviously the error message is a bit misleading. But it's mainly used for older code ( before C99) to VLA use where VLAs are supported to as an extension. Since you are compiling in C99 mode, you can simply drop the -Wvla from your commandline option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM