Problem changing array

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);
}

try a..

  strcpy(myarray,"XXXXX");

have fun.. ~q

1 Like

It should be myarray[6] because you have to account for the null terminator.

1 Like

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.

Thanks very much. I will look into this more today.

I'm having a problem with pointers and trying to get rid of compile warnings. The code and error msgs are below. Thankyou.

char msgData[16];

void setup() {
}
//-----------------------
void loop() {

  int *p;

  *p = &msgData[0];
  mysub(p);
}
//----------------------
void mysub(int *pp) {

  *pp = 'x';
}
//----------------------
/*
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:10: warning: invalid conversion from 'char*' to 'int' [-fpermissive]
     *p = &msgData[0];
          ^~~~~~~~~~~
C:\Users\donwa\Documents\Arduino\sketch_mar18e\sketch_mar18e.ino:10:8: warning: 'p' is used uninitialized in this function [-Wuninitialized]
     *p = &msgData[0];
     ~~~^~~~~~~~~~~~~
*/


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.

This is why code should have comments.

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
   ~~~^~~~~~~~~~~~~
*/


Why would you point an int pointer at a char variable?

int *p = msgData;

should compile (with warnings) but WHY?
Why not a char pointer?

If you don't know pointers, then you don't have to use them. You can use arrays and indexes.

Replacing a certain character in a text can be like this:

char msgData[16] = "Hello World";

void setup() 
{
  Serial.begin(115200);
  Serial.println(msgData);

  mysub(msgData, 5);

  Serial.println(msgData);
}

void loop() {}

void mysub(char msg[], int index) 
{
  msg[index] = 'x';
}

If you still want to use pointers, then you can do this:

char msgData[16] = "Hello World";

void setup() 
{
  Serial.begin(115200);
  Serial.println(msgData);

  char * p = &msgData[5];
  mysub(p);

  Serial.println(msgData);
}

void loop() {}

void mysub(char *pp) 
{
  *pp = 'x';
}

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

1 Like

this attempts to set the location pointed to by the pointer, p, to the address of msgData [0], like you're trying to do in mysub()

should be

  p = &msgData[0];

to set the pointer value to the address

1 Like

Because I barely know what I'm doing. That's why I'm here.

But should it?
If you do an int write to a what a char pointer points to, you're going to write an int, not a char.

We need to ask the OP for some clarity of intent.

ok, should be

  char *p;

i think it's a fair assumption that the OP is attempting to use a char ptr to overwrite a char within a c-string

There are lots of C/C++ language tutorials on line. Learn at your own pace by studying them carefully.

1 Like

It's an assumption.
Whether or not it is fair, is debatable.
Which is why the intent is important.

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.

Thanks everybody for your responses.

if we don't read into what an OP is trying to do on this forum, there would be no responses. the OP is encouraged to explain

1 Like

Please read post #2

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.

Thanks again everybody.