1

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!

  • Can you identify line 77 please? It'll be a lot easier than making everyone dig through the code to discover the error. – Cliff B Jan 18 '19 at 19:55
  • (Line 77) return out; Thanks for your suggestion! – faedx Jan 18 '19 at 21:05
2
    string out[n];

You've declared an array of strings with this declaration. What you probably want is one string (ie, an array of chars). Change that to char out[n]; to get one string.

  • Thanks curiouskiwi. I've just tried to apply the type char to out and modified out assignments (out[i] = (char)&c_i; and out[i] = lett_x), but I'm still getting an error: test1.c:77:12: error: incompatible pointer to integer conversion returning 'char [n]' from a function with result type 'int' [-Werror,-Wint-conversion] return out; I'm starting to think this kind of returning (a char or either a string) is somehow problematic... If I print 'out' (for each i) I'm getting the program works, (it shows ciphed letters), but if I try to return out I'm getting an error... – faedx Jan 19 '19 at 18:55
  • Well, I would suggest that you've really overcomplicated the problem. – curiouskiwi Jan 19 '19 at 21:52
  • Thanks, problem solved! I think you were right – faedx Feb 6 '19 at 21:53
  • Glad to hear @faedx. Can you please accept my answer to remove this from the open queue? thanks! – curiouskiwi Feb 7 '19 at 3:38

You must log in to answer this question.

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