I'm trying to solve pset2 vigenere, but I'm having some troubles. I'm using two functions to adapt the key to the text and then to apply the cipher. When I run the last function, I can't get any result out of it, receiving this error:
c:77:12: error: incompatible pointer types returning 'string [n]' from a function with result type 'string' (aka 'char *') [-Werror,-Wincompatible-pointer-types] return out;
In fact, I'm trying to get out of the cipher function an array of chars, and then passing it to the main function which is expecting an array as input.
Can someone help me to tackle and solve this error?
This is the piece of code I wrote for the "cipher function":
string cipher(char* x, char* y)
{
int n = strlen(x);
int c_i;
//declaring output array
string out[n];
//defining some constants used for ASCII conversion
#define A_ascii 65
#define a_ascii 97
#define normal_ascii 25
//Loop: for each char/letter of x:
for(int i = 0; i < n; i++)
{
//defining some variables
char lett_x = x[i];
char lett_y = y[i];
int ascii_x = (int)(lett_x);
int ascii_y = (int)(lett_y);
//if letter UPPERCASE and key letter UPPERCASE, compute and apply the chiper
if(isupper(lett_x) != 0 && isupper(lett_y) != 0)
{
eprintf("UPPERCASE and UPPERCASE\n");
int input_x = ascii_x - A_ascii;
int input_y = ascii_y - A_ascii;
if(input_x + input_y < normal_ascii)
{
c_i = (input_x + input_y)%26 + A_ascii;
}
else if(input_x + input_y > normal_ascii)
{
c_i = (input_x + input_y)%26 - normal_ascii + A_ascii;
}
//write result (ciphed letter) in out
out[i] = (char*)&c_i;
}
//if letter lowercase and key letter UPPERCASE, compute and apply the chiper
if(islower(lett_x) != 0 && isupper(lett_y) != 0)
{
eprintf("lowercase and UPPERCASE\n");
int input_x = ascii_x - a_ascii;
int input_y = ascii_y - A_ascii;
if(input_x + input_y < normal_ascii)
{
c_i = (input_x + input_y)%26 + a_ascii;
}
else if(input_x + input_y > normal_ascii)
{
c_i = (input_x + input_y)%26 - normal_ascii + a_ascii;
}
//write result (ciphed letter) in out
out[i] = (char*)&c_i;
}
//if x[i] is a space or other punctuations, take the original text char (do not apply cipher)
if( strchr(&lett_x,' ') > 0 || strchr(&lett_x,'-') > 0)
{
out[i] = &lett_x;
}
}
return out; //LINE 77
}
Thanks a lot in advance!