1

Good afternoon,

I'm currently working on an assignment with a custom programming environment using a sublanguage of c++ that lacks some of it's features.

I have written this code:

char comparameEsto(char voy, char vengo, pueblo sitios1, pueblo sitios2, pueblo sitios3,pueblo sitios4){
    bool  encontradoOrigen;
    bool encontradoDestino;
    char copia;
    for(int i=0; i<13;i++){
        strcpy(copia,sitios1[i]);
    }
}

char comparameEsto(char voy, char vengo, pueblo sitios1, pueblo sitios2, pueblo sitios3,pueblo sitios4){
    bool  encontradoOrigen;
    bool encontradoDestino;
    char copia;
    for(int i=0; i<13;i++){
        strcmp(voy,sitios1[i]);
    }
}

It's just an example of the functionality I'm looking for. The thing is, I want to copy the String inside sitios1 to copia, or compare them with voy and vengo, but it keeps telling me this:

invalid conversion from `char' to `char*'

I'm not very fluent in C++ (I'm more used to Java), and I'm having a really hard time understanding what should I do.

5
  • 1
    char is just a single character, not a string. Commented Aug 30, 2020 at 17:30
  • 3
    What is pueblo? Commented Aug 30, 2020 at 17:30
  • 1
    Is the sublanguage missing std::string? Commented Aug 30, 2020 at 17:31
  • We could help you more if you could explain us what are your function's arguments. Commented Aug 30, 2020 at 17:32
  • Hello, I'm sorry I wasn't expecting such a quick answer. @Barmar "pueblo" is a typedef I used to store different Strings, "typedef char pueblo[][30];" I use it to print the parallel value that's stored on an enum, since I can't print from the enum. And yes, I can use string.h in this sublanguage. Commented Aug 30, 2020 at 18:51

3 Answers 3

1

char copia declares just a single character. To declare a string, you have to create an array.

    #define MAX_STRING_SIZE 30

    char copia[MAX_STRING_SIZE];

    for(int i=0; i<13;i++){
        strcpy(copia,sitios1[i]);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks Barmar! I was trying a similar approach in the wrong way, I was trying to directly compare two array using strcmp not realising that I would need to declare a matrix to do it, instead of going for a one dimension array that would allow me to use strcpy. Now I have two ways to procede, one would be to compare copia with another parameter defined by the user and turn a boolean true if it was the same, or directly use strcmp to compare the input(Wich would be stored in char voy[1][20] for example) with the content inside the array. Again, many thanks for the insight.
1

char is just a single character in C (or C++) language, as it would be in other languages like Java.
The main difference between a char and a char * is that a char * is a pointer to a char, meaning like a string to simplify.

Comments

1

As the other answer says, char is a single character, while char* is a pointer to a character (typically, the first character in a string).

String functions like strcmp receive strings, that is, pointers. Trying to send char to a function which receives char* is an error.

To work with single characters, use built-in operators:

  • To copy - use the assignment operator: copia = sitios1[i]
  • To compare - use the comparison operator: voy == sitios1[i]

1 Comment

The problem is that I need to compare the whole String stored inside sitios1[] with the ones I recieve from "voy" and "vengo", not just a single character, and the book I have barely adress how to do it. Should I make a typedef char voy[15] and typedef char vengo[15] and work with that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.