0

I'm trying to use a function to change the command line argument into an an alphabetic index (like in caesar cipher) that I can then use, when converting the plaintext into ciphertext. Below's the function:

string  vigenere_key(string s)
{
    int i = 0;
    while (s[i] != '\0')
    {
        if (isupper(s[i]))
        {
            return(s[i] - 65);
            i++;
        }
        if (islower(s[i]))
        {
            return(s[i] - 97);
            i++;
        }
    }
}

and when I run the program it outputs this error:

vigenere2.c:104:19: error: incompatible integer to pointer conversion returning 'int' from a function with result type 'string' (aka 'char *') [-Werror,-Wint-conversion]
            return(s[i] - 65);
                  ^~~~~~~~~~~
vigenere2.c:109:19: error: incompatible integer to pointer conversion returning 'int' from a function with result type 'string' (aka 'char *') [-Werror,-Wint-conversion]
            return(s[i] - 97);
                  ^~~~~~~~~~~
vigenere2.c:113:1: error: control may reach end of non-void function [-Werror,-Wreturn-type]

Stating that I'm making an incompatible integer to pointer conversion. I want the function to take away a (65) or A (97), from each element of the string, depending on if it's a capital or lowercase letter.

2 Answers 2

1

The integer to pointer conversion does not happen at s[i] - 65 (which produces an int), but at return.

You stated that your function returns a string, which is an alias for char*, a pointer to char. Now you try to return an integer instead.

Maybe you meant to do something like printf with that number instead of returning?

2
  • Is a string(char*) not even an array? I don't want to print it, though, I just want it to return a string of altered characters. Chars can be represented by ints though, so it shouldn't matter, or is there something I'm missing?
    – NPetras
    Sep 13, 2017 at 22:16
  • You'll need allocate some additional memory for that if you're not going to change the passed string itself. This could be stack memory in the calling function, passing the pointer to the function as another argument, or by allocating memory on heap using malloc, that one would have to be freed by the calling code after use. In any case, remember that your memory chunk should also keep the null terminator.
    – Blauelf
    Sep 14, 2017 at 14:30
0

Along with Blauef's comments, the code is also returning to the calling function after processing only the first char in the string. Another side effect of the return statements is that i++ never executes. Since the code is being passed the address of the string, the function can simply process the contents of the string. Once done, it could simply return a bool for success or fail, or return void.

3
  • What do I use instead of return then? Is printf, not going to just print the string to the standard output?
    – NPetras
    Sep 13, 2017 at 22:19
  • What are you really trying to do? If you're trying to modify the contents of the string, then just store the modified values back in the string. If you want to store them in a new array, then create the array in main and pass it as a parameter.
    – Cliff B
    Sep 13, 2017 at 22:28
  • I want to modify the contents of the string that is input, how do I store them back in the string? I thought this code was going to do that, and I'm trying to use the function to create a new string k (key), from the argv[1] (command line input, vigenere key input by the user). The piece of code that does that is: string k = vigenere_key(argv[1]).
    – NPetras
    Sep 14, 2017 at 15:12

You must log in to answer this question.

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