|
From: <de...@ci...> - 2004-05-28 20:26:51
|
Let's go through your examples one by one.
int nArray[5]={1,2,3,4,5};
This declares an array of 5 ints, and initialises them to the values 1-5.
In terms of storage, the array and all the contents are on the stack.
nArray has type int[5] , i.e. an array of five ints.
cout << nArray << endl;
This passes nArray to the stream functions. The type int[5] easily
converts to int*.
Now the act of moving nArray to cout causes C++ to look for an overloaded
function operator<<(ostream, int*). Now there is no such overloading, so
the pointer is converted to a long, leading the the value of the pointer
being printed.
char sArray[6]={'h','e','l','l','o',\0};
Now the type of sArray is char[6] , i.e. an array of 6 chars.
As before the whole array is on the stack.
cout << sArray << endl;
Now in this case sArray easily converts from char[6] to char*.
There *is* an overloaded function operator<<(ostream, char*) as that's
what's used to print a string ; hence the string is printed.
So that's why the two arrays are handled differently.
char* pszString = "hello";
In this case the string "hello" is put into the static data segment during
compilation, and the initialiser sets the value of the pointer to point to
that stored string.
So there isn't an attempt to assign a long string into a short pointer at
all. In fact you can do this with assignments as well as initialisors
because the expression "hello" returns a char*. So all you're ever doing
is assigning one pointer to another.
char String[] = "hello";
char *pszString2 = String;
Because you've defined an array of characters, it is initialised to the
contents of the string, not a pointer to it. The size of the array is
set to the size of the string assigned, i.e. 6 including the null.
So String is an array of characters exactly as sArray was, and so resolves
to a char* and hence cout << String yields the contents.
And pszString2 is initialised to point to the same address and you get the
same result.
if you were to add the following lines:
cout << static_cast<void*>(sArray) << endl;
cout << static_cast<void*>(pszString) << endl;
cout << static_cast<void*>(String) << endl;
cout << static_cast<void*>(pszString2) << endl;
Then the pointer contents will be printed.
You will see that pszString is a very different value than the other
three, whereas String and pszString2 should be identical.
In article <BAY...@ho...>,
coo...@ho... (Noorez Kassam) wrote:
> I have asked questions about pointers and arrays but all the answers
> are not that great. Maybe I was unclear so I will explain my questions
> better...
> first...
>
> int main()
> {
> int nArray[5]={1,2,3,4,5};
> cout << nArray << endl;/*this gives the address of the array*/
> char sArray[6]={'h','e','l','l','o',\0};
> cout << sArray << endl;/*this give the string hello, but i used the
> name of the array without subscripts? Don't i get the address of it
> like in the int array?*/
>
> char* pszString = "hello";/*strangest of all*/
> /*this looks as if you were trying to put "hello" where the address
> of a char would go.*/
>
> char String[] = "hello";
> char *pszString2 = String;/*looks less confusing because the address
> of the array would get assigned to 'pszString2' because of using the
> name of the array without a subscript, but still is confusing because
> cout the name of the char array without subscripts gives you the
> string*/
>
> _________________________________________________________________
> MSN Premium includes powerful parental controls and get 2 months FREE*
>
> http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU
> =http://hotmail.com/enca&HL=Market_MSNIS_Taglines
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: Oracle 10g
> Get certified on the hottest thing ever to hit the market... Oracle
> 10g. Take an Oracle 10g class now, and we'll give you the exam FREE.
> http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|