|
From: Nicolas R. <ni...@bo...> - 2004-05-28 03:05:26
|
> I don't inderstand this. It came from a book I am reading
>
> char* pszMonth;
> pszMonth = "January";
>
> how can you put a string in a char pointer. A char like that should
> only hold one character
Your book is using C string conventions. C has no special data type for
character strings; instead they are stored as arrays of characters.
(Indeed, arrays and pointers have lots in common -- hint: the char
pointer is pointing to the 1st char of the array). C provides special
support for character arrays that do not extend to other kinds of
arrays. For example, C provides ways to print them (in printf and other
output statements) and to read them (in scanf and other input
statements). C also provides a number of library routines that compute
the lengths of strings, append two strings, and the like. It also
provides special syntax to "assign" strings to char pointers, which is
where that strange assignment comes to life.
C++ provides higher level abstractions, such as a proper 'string' class
and a 'vector' container class.
I'd advise you to
a) google for: c string pointer array char
(e.g. see http://www.sepsis.za.net/programming_c07.html)
b) google for: c++ string class
(e.g. see http://www.bgsu.edu/departments/compsci/docs/string.html)
c) google for: c++ faq "arrays are evil"
(e.g. see
http://www.parashift.com/c++-faq-lite/containers-and-templates.html)
Good luck!
N.
|