-2

does char myStr[varLength] work in C?

I have the following (code here):

int isVMin(char c){
    return c == 'a' || 
            c == 'e' ||
            c == 'i' ||
            c == 'o' ||
            c == 'u' ||
            c == 'y';
}

int isNum(char c){
    return c>='0' && c<='9';
}

int removeChars(char str[]){ // removes all vowels, returns the digits number
    int slen = strlen(str);
    
    //char* res = malloc(sizeof(char)*slen+1);
    char res[slen + 1]; /// <<<<<<<<<<<<<<<<<<<<<<<<<<<<< DOES IT WORK AS EXPECTED???

    printf("\nthe initial is: '%s'\n", res);
    int numCount = 0, j = 0;
    
    for (int i = 0; i < slen; i++) {
        if (isVMin(str[i]));
            //str[i]= ' ';
        else { 
            res[j++] = str[i];
            if (isNum(str[i]))
            numCount++;
        }
    }
    res[j] = '\0';
    printf("\nthe removed is: '%s'\n", res);
    //str = res;
    return numCount;
}

int main(){
    char phrase[50];
    gets(phrase);
    
    int nb = removeChars(phrase);
    printf("\nLa phrase '%s' contient %d digits", phrase, nb);
    
    return 0;
}

The program compiles and work as expected. However I have doubts if this usage is legal in C...

Joshua
  • 40,822
  • 8
  • 72
  • 132
serge
  • 13,940
  • 35
  • 121
  • 205
  • Did it work as expected when you tried? If you're asking "Does C have variable length arrays?" the answer is "Yes". – tadman Dec 09 '20 at 20:55
  • no, the question is can I use a variable as array size – serge Dec 09 '20 at 20:56
  • 1
    That's precisely what a *variable length array* is. The key is sort of in the name. – tadman Dec 09 '20 at 20:57
  • 1
    OT: Your `isNum` function duplicates the standard [`isdigit`](https://en.cppreference.com/w/c/string/byte/isdigit) function. – Some programmer dude Dec 09 '20 at 20:58
  • 1
    Tip: [`isdigit()`](https://en.cppreference.com/w/c/string/byte/isdigit) exists. – tadman Dec 09 '20 at 20:58
  • 1
    And never ***ever*** use `gets`! It's so dangerous that it has even been removed from the C language specification. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Dec 09 '20 at 21:00
  • Please,don't use "gets",read this:https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used – MED LDN Dec 09 '20 at 21:00
  • 1
    @Someprogrammerdude: It does not actually. One difference is theirs works regardless of the signedness of `char`, whereas the behavior of `isdigit` is not defined when it is passed a negative value other than `EOF`. – Eric Postpischil Dec 09 '20 at 21:41
  • @Someprogrammerdude, in C everything is dangerous. and gets is the only method I know to read a line from console without writing miles of lines of code... – serge Dec 09 '20 at 22:59
  • fgets puts the new line char at the end... is crazy this language... such a thing, read a line of text, so complicated... – serge Dec 09 '20 at 23:11

1 Answers1

2
char res[slen + 1]; /// <<<<<<<<<<<<<<<<<<<<<<<<<<<<< DOES IT WORK AS EXPECTED???

This works starting in C '99.

You're kind of limited as to how big. A rule of thumb would be exceeding 8k is probably not recommended. Note that on Windows, if the whole stack exceeds 1mb, the program will crash. On modern Unix. the same thing happens at a larger limit.

You can't return it though. Just sayin'

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • 1
    Here `strdup()` to get a roughly sized buffer might be a good place to start, then `realloc()` to trim it if that's important. – tadman Dec 09 '20 at 20:59
  • you can't return it... ? what do you mean? – serge Dec 09 '20 at 23:12
  • @Serge: It means don't write `char res[slen + 1]; return res;`. If you need to do something like that you need `malloc()` after all. – Joshua Dec 09 '20 at 23:34
  • why do not return? this language is crazy – serge Dec 09 '20 at 23:50
  • @Serge: This language is not crazy. This language does not do things behind your back and so _cannot implicitly allocate the array on the heap_. The apparent lack of safeguards becomes a really necessary safeguard of its own when doing kernel mode work. – Joshua Dec 09 '20 at 23:52
  • so, inside the function, it does allocate memory(where on the stack?), but outside, it does not (in the heap?) – serge Dec 10 '20 at 00:26
  • @Serge: Correct about inside. The array goes onto the stack. If you want to live after the function returns, you must allocate it on the heap yourself. – Joshua Dec 10 '20 at 00:27