Search code examples
ccs50

Incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *


This is a problem from the Harvard cs50 week 2, https://cs50.harvard.edu/x/2023/psets/2/substitution/.

It is giving me the error

incompatible integer to pointer conversion passing 'char' to parameter of type 'const char* ;

It occurs in line 36 when I try to compile the program. I am using the cs50.dev to run the vs code in browser. From here you can use the <cs50.h> enter image description here

How can I fix it?

#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

int main(int argc, string argv[])
{
    string pt = get_string("plaintext: ");
    int len = strlen(pt);
    string ct;;
    string key = argv[1];
    int lenkey = strlen(key);
    if (lenkey < 26)
    {
        printf("Key must contain 26 characters.");

    }
    else if (argc > 2)
    {
        printf("Usage: ./substitution key");

    }
    else if (argc < 2)
    {
        printf("Usage: ./substitution key");

    }
    string keyu;
    string keyl;
    // Key of uppercase
    for (int i = 0; i < 26; i++)
    {
        if (isupper(key[i]))
        {
            keyu = strcat(keyu, key[i]);
        }
        else if (islower(key[i]))
        {
            keyu = strcat(keyu, toupper(key[i]));
        }
    }
    // key of lowercase
    for (int i = 0; i < 26; i++)
    {
        if (islower(key[i]))
        {
            keyl = strcat(keyl, key[i]);
        }
        else if (isupper(key[i]))
        {
            keyl = strcat(keyl, toupper(key[i]));
        }
    }
    // create cyphertext
    for (int i = 0; i < len; i++)
    {
        if (islower(pt[i]))
        {
            ct = strcat(ct, keyl[pt[i] - 97]);
        }
        else if (isupper(pt[i]))
        {
            ct = strcat(ct, keyu[pt[i] - 65]);
        }
        else
        {
            ct = strcat(ct, pt[i]);
        }
    }
}

Solution

  • As noted in the good comments, the main issue for the error is that the "strcat" function is set up to concatenate one character array (string) to another character array (string). In your code, it is attempting to concatenate a character to a character array.

    Following is a refactored version of your program that builds the encrypted version of a word based upon an encryption key.

    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])                        /* Usual declaration with input parameters              */
    {
        if (argc != 2)
        {
            printf("Usage: ./substitution key\n");
            return 1;
        }
        char ct[30], pt[30], key[30], keyl[30], keyu[30];   /* Strings are character arrays                         */
        int len;
        //string pt = get_string("plaintext: ");
        printf("plaintext: ");                              /* Common method to prompt for input                    */
        len = scanf("%s", pt);
        len = strlen(pt);
    
        //string ct;;
        //string key = argv[1];
    
        strcpy(key, argv[1]);                               /* Common string copy method                            */
        int lenkey = strlen(key);
        if (lenkey < 26)
        {
            printf("Key must contain 26 characters.");
            return 2;
        }
    
        //string keyu;
        //string keyl;
    
        // Key of uppercase
        for (int i = 0; i < 26; i++)                        /* Simply store uppercase versions of the characters    */
        {
            keyu[i] = toupper(key[i]);
        }
        // key of lowercase
        for (int i = 0; i < 26; i++)                        /* Simply store lowercase versions of the characters    */
        {
            keyl[i] = tolower(key[i]);
        }
        // create cyphertext
        for (int i = 0; i < len; i++)
        {
            if (islower(pt[i]))
            {
                ct[i] = keyl[pt[i] - 97];
            }
            else if (isupper(pt[i]))
            {
                ct[i] = keyu[pt[i] - 65];
            }
            else
            {
                ct[i] = pt[i];
            }
        }
    
        printf("cypher text: %s\n", ct);
    
        return 0;
    }
    

    Some key points to this refactored code are:

    • Since I do not have the "CS50" libraries and files installed on my system, code specifically utilizing functions and definitions within that context were commented out and generic definitions were used instead (e.g. instead of defining "string pt", pt is defined as a character array).
    • Building uppercase and lowercase encryption keys did not really need to have lowercase or uppercase testing, so that made those "for loops" simpler.
    • Instead of utilizing the "strcat" function, each character in the encrypted text is built character by character utilizing the key parameter.

    Following were some tests of the refactored code.

    craig@Vera:~/C_Programs/Console/Substitution/bin/Release$ ./Substitution zyxwvytsrqponmlkjihgfedcba
    plaintext: checkers
    cypher text: xsvxpvih
    craig@Vera:~/C_Programs/Console/Substitution/bin/Release$ ./Substitution zyxwvytsrqponmlkjihgfedcba
    plaintext: xsvxpvih
    cypher text: checkers
    craig@Vera:~/C_Programs/Console/Substitution/bin/Release$ ./Substitution Hello there
    Usage: ./substitution key
    craig@Vera:~/C_Programs/Console/Substitution/bin/Release$ 
    

    In no way am I denigrating the "CS50" support library and files. Utilize the functions as you see fit in your studies. But as you evaluate code from other sources, you will probably see items such as character array definitions and other variable definitions more likely in this common format. So you will want to also be comfortable with code built in that fashion.