String [] and *

Hello everyone it is my first topic in this forum. I have an arduino uno, and i'm programming in C. I trying to make something like this:

String Buttons[] = {“[RESET]”, “[ENTER]”, “[PLAY]”};
…
ButtonMaker(Buttons);
…
void ButtonMaker(String a[]){
b = a;//b it is a String[]
}

but it says :

In void 'ButtonMaker(String*)':
error: incompatible types in assignment of 'String*' to 'String [0]'

b is a string and a is an array of String.

TheLeandroNex:
Hello everyone it is my first topic in this forum.
I have an arduino uno, and i’m programming in C.
I trying to make something like this:

String Buttons[] = {“[RESET]”, “[ENTER]”, “[PLAY]”};

If you are new to C and Arduino programming, you’d better forget about “String” objects.

The ‘strings’ that are typically used with C/C++ programs is a ‘char array’, also named “C-string” or “null-terminated string”. Declared as char-array:

char myString[]="This is a C-string";

If you start learning about “String” objects instead, you will soon notice, that these “String” objects

  • are not compatible to the default GCC library (AVR LIBC)
  • absence of many features needed for string handling
  • much too high RAM usage (a UNO hast just 2048 Bytes of RAM)
  • much slower program execution than possible

So you’d better ban ‘String’ class type from all your Arduino programs before you run into troubles.

What gets passed into your ButtonMaker() function is not an array, but a pointer to the type String. It's not intuitive, I know, because the syntax makes it appear that it is an array that is getting passed. But it would be inefficient to copy the entire array when calling the function, so instead the language allows you to use the syntax you are using but in reality all that is passed to the function is a pointer to the first element in the array (which is really just as good as an array).

When you first learn C/C++, you may get confused by the difference between an array that is created using a pointer versus one that is created as a simple array on the stack (if you don't know what the stack and heap are, you'll want to learn about that too, though I'll give you a simple tip: when programming on Arduino, avoid heap allocations at all costs due to heap fragmentation issues).

Here is what it looks like when creating an array using a pointer, which allocates memory on the heap (remember, don't do this on Arduino):

int* nums = new int[10];

Here is an array on the stack:

int nums[10];

Either one of these could be passed into a function with this signature:

void DoArrayStuff(int numberArray[]);

Or this signature too, which is perhaps more honest about what is being passed:

void DoArrayStuff(int* numberArray);

In your case, you will need to manually loop through the array and do the copying or use the C function memcpy to do the work. But you'll need one more piece of information to do that. Can you guess what that is?

Tony

PS Stroustrup would be upset if he heard you say you are programming in C. ...you are programming in C++! :)