Pages: 1 [2]   Go Down
Author Topic: Passing char arrays to a function - best practice  (Read 2781 times)
0 Members and 1 Guest are viewing this topic.
Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1857
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
char duh[] = "string";
sendMsg((const char *)duh);
Logged

Germany
Offline Offline
Faraday Member
**
Karma: 59
Posts: 3071
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
sendMsg("Hello "); // to allow this, we define sendMsg( const char *)
char msg[20] = "variable text";
msg[0] = 'V';
sendMsg(msg);    // this is fine, too
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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



Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://en.wikipedia.org/wiki/Harvard_architecture

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

Pages: 1 [2]   Go Up
Jump to: