My function code looks like this:

int * kToInt(string k) {
    int kE[strlen(k)];
    // Convert k to chiffer value
    for (int i = 0; i < strlen(k); i++) {
        // Lowercase
        if (k[i] >= 97 || k[i] <= 122) {
            kE[i] = (k[i] % 26) -18;
        // Uppercase
        else if (k[i] >= 65 || k[i] <= 90) {
            kE[i] = (k[i] % 26) - 12;

    return kE[];

And returns the error:

vigenere.c:80:15: error: expected expression
    return kE[];

I have tried changing the return kE[]; statement to return kE; That resulted in the error:

vigenere.c:80:12: error: address of stack memory associated with local variable 'kE' returned
    return kE;

Could anyone please explain what the error codes mean, and what I am doing wrong.

Thank you.


when returning a variable of any kind, you write

return <variable name>;

where <variable name> is the name of the variable. for example, if I have a variable called x, I may return it as follows:

return x;

that problem with the array is that you created it inside the function. that means that the memory that was allocated for the array was actually allocated on a location in memory called the stack. the consequences of this is that as soon as the function returns (terminates/ends execution), your array will also go away (aka popped out of the stack) along with all of its elements. so trying to access the elements of that array after the function returns may cause troubles.

the solution is to either pass your array from the caller code to the function as argument and manipulate the array inside the function normally or to use dynamic memory allocation techniques.

if you haven't reached dynamic memory allocation yet, don't worry! you will learn more about it as you proceed with the course!

  • It's important to remember if you return an array created with dynamic memory functions you must manage the memory 100% c will no longer do a thing for you. You must free it before the program ends or you have a memory leak.
    – Nick Young
    Jan 21 '16 at 2:13

You must log in to answer this question.

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