Offline
Jr. Member
Karma: 0
Posts: 69
Carpe Diem
|
 |
« on: July 28, 2011, 09:02:00 pm » |
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! #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
Tesla Member
Karma: 87
Posts: 8501
:(){:|:&};:
|
 |
« Reply #1 on: July 28, 2011, 09:31:01 pm » |
i think the error is in this line: Serial.println(msg); you are giving a char* to the println, who use it as char. solution: Serial.println(*msg);
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 108
Posts: 6615
|
 |
« Reply #2 on: July 28, 2011, 09:35:42 pm » |
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
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 69
Carpe Diem
|
 |
« Reply #3 on: July 28, 2011, 10:13:34 pm » |
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.. #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
Tesla Member
Karma: 87
Posts: 8501
:(){:|:&};:
|
 |
« Reply #4 on: July 28, 2011, 10:25:03 pm » |
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 char note[4] = "Hi!"; also you don't have to do const char *msg = note; you can simply do Serial.println(note); vw_send((uint8_t *)note, strlen(note));
also this work: char *note; note = "Hi!";
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 69
Carpe Diem
|
 |
« Reply #5 on: July 29, 2011, 12:05:00 am » |
I think I'm still confused..  What does the asterisk do? Does it declare that the variable is an array?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 144
Posts: 19382
I don't think you connected the grounds, Dave.
|
 |
« Reply #6 on: July 29, 2011, 01:56:38 am » |
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.
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 69
Carpe Diem
|
 |
« Reply #7 on: July 29, 2011, 01:43:58 pm » |
thanks for your patience.. But what exactly do you mean by 'pointer'? Would it be possible to give me an example? How does const char *msg = note; differ from const char msg = note; Thanks! 
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 108
Posts: 6615
|
 |
« Reply #8 on: July 29, 2011, 03:32:47 pm » |
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
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 336
Posts: 36486
Seattle, WA USA
|
 |
« Reply #9 on: July 29, 2011, 04:03:35 pm » |
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
Tesla Member
Karma: 108
Posts: 6615
|
 |
« Reply #10 on: July 29, 2011, 04:15:44 pm » |
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: char *note; strcpy(note, "Hi!"); // This will fail, if you or lucky, or just corrupt memory
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 87
Posts: 8501
:(){:|:&};:
|
 |
« Reply #11 on: July 29, 2011, 04:34:51 pm » |
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
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 69
Carpe Diem
|
 |
« Reply #12 on: July 30, 2011, 01:56:59 pm » |
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!  #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
Tesla Member
Karma: 87
Posts: 8501
:(){:|:&};:
|
 |
« Reply #13 on: July 30, 2011, 02:15:01 pm » |
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
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 69
Carpe Diem
|
 |
« Reply #14 on: July 30, 2011, 02:38:19 pm » |
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
|
|
|
|
|
|