Go Down

Topic: Passing char arrays to a function - best practice (Read 9845 times) previous topic - next topic


char duh[] = "string";
sendMsg((const) duh);

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts


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

char duh[] = "string";
sendMsg((const) duh);

This does not do what do what you think it does.  It would convert duh to a const int, not const char *.  I believe you want:

Code: [Select]

char duh[] = "string";
sendMsg((const char *)duh);


I think you might benefit from one more comment:

sendMsg needs to take a const char* argument.

Correct. You have to define sendMsg to take a const char*, to signal to a user and the compiler that sendMsg won't change the text.

When you did that, you are free to supply const char and variable ones:

Code: [Select]
sendMsg("Hello "); // to allow this, we define sendMsg( const char *)
char msg[20] = "variable text";
msg[0] = 'V';
sendMsg(msg);    // this is fine, too


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

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts



In a Harvard-type uP, code space (i.e. PROGMEM) and data space (i.e. RAM) are different memories.
Therefore, I think function pointers already refer in some way or another to PROGRMEM.

Go Up