see simplified code - I'm confused...
#include <stdio.h>
#include <string>
#include <cstdlib> // includes the standard library and overarchs over stdlib.h
using namespace std;
void main()
{
char buffer[10];
string theString;
int i = 997799; //(simplified)
itoa(i,buffer,10);
theString = buffer;
printf("\n string is: %s of length %d \n", theString, theString.length());
printf("\n buffer is: %s of length %d \n", buffer, theString.length());
return;
}
The Output I get is unexpected:
string is: (null) of length 926366009
buffer is: 997799 of length 6
(1) why is the string printing as null?
(2) why is theString.length() not printing properly in the first printf() but is right in the 2nd?
(3) if I set breakpoints in visual studio 'buffer' appears as "997799" while 'theString' appears as {"997799"} - something weird going on here?
Thanks folks! Edit I greatly appreciate the level of detail of the provided answers - they all added clarity and helped me go beyond my issue - many thanks for the time you spent helping out :)