Issue with reading / writing characters in string array

Apologies in advance if this is not the right place for this question please point me in the right direction.

Background:
This is a snippit of a project reading nmea data from a Garmin hiking GPS.
I'm not trying to solve a particular problem here, I'm just trying to understand the current behavour.

I'm trying to understand why the following code returns a different character from what it's set it to.

Any insight here is appreciated.


String nmea = "abcdefg";
int Ascii_Value;

void setup() {
  Serial.begin(4800);
}

void loop() {
  nmea[3] = "L";   // set nmea[3] to "L"
  Serial.println(nmea[3]);   // this should output "L", instead it outputs "#"

  Ascii_Value = nmea[3];   // find ascii value to see what the new character is
  Serial.println(Ascii_Value);   // Ascii_value = 35 for "#", this should be 76 for "L"

  Serial.println();
  Serial.println();
  delay(500);
}// end loop

Hi @Stampmaille,

String nmea = "abcdefg";

is not an array of characters but a String object (see https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/
).

You might want to use an array of char ...

char nmea[] = "abcdefg";

Example sketch:

char nmea[] = "abcdefg";
int Ascii_Value;

void setup() {
  Serial.begin(4800);
  nmea[3] = 'L';   // set nmea[3] to 'L'
  Serial.println(nmea);
}

void loop() {
}

Be aware not to use

 nmea[3] = "L";

as the double quotes are interpreted as a string not a single character.

See here https://www.arduino.cc/reference/en/language/variables/data-types/char/

Character literals are written in single quotes, like this: 'A' (for multiple characters - strings - use double quotes: "ABC").

Edit: @Delta_G's suggestion works also of course: (but see post #5)

char * nmea = "abcdefg";
int Ascii_Value;

void setup() {
  Serial.begin(4800);
  nmea[3] = 'L';   // set nmea[3] to 'L'
  Serial.println(nmea);
}

void loop() {
}
1 Like

That's it!
The issue was using double quotes.
I will also be using a char array from now on.
Thank you for your help!

Although the compiler will complain when you do

    nmea[3] = 'L';
 warning: ISO C++ forbids converting a string constant to 'char*'

In any case you should have a look at arrays of char not to run into other issues ...

Maybe this can be helpful: https://www.aranacorp.com/en/summary-of-strings-with-arduino/

Also this from https://www.arduino.cc/reference/en/language/variables/data-types/array/

 // When declaring an array of type char, you'll need to make it longer
 // by one element to hold the required null termination character:
 char message[6] = "hello";

For further information feel free to check this out also

/*

  Forum: https://forum.arduino.cc/t/issue-with-reading-writing-characters-in-string-array/1198946/2
  Wokwi: https://wokwi.com/projects/383743348014536705 

*/

char nmea[] = "abcdefg";
int lengthOfNmea = sizeof(nmea)/sizeof(nmea[0]);

void setup() {
  Serial.begin(4800);
  nmea[3] = 'L';   // set nmea[3] to "L"
  Serial.println(nmea);
  // Use the function strlen to get the text length
  Serial.print("strlen :\t");
  Serial.println(strlen(nmea));
  // Use the calculation sizeof(nmea)/sizeof(nmea[0]) to get the size in memory
  // allocated by char nmea[] = "abcdefg";
  // It is one char/byte more than the letters above ...
  // because the end of the char string is marked by a zero = '\0'
  // that requires an additional entry
  Serial.print("sizeof-calc :\t");
  Serial.println(lengthOfNmea);

  Serial.print("[ ");
  for (int i=0; i<lengthOfNmea;i++){
    Serial.print(nmea[i],HEX);
    Serial.print(" ");
  }
  Serial.println("  ]");

  Serial.print("[  ");
  for (int i=0; i<lengthOfNmea;i++){
    Serial.print(nmea[i]);
    Serial.print("  ");
  }
  Serial.println("]");


}

void loop() {
}

Results:

abcLefg
strlen :	    7
sizeof-calc :	8
[ 61 62 63 4C 65 66 67 0   ]
[  a  b  c  L  e  f  g     ]

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.