0

I'm following your advice guys, but I'm not able to store key as set of integers in array.

int main (int argc, string argv[])
{   
    int n;
    int k[n-1];

    for (int j = 0, n = strlen(argv[1]); j < n; j++)
    {
        if (islower(argv[1][j]))
        {   
            int k[j] = argv[1][j] - 97;
        }
        else if (isupper(argv[1][j]))
        {   
            int k[j] = argv[1][j] - 65;
        }
    printf("%i\n", k[j]); 
    }
}

I'm receiving error message below. What's the problem in my code?

jharvard@ubuntu (~/pset2): make vigenere
clang -ggdb3 -O0 -std=c99 -Wall -Werror    vigenere.c  -lcs50 -lm -o vigenere
vigenere.c:16:17: error: variable-sized object may not be initialized
            int k[j] = argv[1][j] - 97;
                ^      ~~~~~~~~~~~~~~~
vigenere.c:20:17: error: variable-sized object may not be initialized
            int k[j] = argv[1][j] - 65;
                ^      ~~~~~~~~~~~~~~~
2 errors generated.
0

When you do

int k[j] = ...

you're actually declaring a new int array, also named k.

And you're declaring it with a variable size (j), which you cannot do in 'C', therefore the error thrown by clang.

What you were probably trying to do there was accesing one location in the array, so as to store some value in there. That you do without prepending the int type:

k[j] = ...

EDIT:

your first two lines of code are:

int n;
int k[n - 1];

there you're declaring two variables, but initializing none. I.e.: they don't have any values inside yet.

So, when you tell the compiler to allocate n - 1 contiguous chunks of memory, each of size int, and reference them as k, your program failes to compile because the compiler doesn't know yet how much n is.


EDIT:

'C' is pretty literal, and gets compiled on a "line-by-line" fashion. When you're about to allocate memory for a given variable, the compiler needs to know in advance how much memory will such variable utilize.

It's not enough for the compiler "the promise" that such variable will exist. Once, and only once, through the variable's lifetime, will you allocate memory for it. So, in this case, the size of the array needs to be know by the time you're declaring it (allocating memory for it).

If you need to dinamically allocate memory (i.e.: allocate memory at runtime, based on values calculated once the program has already started running) you need to follow another strategy, which you'll learn eventually through the course. But you shouldn't bother so far, since it gets a little more complicated, and you should get confident and become acquainted with a few basic concepts first.

As regards vigenere, fixed-size arrays should do just fine. So, you should just estimate a maximum fixed size your array should have, and define it as such.

5
  • yes, I'm trying to somehow store key, so that it can be accessible when ciphering. I removed prepending int an now have another error: variable 'n' is uninitialized when used here [-Werror,-Wuninitialized]
    – Dauren Z
    Jan 10 '15 at 14:46
  • Well, of course, once you've fixed that error, you'll encounter new ones ;) clang is probably telling you on which line is such error happening. Does that give you a clue? (see my edited answer)
    – abelinux
    Jan 10 '15 at 15:02
  • but I'm planning to fill n by running strlen function
    – Dauren Z
    Jan 10 '15 at 15:48
  • (see edited answer) notice that in order to do what you're trying to do, you're going to need a few more tools than you (most likely) have up to now. Don't rush! you'll learn them through the course eventually, but get comfortable with basic concepts first.
    – abelinux
    Jan 10 '15 at 16:20
  • It finally works, thanks! Now I need to figure out how to link key with plaintext.
    – Dauren Z
    Jan 11 '15 at 6:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .