I mainly do embedded stuff in C but recently, wanting to sink my teeth into something new, I started following a c++ tutorial and i was playing with some code when this thing occurred to me and now i'm a bit confused. Simplifying I used to know that if you want to use an array as a string you declare it with the maximum number of characters you think you'll need plus an extra one for the termination character and that's all the space you're allowed to use from that point on. So, at a certain point in the tutorial series this example comes up:
// example about structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
} mine, yours;
void printmovie (movies_t movie);
int main ()
{
string mystr;
mine.title = "2001 A Space Odyssey";
mine.year = 1968;
cout << "Enter title: ";
getline (cin,yours.title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> yours.year;
cout << "My favorite movie is:\n ";
printmovie (mine);
cout << "And yours is:\n ";
printmovie (yours);
return 0;
}
void printmovie (movies_t movie)
{
cout << movie.title;
cout << " (" << movie.year << ")\n";
}
Everything's ok, it works and i know why. Perfect. Only that to get how things work I decided to fiddle around a bit and wanting to change the way the yours.year struct member is translated from string to int (I like my conversions to be as explicit as possible) I came up with different versions like assigning yours.year directly with cin (since it adapts to the receiving type)
cin >> yours.year;
so this works too, fine. Going on i also made this code which leaves me puzzled because...it works! But I don't know why:
// example about structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
} mine, yours;
void printmovie (movies_t movie);
int main ()
{
mine.title = "2001 A Space Odyssey";
mine.year = 1968;
cout << "Enter title: ";
getline (cin,yours.title);
cout << "Enter year: ";
char mystr[0];
cin >> mystr;
yours.year = atoi(mystr);
cout << "My favorite movie is:\n ";
printmovie (mine);
cout << "And yours is:\n ";
printmovie (yours);
return 0;
}
void printmovie (movies_t movie)
{
cout << movie.title;
cout << " (" << movie.year << ")\n";
}
as you can see I changed the string declaration in favor of an array:
char mystr[0];
cin >> mystr;
yours.year = atoi(mystr);
now the problem is that I declared an array of characters of length 0. At first I made it of length [5] so i expected to get a string of 4 characters (for a year it seemed right) but then i experimented and saw that even an array with a single element could store my 4,6,10 or more digits string! This confuses me as i thought I had not only to declare the length of my array but also to use it within this declared length, else I would get errors. What's going on here? I know i'm getting a string because I have to use atoi() so it's not some obscure and hidden conversion, more so because i declared the array to be of type char! What am I missing?