Pages: [1] 2   Go Down
Author Topic: invalid conversion from 'char' to 'const char'  (Read 1613 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Carpe Diem
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}
Logged

0
Offline Offline
Shannon Member
****
Karma: 130
Posts: 10463
:(){:|:&};:
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Code:
Serial.println(*msg);
Logged

sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 *".
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Carpe Diem
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}
Logged

0
Offline Offline
Shannon Member
****
Karma: 130
Posts: 10463
:(){:|:&};:
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
char note[4] = "Hi!"; 
also you don't have to do const char *msg = note;
you can simply do

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

also this work:

Code:
char *note;
note = "Hi!"; 
Logged

sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Carpe Diem
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 300
Posts: 26210
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Carpe Diem
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

How does

Code:
const char *msg = note;

differ from

Code:
const char msg = note;

Thanks!  smiley
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49048
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
char *note;
strcpy(note, "Hi!");  //  This will fail, if you or lucky, or just corrupt memory
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

0
Offline Offline
Shannon Member
****
Karma: 130
Posts: 10463
:(){:|:&};:
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Carpe Diem
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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! smiley


Code:
#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);
    }
}
Logged

0
Offline Offline
Shannon Member
****
Karma: 130
Posts: 10463
:(){:|:&};:
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Carpe Diem
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Pages: [1] 2   Go Up
Jump to: