user48730: Explain in a simple example. How to pass a char* to a function?
#define name1 "ABCD"
#define name2 "EFGH"
#define name3 "HIJK"

char *list[3] = {};

void printList(char *l, int x) {
  for (int i = 0; i < x; i++) {
    Serial.println(l[i]);
  }

}



void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("start");
  list[0] = name1;
  list[1] = name2;
  list[2] = name3;

  printList(list, 3);

}

void loop() {
  // put your main code here, to run repeatedly:

}

I get an error:
/home/guy/Documents/Dropbox/Arduino/sketch_jun12a/sketch_jun12a.ino: In function 'void setup()':
sketch_jun12a:24:13: error: cannot convert 'char**' to 'char*'
   24 |   printList(list, 3);
      |             ^~~~
      |             |
      |             char**
/home/guy/Documents/Dropbox/Arduino/sketch_jun12a/sketch_jun12a.ino:7:22: note:   initializing argument 1 of 'void printList(char*, int)'
    7 | void printList(char *l, int x) {
      |                ~~~~~~^
exit status 1
cannot convert 'char**' to 'char*'


user7508: Just change this:
void printList(char *l, int x) {

to this:
void printList(char **l, int x) {

OK, this deserves some explanation...
In C and C++, when you use (not when you define) an array
identifier, it decays to a pointer to its first element. In
this case,
char *list[3] = {};

is an array of pointers to char. When you use it here:
printList(list, 3);

it decays to a pointer to pointer to char. Thus the double * needed
in the parameter declaration.
Side note: to get really correct C++, you should replace every
instance of char by const char. This is because the characters in
question belong to string literals, and you are not allowed to modify a
string literal at run time.

48730: <p>Explain in a simple example. How to pass a <code>char*</code> to a function?</p>
<pre><code>#define name1 &quot;ABCD&quot;
#define name2 &quot;EFGH&quot;
#define name3 &quot;HIJK&quot;

char *list[3] = {};

void printList(char *l, int x) {
  for (int i = 0; i &lt; x; i++) {
    Serial.println(l[i]);
  }

}



void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println(&quot;start&quot;);
  list[0] = name1;
  list[1] = name2;
  list[2] = name3;

  printList(list, 3);

}

void loop() {
  // put your main code here, to run repeatedly:

}
</code></pre>
<p>I get an error:</p>
<pre><code>/home/guy/Documents/Dropbox/Arduino/sketch_jun12a/sketch_jun12a.ino: In function 'void setup()':
sketch_jun12a:24:13: error: cannot convert 'char**' to 'char*'
   24 |   printList(list, 3);
      |             ^~~~
      |             |
      |             char**
/home/guy/Documents/Dropbox/Arduino/sketch_jun12a/sketch_jun12a.ino:7:22: note:   initializing argument 1 of 'void printList(char*, int)'
    7 | void printList(char *l, int x) {
      |                ~~~~~~^
exit status 1
cannot convert 'char**' to 'char*'
</code></pre>
 7508: <p>Just change this:</p>
<pre class="lang-cpp prettyprint-override"><code>void printList(char *l, int x) {
</code></pre>
<p>to this:</p>
<pre class="lang-cpp prettyprint-override"><code>void printList(char **l, int x) {
</code></pre>
<p>OK, this deserves some explanation...</p>
<p>In C and C++, when you <strong>use</strong> (not when you <em>define</em>) an array
identifier, it <a href="https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay">decays to a pointer to its first element</a>. In
this case,</p>
<pre class="lang-cpp prettyprint-override"><code>char *list[3] = {};
</code></pre>
<p>is an array of pointers to <code>char</code>. When you use it here:</p>
<pre class="lang-cpp prettyprint-override"><code>printList(list, 3);
</code></pre>
<p>it decays to a pointer to pointer to <code>char</code>. Thus the double <code>*</code> needed
in the parameter declaration.</p>
<p><strong>Side note</strong>: to get really correct C++, you should replace every
instance of <code>char</code> by <code>const char</code>. This is because the characters in
question belong to string literals, and you are not allowed to modify a
string literal at run time.</p>
