ok, let’s take one line at a time.
char* int2month(int nMonth)
This line is most likely WRONG , because it says that the function returns a pointer to a mutable char (by convention, this will be the first char element of the array). Instead, char const* or const char* should be used as the result type. These two specifications mean exactly the same thing, namely a pointer to a char that you cannot change.
{
This is just the opening bracket of the function body. The body of the function ends on the corresponding closing bracket.
//check to see if value is in rang
This is a comment. The compiler is ignored.
if ((nMonth < 0) || (nMonth > 12)) return "invalid";
Here, the return is executed if and only if the condition in if is satisfied. The goal is to have a predictable way with the wrong argument value. However, the check is probably WRONG , because it allows you to use both the 0 and 12 values, giving a total of 13 real values, while the calendar year has only 12 months.
By the way, technically for the return operator, the specified return value is an array of 8 char elements, namely 7 characters plus zero byte at the end. This array is implicitly converted to a pointer to its first element, which is called the decay type. This particular decay, from a string literal to a pointer to a non-const char , is specifically supported in C ++ 98 and C ++ 03 to be compatible with old C, but not valid in the upcoming C ++ 0x standard.
A book should not learn such ugly things; use const for the result type.
//nMonth is valid - return the name of the month char* pszMonths[] = {"invalid", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
This array initialization again includes this decay. This is an array of pointers. Each pointer is initialized with a string literal, which by type is an array and splits into a pointer.
By the way, the psz prefix is a monster called the Hungarian notation . It was invented for C programming by supporting the help system in the Microsoft Programmer Workbench. In modern programming, this is impractical, but instead, just a simple code reads like gibberish. You really do not want to accept it.
return pszMonths[nMonth];
This indexing has a formal Undefined Behavior , also affectionately known as "UB", if nMonth is a value of 12 because element 12 does not have an array element. In practice, you will get the result of gibberish.
EDIT: oh I didn’t notice that the author put the month name "invalid" on the front, which makes for 13 elements of the array. how to hide the code ... I did not notice it, because it is very bad and unexpected; check for "invalidity" is performed above in the function.
}
And this is the closing bracket of the function body.
Cheers and hth.,