char array adds gibberish

I’m trying to copy characters from one character array to another in order to later use it to connect to a wifi network.
The problem is that when I create a new character array it is filled with random looking gibberish characters. When I fill the array with the characters I want, there ends up being an array with the characters I want followed by a bunch of gibberish ones.
The weird thing is, with the addition of the gibberish characters the array seems to be longer than its defined length.

Here is my code:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <EEPROM.h>

WiFiClient espClient;
PubSubClient client(espClient); 

void wifiSetup (char* ssid, char* password)
  Serial.print("Connecting to ");
  Serial.print ("with password: ");
  Serial.println (password);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  Serial.println("WiFi connected");

void setup() 
  delay (2000);
  char* ssid= new char [5];
  char* password= new char [11];
  char  ssidname [6]="bitAP";
char  passwordname [11]="0773366069";
delay (3000);
  for (int i=0; i< 5; i++)
    ssid [i]=ssidname [i];
  for (int j=0; j<10; j++)
    password[j]=passwordname [j];
  delay (1000);
  Serial.println (ssid);
  Serial.println (password);
wifiSetup (ssid, password);

void loop() 

Thank you for any ideas or suggestions!

your char arrays need to be '\0' terminated (which means the last char followed must be '\0')

Why do you have to copy the strings? I don't see a reason. Also, why do you need dynamic memory? And if you really do need to copy the strings, why do it manually (and incorrectly, because you don't copy the terminating null character) instead of using the strcpy function?


any ideas or suggestions!

To turn an array of chars into a string, which is what you want, the array has to be terminated by ‘\0’

If you declare a global array of chars and don’t initialise it then it will be full of zeroes but locally declared arrays have random values in them. Arrays of chars declared either way but given constant text will automatically be zero terminated as long as they are large enough to contain the number of characters required, plus 1 for the terminating zero.

With that in mind consider part of your code

  char* ssid = new char [5];  //an array of 5 random pointers to chars

  char  ssidname [6] = "bitAP";  //a zero terminated array of 5 chars with values and a terminating zero
  for (int i = 0; i < 5; i++)
    ssid [i] = ssidname [i];  //copy 6 values from an array with only 5 entries overwriting the terminating zero of the destination array

When strings are in a character array, the end of the string is marked by a zero, a ‘\0’ character. If the ssid is five characters, you need a six-element char array and you need to copy all six characters.

There are standard functions to do these things (and more), and you should make yourself familiar with them.

See avr-libc: <string.h>: Strings. The function you need is named ‘strncpy’.

Thanks!! The array really only needed to be terminated. I added: ssid [10]='/0'; and it works great.

I also wasn't familiar with the strncpy function.

I added: ssid [10]='/0';

Adding '\0' would have been a better idea.

How and where is the ssid array declared and how many elements does it have in it ?