1

When I'm converting a string to a char array there are always some weird chars at the end. I have to do it manualy because the convertTochar function crashes my ESP.

  String sssid = module.getCssid(); //String is stored in the eeporm
  char cssid[sssid.length()];
  Serial.println(sssid.length()); //prints correct length of 6
  //convert do ssid
  for( int i = 0; i<sizeof(cssid);i++){
    cssid[i] = sssid[i];
  }
  Serial.println(cssid); //prints myssid⸮?⸮2⸮?
enter code here

If I'm printing the value directly fromm eeprom there are no wired characters. Also the max eeprom size for the string is 10 bytes...

Why are these characters at the end of string?

1 Answer 1

2

You forgot to add one more character to the array to place a \0 (0 value byte) in. This denotes the end of a string. If you forget this, the string will continue until it finds the first 0 byte, which will probably be after the original string copy. This can be dangerous, because it might be in memory that is not containing useful data, and those weird character can be many if there is not a 0 soon or at all.

Note that the 6 bytes you get are the bytes EXCLUDING this 0 byte, so you need to allocate 7 bytes to store a string of length 6.

Instead of the for loop you can use the strcpy function which copies a function including the 0 string.

See description:

Description

The C library function char *strcpy(char *dest, const char *src) copies the string pointed to, by src to dest.

Declaration

Following is the declaration for strcpy() function. char *strcpy(char *dest, const char *src)

Parameters

dest − This is the pointer to the destination array where the content is to be copied.

src − This is the string to be copied.

Return Value

This returns a pointer to the destination string dest.

Example

The following example shows the usage of strcpy() function.

#include <stdio.h>
#include <string.h>

int main () {
   char src[40];
   char dest[100];

   memset(dest, '\0', sizeof(dest));
   strcpy(src, "This is tutorialspoint.com");
   strcpy(dest, src);

   printf("Final copied string : %s\n", dest);

   return(0);
}
3
  • Note that sizeof() only works for statically allocated arrays - that is, arrays that are allocated at compile time with a constant value. Using sizeof() with an array that is sized using a variable (or the return value of a function) will instead result in a pointer to a block of memory allocated on the stack, and sizeof() would return the size of that pointer - 2 on an 8-bit Arduino, or 4 on a 32 bit system like an ESP8266. You need to use strlen() not sizeof().
    – Majenko
    Mar 3, 2018 at 11:24
  • Thank you for the answer! But strcpy only works when you copy char to char not string to char, right? ('strcpy(myChar, mystring)') Making the size of the char array bigger fixed the problem.
    – rusky
    Mar 3, 2018 at 12:16
  • Probably you have to make the string 1 bigger (to store the \0 at the end). Mar 3, 2018 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.