Invalid conversion from *byte to *char

Thread Starter

zazas321

Joined Nov 29, 2015
936
I am trying to pass a *byte type variable to a function which uses *char type variable. However, I have no luck doing that
I am using Arduino IDE and programming ESP32 devices

This is the function to which I am trying to pass a char* payload ( It is JSON type string which I need to parse)
Code:
void parse_item_inside(char* payload){

    cJSON* jsonObj = cJSON_Parse((char*)(payload));
  // Get pointer to specific field in JSON object
    cJSON* item = cJSON_GetObjectItem(jsonObj, "item");
    cJSON* serial = cJSON_GetObjectItem(jsonObj, "serial");
    cJSON* quantity = cJSON_GetObjectItem(jsonObj, "quantity");
    if (item != NULL)
    {
     //Get integer value - other fields allow access to strings, floats etc.
    strcpy(item_inside.item,item->valuestring);
    strcpy(item_inside.serial,serial->valuestring);
    item_inside.quantity = quantity->valueint;
    OLED_display(item_inside.quantity);
    }

// Delete JSON object (don't forget this)
  cJSON_Delete(jsonObj);

    }

This payload variable comes from this function:
Code:
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");

  char message[length + 1];


  strncpy (message, (char*)payload, length);

  message[length] = '\0';
  Serial.println(message);

if (strcmp(topic,item_inside_topic) == 0){ //if number_to_pick received, means the complecataion has been scanned and initiate the pick_to_light

    parse_item_inside(payload);
  }
}
The error that I am getting:
Code:
invalid conversion from 'byte* {aka unsigned char*}' to 'const char*' [-fpermissive]
 

WBahn

Joined Mar 31, 2012
29,979
Because strcpy expects a char* type and not a byte type i believe
Yet you were able to pass a byte * type to it by casting it to a char * type.

What does that suggest when trying to pass a byte * type to a function called parse_item_inside() that expects a char * type as the argument?
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
Yet you were able to pass a byte * type to it by casting it to a char * type.

What does that suggest when trying to pass a byte * type to a function called parse_item_inside() that expects a char * type as the argument?
Okay I see. I can just pass it as byte* type to the function and then use cast to char when using strcpy
 

WBahn

Joined Mar 31, 2012
29,979
Okay I see. I can just pass it as byte* type to the function and then use cast to char when using strcpy
You seem to be missing the obvious parallels that are starting you right in the face.

When you used the function strncpy() and you wanted to pass it a variable of type byte * you cast it to a char * because strncpy takes a value of type char *. Doing this things worked just fine.

You now want to use the function parse_item_inside() and you want to pass it a variable of type byte * but it is complaining when you do because it takes a value of type char *.

Do you not see that these two situations are the same and have the same solution?
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
You seem to be missing the obvious parallels that are starting you right in the face.

When you used the function strncpy() and you wanted to pass it a variable of type byte * you cast it to a char * because strncpy takes a value of type char *. Doing this things worked just fine.

You now want to use the function parse_item_inside() and you want to pass it a variable of type byte * but it is complaining when you do because it takes a value of type char *.

Do you not see that these two situations are the same and have the same solution?

I am not exactly sure what did you mean, but as I mentioned, passing it to the function parse_item_inside as byte* and then doing a cast to char* when I need to ( strcpy function works just fine)
Code:
void parse_item_inside(byte* payload){

    cJSON* jsonObj = cJSON_Parse((char*)(payload));
....
...
.....
Is there are problem with doing it that way?
 

WBahn

Joined Mar 31, 2012
29,979
You make the following call:

parse_item_inside(payload);

Look at the declaration for the function being called:

void parse_item_inside(char* payload)

This tells the compiler that when you call this function, the argument passed to it should be a pointer to an char. If you pass it some other type of value, the compiler will assume that you have messed up and throw an error.

This is the same situation you had when you made the following call:

strncpy (message, (char*)payload, length);

The second argument is expected to be a pointer to a char. If you had just done:

strncpy (message, payload, length);

The compiler would have thrown the same error, namely

invalid conversion from 'byte* {aka unsigned char*}' to 'const char*' [-fpermissive]

because the compiler sees the attempt to pass a pointer to a byte instead of a pointer to a char as an error and that you really didn't mean to do that.

So you cast the variable payload to an pointer to a char to tell the compiler that this wasn't an error, that you really meant to do that.

So what, pray tell, might you possibly do to your call to parse_item_inside() in order to tell the compiler that passing it a variable that is a pointer to a byte was not a mistake and that you really meant to do that?
 

WBahn

Joined Mar 31, 2012
29,979
Good.

Now go back and read my first response and see if it now makes sense what I was trying to tell you.

You need to be able to understand why you do things certain ways and how to apply that knowledge to new, but similar situations elsewhere.
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
Good.

Now go back and read my first response and see if it now makes sense what I was trying to tell you.

You need to be able to understand why you do things certain ways and how to apply that knowledge to new, but similar situations elsewhere.
I believe I understand now. I cast it to whatever the function expects to get. Since the strncpy expects char* I use cast to tell the function that this will be char* variable. Same with the parse_item_inside function
 

WBahn

Joined Mar 31, 2012
29,979
I believe I understand now. I cast it to whatever the function expects to get. Since the strncpy expects char* I use cast to tell the function that this will be char* variable. Same with the parse_item_inside function
Yes, but you should only cast when you must and only after giving it due consideration to ensure that casting is the correct thing to do. There is no shortage of people that will make whatever cast is required to get the program to compile without stopping to think that the goal isn't to get a program to compile, but to get a program that will function correctly. The reason that compilers through errors for things like this to primarily to bring to the attention of the programmer issues that have little to do with the ability to physically compile a program, but much more likely issues where the programmer has done something stupid and are trying to give the programmer the opportunity to reflect on the error of their ways and make amends. By just making a cast to make the compiler "happy", the most likely outcome is sealing in a logic error into the program that will bite back at the worst time down the road.
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
Yes, but you should only cast when you must and only after giving it due consideration to ensure that casting is the correct thing to do. There is no shortage of people that will make whatever cast is required to get the program to compile without stopping to think that the goal isn't to get a program to compile, but to get a program that will function correctly. The reason that compilers through errors for things like this to primarily to bring to the attention of the programmer issues that have little to do with the ability to physically compile a program, but much more likely issues where the programmer has done something stupid and are trying to give the programmer the opportunity to reflect on the error of their ways and make amends. By just making a cast to make the compiler "happy", the most likely outcome is sealing in a logic error into the program that will bite back at the worst time down the road.
I understood. Thank you very much
 

ci139

Joined Jul 11, 2016
1,898
? what happens if you set the (static globals) *char *byte point to the same address ??

i donno how it's done in C - in particular . . .

see section : Passing Reference Type Variables -- it does not solve your problem but enables to find out any possible translator/compiler specifics
 

WBahn

Joined Mar 31, 2012
29,979
? what happens if you set the (static globals) *char *byte point to the same address ??

i donno how it's done in C - in particular . . .

see section : Passing Reference Type Variables -- it does not solve your problem but enables to find out any possible translator/compiler specifics
The compiler is doing type checking at compile time. It doesn't care whether two pointers point to the same memory location.
 
Top