It depends on what else you are doing. But IMHO concatenating two strings for output is a bad idea. Better output the two strings separately thus avoiding unnecessary use of RAM. Also: if you are using string constants you should learn about progmem.
is exactly the same, and the receiving end will have no idea which method was used. The second uses less memory, is faster, and is far less likely to have issues with overwriting array bounds (if you change the size of the string concatenated, without changing the array size).
I need to concatenate the two strings together because the message is going out to Twitter. In the Tweet I want to have the message and a timestamp. If twitter sees two identical tweets one after the other it will just drop the 2nd one. The timestamp prevents this. I know I could just put millis() or something instead of the time, but that wouldn't change the need to concatenate two strings.
@ScottG: as PaulS confirmed the receiving end will never know that the strings were not concatenated. It does not matter if the receiving end is sending twitter messages or something else. It will never notice the difference.
ScottG:
I need to concatenate the two strings together because the message is going out to Twitter.
No you don't. What goes over the serial port is identical in both cases; all that's changing is the code you use to put that sequence of characters into the transmit buffer.
No you don't. What goes over the serial port is identical in both cases; all that's changing is the code you use to put that sequence of characters into the transmit buffer.
Actually, OP does. The twitting library has one method to post a twit, and that method takes a string.
If I pass a character array to a function, then add to it by using strcat() will my program adjust the memory allocated for this char array, or am I going to run into a memory problem. For example
void createMsg() {
char msgTweet[27];
strcpy(msgTweet, "This is 26 characters long");
sendMsg(msgTweet);
}
void sendMsg(char msgToSend[]){
char addToMsg[] = "stuff";
// add stuff to the message
strcat(msgToSend, addToMsg);
Serial.println(msgToSend);
}
ScottG:
If I pass a character array to a function, then add to it by using strcat() will my program adjust the memory allocated for this char array, or am I going to run into a memory problem.
You are going to run into problems and overwrite whatever happens to be adjacent to your array. Strcat is fairly low level, and it assumes you have already checked that there is enough space allocated in the array to hold the current string plus the new string and the trailing byte. There is the String class that does what you want, but since it uses dynamic memory allocation, it is subject to running out of memory. I tend to be of the opinion that in a memory poor environment like the Arduino you should never use dynamic allocation (or if you must, only use it very sparingly).
ScottG:
If I pass a character array to a function, then add to it by using strcat() will my program adjust the memory allocated for this char array, or am I going to run into a memory problem. For example
strlcat() can be used to prevent writing outside the bounds of the destination buffer. It will just truncate the second string if necessary. Here is the reference for all the C string library functions: avr-libc: <string.h>: Strings
please try to adjust your sendmsg function like this, i believe it should work.
void SendMsg(char * myMsg) // << points to your array and that should be enough, a basic c++ method
{
char MsgAndTime[strlen(myMsg) + 10];
strcpy(MsgAndTime, myMsg);
strcat(MsgAndTime, " 1:00 PM");
Serial.println(MsgAndTime);
}
notice only the main calling method is changed the way you would if calling for an array function variable.
if it doesnt work then arduino isnt c++ compliant but i dont doubt it wouldnt work like this.
notice only the main calling method is changed the way you would if calling for an array function variable.
if it doesnt work then arduino isnt c++ compliant but i dont doubt it wouldnt work like this.
That was pretty much my plan B. But I was going to use (no pointer like in your example ):
void SendMsg(char myMsg)
I have a hard time grasping pointers. I understand how they work, but it's much less intuitive and harder to understand for me. I thought when you passed an array to a function, C always passes it as pointer. Is that right? So what's the difference between these two functions:
Passes SendMsg an unsigned 16-bit address to a signed 8-bit value.
Using the address (pointer) you can access that char, and others by their relative memory positions.
IMO best practice would be to make the original array big enough to hold the time stamp -- if you have to print just one string -- then strcat to that.
You might check but perhaps the twit thing looks for a newline or carriage return to determine end of message rather than timing out serial chars received or however it's supposed to "know" one bunch of print chars from the next -- you can fill the transmit buffer WAY faster than it sends chars out.
If you have a function that takes a const char * pointer, and you have a char array or char pointer, it is valid C/C++ to pass the pointer without cast. Now, if you had a const char * and the function wanted char *, you would have to use a cast in C and a reinterpret_cast in C++ (which Arduino uses).
maybe its more handy to know what pointers are.
suppose that you do something like this
int A = 10;
Then well thats easy a variable A got assigned a value of 10
But computers dont store the whole alphabet, and your "A" is just easy to read and handy to work with.
In reality this "A" is stored somewhere.
Imagine if the arduino memory where a street, each house might contain some value like an "A".
Now the easiest way to retrieve those values would be if someone told you to go to the address of a house and retrieve what it has stored.
This address points you to where it is stored, so POINTERS simply refer to places in memory.
There is a lot of fun that you can do with pointers, because they are not only used for simple variables
One could store also an object in an house, and that object could contain more values, like your array.
But there are even more interesting things you might store in a house
For example you could put in an object that not only contains an some value, but also the address of the next house to visit and the previous house, and maybe even the house address of the first sidelane.
Such more complex objects allow for the creation of database like structures.. In fact databases are made like that.
So from something simple as a pointer, you can get into complex stuff and store any kind of information structure.
Now the speed you get from it is that you only have to refer to something by a pointer and dont have to cast it into another variable for some complex things (like your function).
And well it might look a bit scary, its to good to learn about this topic a bit. There are things you can do with it for wich they are handy.
You can make and use pointers to functions, even arrays/tables of those. Don't know what functions you will run through a process until run-time? Use pointers and you have the flexibility to make dynamic functionality.
OTOH you could set up integer to say which function and then switch-case to hard-coded functions.
I wonder is it possible to store function pointers in PROGMEM? PGM_VOID_P might get them back out....