Hi, I think this will be an easy problem. In the code below, Setup sets up an array of characters. Then loop tried to change those characters but the printed output has not changed. What am I doing wrong with "myarray [5] = {"XXXXX"};"
Thanks in advance.
char myarray[5] = "12345";
void setup() {
int i = 0;
Serial.begin(9600);
delay (50);
for (i = 0; i < 5; i++)
Serial.print(myarray[i]); // output "12345" ok
Serial.println(" ");
}
void loop() {
int i = 0;
myarray [5] = "XXXXX";
for (i = 0; i < 5; i++)
Serial.print(myarray[i]); // output "12345" not ok, expect "XXXXX"
while (true);
}
If you set "compiler warnings" to all in file → preferences in the IDE, you get the following warnings (that can't be ignored in this case)
C:\Users\sterretje\AppData\Local\Temp\.arduinoIDE-unsaved2023218-1916-47mln0.wtgdd\sketch_mar18a\sketch_mar18a.ino:1:19: warning: initializer-string for array of chars is too long [-fpermissive]
char myarray[5] = "12345";
^~~~~~~
C:\Users\sterretje\AppData\Local\Temp\.arduinoIDE-unsaved2023218-1916-47mln0.wtgdd\sketch_mar18a\sketch_mar18a.ino: In function 'void loop()':
C:\Users\sterretje\AppData\Local\Temp\.arduinoIDE-unsaved2023218-1916-47mln0.wtgdd\sketch_mar18a\sketch_mar18a.ino:18:17: warning: invalid conversion from 'const char*' to 'char' [-fpermissive]
myarray [5] = "XXXXX";
^~~~~~~
The first one tells you what @ToddL1962 mentions above. For the second one, myarray[5] is a character in myarray, not a c-string.
Further in in array with N elements, the index of the last element is N-1 because indices start counting from zero.
It's hard to tell your intention.
You have an int pointer, called 'p', but you are trying to write to what it points to (and because it is, uninitialised, this could be anywhere) the address of a char variable.
Sorry I left out the top line. This code is a simplification of a much larger project that's too big to post here. So I took the main parts of the code to get the same compiler warnings. My goal is to pass a pointer to a character array (filled with ascii values) to another function and either modify or copy elements of the array in that function - and not get any compiler warnings.
Clearly I'm not very familiar with pointers and tried to borrow the ideas from example code and reading and language reference. I feel like it should be something very simple I'm doing wrong but can't figure it out. Code reposted below. Thankyou.
char msgData[10]="12345";
void setup() {
}
//----------------------
void loop() {
int *p;
*p = &msgData[0]; // point to first element of array
mysub(p); // pass pointer to subroutine
}
//----------------------
void mysub(int *pp) {
*pp = 'x'; // modify first element of array
}
/* Compiler Msgs
* C:\Users\donwa\Documents\Arduino\sketch_mar18e\sketch_mar18e.ino: In function 'void loop()':
C:\Users\donwa\Documents\Arduino\sketch_mar18e\sketch_mar18e.ino:10:8: warning: invalid conversion from 'char*' to 'int' [-fpermissive]
*p = &msgData[0]; //error: cannot convert 'char*' to 'int*' in assignment
^~~~~~~~~~~
C:\Users\donwa\Documents\Arduino\sketch_mar18e\sketch_mar18e.ino:10:6: warning: 'p' is used uninitialized in this function [-Wuninitialized]
*p = &msgData[0]; //error: cannot convert 'char*' to 'int*' in assignment
~~~^~~~~~~~~~~~~
*/
Your "msgData" is an array of characters, so if you point to it then you should use "char" pointers.
If you declare "char *p", then the "p" points to a character and "*p" is the contents of what "p" is pointing to.
The name of an array is the label of where the array starts. It is an address, almost the same as a pointer. So you should do: "char * p = msgData;" or if you want to point to the fifth character, then you can do "char * p = &msgData[5];" or "char * p = msgData + 5;".
Thanks. I do understand about the sizes of characters vs integers. I have it working now. I had no idea about different types of pointers - which I'll study up on.
Honestly I thought this would be a cut and dried problem. I'm just interested in passing a pointer to a char array to a subroutine where I will copy or modify the array.