1

I'm starting school again in a few weeks and I'm trying to practice a bit. I want to make a function that takes a word and turns it backwards (word -> drow).

I need to know the length of the word of course. I use another function to determine its size. Don't want to use any built-in functions.

This is the function that determines the length of the word:

int largopal(char * palabra) //Devuelve la cantidad de caracteres de la palabra
{   
    int counter = 0;
    while (palabra[counter] != '\0')
    {
        counter++;
    }
    return counter;
}

And this is the function that turns the word backwards:

void darvuelta(char *pal){
    int c = 0, i;
    const int l = largopal(pal);
    char t[l];

    for (i = 0; i < l; i++){

    }
}

Before going on with my for, I need to know why the compiler is asking me to give "a constant expression" in char t[l] and fix it.

I'm using Visual Studio 2012, the filename extension is c, the project was created as a win32 console application, I'm using Win7 enterprise, let me know if you need more info. First question made here.

Thanks in advance.

Dimas.

Dimas
  • 116
  • 7
  • 2
    note that - although it sounds similar - the keyword "const" is something completely different than a "constant expression" – mfro Mar 19 '14 at 15:25

2 Answers2

4

This is the code at fault:

const int l = largopal(pal);
char t[l];

This is a variable length array because the compiler, at compile-time, does not know what the value of l is.

To fix this problem, you have a few options:

  1. Compile in C99 mode, which will enable variable length arrays to be allocated on the stack.
  2. Allocate the memory on the heap by using malloc() and free().
  3. Don't allocate any space at all, and do the swap in-place.

Personally, I'd recommend option 3.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • So you mean, use a temp char var instead of a temp array... makes sense. – Dimas Mar 19 '14 at 15:12
  • I will try that, but anyway: I have seen similar questions where the replies are "declare your `int x` as `const int x` and it will work" and the OP says "thanks it works". Why is that not working for me...? – Dimas Mar 19 '14 at 15:13
  • 1
    It looks like Visual Studio doesn't support this feature of C99 (They only have a small subset of C99 implemented at all). Most of the linux compilers do contain this feature which is where you're probably hearing about it from. – Bill Lynch Mar 19 '14 at 15:16
  • Also, just to note, C99 means the version of the C language that came out in 1999. The most recent is C11, which is the 2011 version. Visual Studio looks to implement most of C89 (1989), and parts of C99 (1999). So other compilers may have more features than yours. – Bill Lynch Mar 19 '14 at 15:18
  • Const cannot help VLA. I find it hard to believe it actually helped anyone in this particular case. Latest visual studio (2013) implements quite a lot of C99, by the way. – keltar Mar 19 '14 at 15:21
  • @sharth, if by "this feature of C99" you mean variable size arrays, I do not want that - just get the length of the word into "l" and then create an array of "l" size. How do I accomplish this? – Dimas Mar 19 '14 at 15:22
  • @user3438194: "*I have seen similar questions ...*" you might like to link such questions/answers? – alk Mar 19 '14 at 15:23
  • 1
    @user3438194: If you do that on the stack, that is defined as a variable length array. Otherwise, you can allocate it on the heap using `malloc()` and `free()`. `char *t = malloc(l);` – Bill Lynch Mar 19 '14 at 15:24
  • @user3438194: "*I do not want that*" what's the problem with it? – alk Mar 19 '14 at 15:25
  • 1
    @user3438194: Referring your link above: That's C++ not C. And `a` is still a VLA. – alk Mar 19 '14 at 15:30
2

It seems that you are not compiling your code in C99 mode. Variable length arrays are allowed in C99 and latter. Compile your program with option std=c99.
Another problem is that l is not known at compile time.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • I do not want a variable length array, I want a fixed length array. That is why I'm declaring `const int l = largopal(pal);`. Am I doing it wrong? – Dimas Mar 19 '14 at 15:09
  • 2
    It is variable - `const` doesn't making it compile-time constant. In fact, const value could even be changed, although quite tricky. So, anything that is not compile-time constant is variable. – keltar Mar 19 '14 at 15:19