cpointerscharcs50incompatibletypeerror

error: incompatible integer to pointer conversion assigning to 'string' (aka 'char *') from 'int' C


This is incomplete code but I'm trying to make copies of argv[1] that are uppercase and lowercase, but get an error message. If it's an inefficient way to go about it in general or completely wrong I would appreciate any tips, but is there a way to fix the error in this specific case?

I'm extremely new to coding as you can tell by the question, so I'm sorry if it's a stupid one, but where might the error be occurring? I realize somehow argv[1] is being converted into an integer but I neither know where nor how to really fix it.

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

int main(int argc, string argv[])
{
    string keyu[26];
    string keyl[26];
    string key = argv[1];

    for (int u = 0; u < strlen(key); u++)
    {
        keyu[u] = toupper(key[u]);
    }

    for (int l = 0; l < strlen(key); l++)
    {
        keyl[l] = tolower(key[l]);
    }

The error it gives out is:

14:17: error: incompatible integer to pointer conversion assigning to 'string' (aka 'char *') from 'int' [-Werror,-Wint-conversion]
keyu[u] = toupper(key[u]);

Solution

  • These declarations

    string keyu[26];
    string keyl[26];
    

    are equivalent to

    char * keyu[26];
    char * keyl[26];
    

    That is they are arrays of pointers.

    This in these statements

    keyu[u] = toupper(key[u]);
    keyl[l] = tolower(key[l]);
    

    you are trying to assign an integer (as for example toupper(key[u])) to a pointer (as for example keyu[u])

    It seems you want actually to declare arrays of characters

    char keyu[26];
    char keyl[26];
    

    Pay attention to that you should append the arrays with the terminating zero character '\0' after the for loops to guarantee that the arrays contain strings.