Go Down

Topic: invalid conversion from 'char' to 'const char' (Read 1 time) previous topic - next topic

heyarn

Hi Everyone!

I have a very simple task.. Using the code below, I want it to send out a variable's value instead of just "Hello". I've tried to assign a char variable to it, but it won't let me. I get this error:

txVW.cpp: In function 'void loop()':
txVW:11: error: invalid conversion from 'char' to 'const char*'

Would someone be able to explain this to me and show me how to fix it? Thanks so much for your help!



Code: [Select]
#include <VirtualWire.h>

//signal is at digital 12 pin
void setup()
{
Serial.begin(9600);
vw_setup(2000); // Bits per sec
}
void loop()
{
 
const char *msg = "hello";
Serial.println(msg);
vw_send((uint8_t *)msg, strlen(msg));
delay(400);
}

lesto

i think the error is in this line:
Code: [Select]
Serial.println(msg);

you are giving a char* to the println, who use it as char. solution:

Code: [Select]
Serial.println(*msg);
sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

johnwasser

The code you posted compiles just fine.  What is the version of the code that gets the error?

The error is saying that the value you are trying to store in the "const char *" is a char, not a "char *".
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

heyarn

Sorry for the confusion.. The code I originally posted compiles, no problem. But when I edit it to this, the error pops up. Basically, I don't want it to print from a text directly. I want it to print from a variable so that later, I can just put in new values into the variable and see it print the whatever the variable is assigned to..


Code: [Select]
#include <VirtualWire.h>

//signal is at digital 12 pin
void setup()
{
Serial.begin(9600);
vw_setup(2000); // Bits per sec
}
void loop()
{
char note = "Hi!"; 
const char *msg = note;
Serial.println(msg);
vw_send((uint8_t *)msg, strlen(msg));
delay(400);
}

lesto

Code: [Select]
char note = "Hi!"; 

this is wrong, a char can contain only ONE char. here you have 4 char: H, i, ! and '\0' (end string character)

so you should use

Code: [Select]
char note[4] = "Hi!"; 
also you don't have to do const char *msg = note;
you can simply do

Code: [Select]

Serial.println(note);
vw_send((uint8_t *)note, strlen(note));


also this work:

Code: [Select]
char *note;
note = "Hi!"; 
sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

heyarn

I think I'm still confused.. :( What does the asterisk do? Does it declare that the variable is an array?


AWOL

Quote
What does the asterisk do?

In a declaration, it says the variable is a pointer.
In an expression, it dereferences the pointer, and fetches the value pointed at.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

heyarn

thanks for your patience.. But what exactly do you mean by 'pointer'? Would it be possible to give me an example?

How does

Code: [Select]
const char *msg = note;

differ from

Code: [Select]
const char msg = note;

Thanks!  :)

johnwasser

Everything in memory has an address.  Arrays are in consecutive memory addresses.  A pointer is a variable that contains an address.  The pointer can be used to fetch the data at the address.

Rather than copy ann array of characters or a character string we usually pass around a pointer to the first character:

char *myString = "hello";

You can reference a pointer with the '*' operator OR you can use an array index as if it pointed to an array:

char firstChar = *myString;
OR
char firstChar = myString[0];

You can do addition and subtraction on pointers:

char secondChar = *(myString+1);

They are very handy.  You should learn about them.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

PaulS

Quote
also this work:

Code:

char *note;
note = "Hi!"; 

No that will NOT work.

The note variable is defined as a pointer, but it does not point to any space where characters can be stored. Trying to then store characters there will overwrite something that should not be overwritten.

johnwasser


Quote
also this work:
char *note;
note = "Hi!"; 

No that will NOT work.

The note variable is defined as a pointer, but it does not point to any space where characters can be stored. Trying to then store characters there will overwrite something that should not be overwritten.


Yes, it will work.  char * note; note="Hi!"; will declare a character pointer and then point it at the character string "Hi!".  It is functionally equivalent to char * note = "Hi!";

Note that you can't COPY the string into the address pointed to by 'note' since 'note' doesn't point to anything when it is created:
Code: [Select]

char *note;
strcpy(note, "Hi!");  //  This will fail, if you or lucky, or just corrupt memory
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

lesto

that because "Hi!" means a malloc of 4 byte somewhere... probably if the pointer life is "larger" than the "Hi!" one's, something will fail.
sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

heyarn

Thanks for your replies everyone..

So below is my new code. I'm taking in the serial input and assigning it to memory allocation *char.. But it doesn't work.. I thought that the pointer will start on the nth slot, then go to the next based on the nth byte from serial?

Thanks again in advance! :)


Code: [Select]
#include <VirtualWire.h>

//signal is at digital 12 pin
void setup()
{
Serial.begin(9600);
vw_setup(2000); // Bits per sec
}
void loop()
{
  if (Serial.available() > 0)
    {
      const char *incomingByte = Serial.read(); // assign serial input to a memory pointer?
      Serial.println(*incomingByte);
      vw_send((uint8_t *)incomingByte, strlen(incomingByte));
      //delay(400);
    }
}

lesto

do Serial.println(*incomingByte); works? if not because the pointer i spointing to the first buffer address, and the read has eliminated the value.

strlen(incomingByte) won't work: first of all read return only ONE charater, so size should be always 1: BUT you don't have put the final '\0', so the strlen get crazy.
sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

heyarn

should I just create an array, and build a for loop to start assigning each character to the array? I kinda wanted to learn how to do it using pointers though.. Anyone can shed light on this? Thanks!

Go Up