I have the following function:
string encipher(string plaintext, string key)
{
int size = strlen(plaintext);
char ciphertext[size];
// For each alphabetic characters determine what letter it map to
for(int i = 0; i < size; i++ )
{
for(int j = 0; k[j] != plaintext[i]; j++)
{
ciphertext[i] = key[j];
}
}
return ciphertext;
}
Unfortunately when I compile it returns me the following error:
error: address of stack memory associated with local variable 'ciphertext' returned [-Werror,-Wreturn-stack-address] return ciphertext; ^~~~~~~~~~
I tried static
and malloc
but I am not sure to understand how the stack allocation works.