-2

I build an app that uses the Vigenere cipher to encoded words or sentences. It works but everything was contained in main(). I'm rebuilding the app using functions. But I'm getting the error 'Subscripted value is not an array, pointer, or vector' when it appears to me that it is an array.

Error comes from this

if ((int)sentencetoencode[repeatciphercounter] < 65 || ((int)sentencetoencode[repeatciphercounter] > 90 && (int)sentencetoencode[repeatciphercounter] < 97) || (int)sentencetoencode[repeatciphercounter] > 122)
    {
        ciphersentence[repeatciphercounter] = (char)32;

Additional warning for incompatible pointer to integer conversion passing 'char [100]' to parameter type of 'char'

CipherRepeater(sentencetoencode, cipherwordtorepeat, ciphersentence);

Full Code

#include <stdio.h>
#include <string.h>

char CipherRepeater(char sentencetoencode, char cipherwordtorepeat, char ciphersentence);

int main(int argc, char * argv[])
{
// This is a message to encode using Vigenere cipher
char sentencetoencode[100] = "secret message is come over at five";

// Pass a word at command line, then assign that word to cipherwordtorepeat
// This word is the Vigenere encoding key
// Example word: cat
char cipherwordtorepeat[100];

for(unsigned long argvcounter = 0, argvlength = strlen(argv[1]); argvcounter < argvlength; argvcounter++)
{
    cipherwordtorepeat[argvcounter] = argv[1][argvcounter];
}

// Now the word cat needs to be repeated until it matches the length of the message
// Example "secret message is come over at five"
//         "catcat catcatc at catc atca tc atca"
// Declare an array of characters
// Then the function fills the array with the repeating word
char ciphersentence[100];
CipherRepeater(sentencetoencode, cipherwordtorepeat, ciphersentence);

printf("%s\n", ciphersentence);

return 0;
}

char CipherRepeater(char sentencetoencode, char cipherwordtorepeat, char ciphersentence)
{
// Create a counter to make sure to go back to char 0 in the word array when end is reached
int endofwordcounter = 0;
unsigned long cipherwordlength = strlen(&cipherwordtorepeat);

// Take the cipher word and repeat it until it's the same length as the word to encode
for (unsigned long repeatciphercounter = 0, sentencetoencodelength = strlen(&sentencetoencode); repeatciphercounter < sentencetoencodelength; repeatciphercounter++)
{
    // Put a space in the new string in any spot where a non-letter is used
    if ((int)sentencetoencode[repeatciphercounter] < 65 || ((int)sentencetoencode[repeatciphercounter] > 90 && (int)sentencetoencode[repeatciphercounter] < 97) || (int)sentencetoencode[repeatciphercounter] > 122)
    {
        ciphersentence[repeatciphercounter] = (char)32;
    }
    else
    {
        // Copy the character
        ciphersentence[repeatciphercounter] = cipherwordtorepeat[endofwordcounter];
        endofwordcounter++;
        if (endofwordcounter >= cipherwordlength)
        {
            endofwordcounter = 0;
        }
    }
}

return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
James Goldstein
  • 163
  • 1
  • 3
  • 11
  • `char cipherwordtorepeat` means that `cipherwordtorepeat` is a single character (and thus not an array, pointer or vector) – M.M Jun 13 '16 at 04:18
  • 1
    You need to learn the difference between `char`, `char []`, and `char *`. The first is a single character, not a string. The second is an array of characters. The third is a pointer to a character. The character pointed to may be a member of a character array. `char []` can be converted to `char *`, and in fact it is when used in an expression context. – Tom Karzes Jun 13 '16 at 04:27

1 Answers1

3

Your sentencetoencode is declared as

// This is a message to encode using Vigenere cipher
char sentencetoencode[100] = "secret message is come over at five";

but your CipherRepeater is declared as

char CipherRepeater(char sentencetoencode, char cipherwordtorepeat,
                        char ciphersentence);

which accepts char not char*. And you are passing char* to that function which is generating error

incompatible pointer to integer conversion passing 'char [100]' to parameter type of 'char'

So change char to char*.

And also replace strlen(&cipherwordtorepeat) to strlen(cipherwordtorepeat) and do similar to the other strlen in your for loop.

0x0001
  • 525
  • 4
  • 12
  • Ok, that makes sense. But I still have have three of these errors after switching to char*. All in that same if statement above. – James Goldstein Jun 13 '16 at 04:46
  • And my warnings went from 1 to five. Same warning. – James Goldstein Jun 13 '16 at 04:48
  • Can you tell the warnings please? – 0x0001 Jun 13 '16 at 06:06
  • Code: CipherRepeater(sentencetoencode, cipherwordtorepeat, ciphersentence); Gives incompatible pointer to integer conversion passing 'char [100]' to parameter type 'char' – James Goldstein Jun 13 '16 at 06:27
  • Code: unsigned long cipherwordlength = strlen(cipherwordtorepeat); Give incompatible integer to pointer conversion passing 'char' to parameter of type 'const char*' – James Goldstein Jun 13 '16 at 06:29
  • Have you changed parameters from `char CipherRepeater(char sentencetoencode, char cipherwordtorepeat, char ciphersentence)` to `char CipherRepeater(char *sentencetoencode, char *cipherwordtorepeat, char *ciphersentence)`? (Both declaration and definition) – 0x0001 Jun 13 '16 at 06:34
  • Yes, no indicated errors in XCode but it gives Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) – James Goldstein Jun 13 '16 at 06:39
  • Code in above link has an error in `if` statement. Remove `&` from the if statement `((int)&sentencetoencode[repeatciphercounter] > 90` . – 0x0001 Jun 13 '16 at 07:24