string array v.s. int array
-
Hello everyone, I find strange result in the following program. 1. For string array, dereferencing it will result in the string itself, but for int array, dereferencing it will result in the address of the array; 2. When dereferencing it twice (operator **), why the result is always the 1st element in both string array sample and int array sample?
int main() { wchar_t me[] = L"Hello World \n"; wchar_t (*me2_ptr)[14] = &me; wcout << *me2_ptr << endl; // output Hello World wcout << **me2_ptr << endl; // output H int values[] = { 10, 20, 30, 40, 55, 60, 70, 80, 90, 100 }; int (*pval)[10] = &values; wcout << *pval << endl; // output 0x0017f6f0 wcout << **pval << endl; // output 10 return 0; }
thanks in advance, GeorgeThe depicted behavior is not so strange...
George_George wrote:
1. For string array, dereferencing it will result in the string itself, but for int array, dereferencing it will result in the address of the array;
In fact the behavior is quite similar: the name (or the address-of operator
&
applied on the name, remembering past posts... :) ) of theint
array returns the address of the memory containing the integer values as well the name of the character array returns the address of the memory containing the character values; you find a noticeable difference in the output only because a (C
-like) string is nothing more than the pointer to the memory holding the zero-terminated character sequence, hence wheneverwcout
finds a (wide) character pointer, it prints the characters belonging to the pointed memory.George_George wrote:
When dereferencing it twice (operator **), why the result is always the 1st element in both string array sample and int array sample?
Again, nothing strange here. Given a int pointer, say
int *p
, deferencing it means: "take the content of the pointed memory", namely the integer value at addressp
(that coincide with the firstint
element of the array). The same point applies to the character array. :) :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Hi, I'm not exactly sure why you are using
wchar_t (*me2_ptr)[14] = &me;
and its equivalent for the int. However, look at things like this: me2_ptr - is a pointer on a pointer on a wchar_t *me2_ptr - is a pointer on a wchar_t **me2_ptr - is a wchar_t pval - is a pointer on a pointer on a int *pval - is a pointer on a int **pval is a int If you call operator<< on a pointer on a wchar_t it assumes its target is a c-string and delivers the string itself. If you call operator<< on a pointer on a int it just delivers the value of the pointer. This is just the way operator<< is implemented.Thanks Doc, Why *me2_ptr - is a pointer on a pointer on a wchar_t*? I think &me is a pointer to wchar_t, right? Since in the following code, you can see &me is the same as me,
int main() { char buf[] = "Hello World \n"; _ASSERT (buf == &buf); return 0; }
regards, George -
The depicted behavior is not so strange...
George_George wrote:
1. For string array, dereferencing it will result in the string itself, but for int array, dereferencing it will result in the address of the array;
In fact the behavior is quite similar: the name (or the address-of operator
&
applied on the name, remembering past posts... :) ) of theint
array returns the address of the memory containing the integer values as well the name of the character array returns the address of the memory containing the character values; you find a noticeable difference in the output only because a (C
-like) string is nothing more than the pointer to the memory holding the zero-terminated character sequence, hence wheneverwcout
finds a (wide) character pointer, it prints the characters belonging to the pointed memory.George_George wrote:
When dereferencing it twice (operator **), why the result is always the 1st element in both string array sample and int array sample?
Again, nothing strange here. Given a int pointer, say
int *p
, deferencing it means: "take the content of the pointed memory", namely the integer value at addressp
(that coincide with the firstint
element of the array). The same point applies to the character array. :) :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
Hi CPallini, What makes me confused is, in my test program before, the address of a string array should be the same as the string array itself. For example below, buf is the same as &buf.
int main() { char buf[] = "Hello World \n"; _ASSERT (buf == &buf); return 0; }
I think in the code posted in the question, me2_ptr = &me, and it is the same as me, without dereferencing it, if we simply wcout << me2_ptr, result should be the same as wcount << me? Why we need to dereferencing it by operator * in order to get the same output of wcout << me? regards, George -
Hi CPallini, What makes me confused is, in my test program before, the address of a string array should be the same as the string array itself. For example below, buf is the same as &buf.
int main() { char buf[] = "Hello World \n"; _ASSERT (buf == &buf); return 0; }
I think in the code posted in the question, me2_ptr = &me, and it is the same as me, without dereferencing it, if we simply wcout << me2_ptr, result should be the same as wcount << me? Why we need to dereferencing it by operator * in order to get the same output of wcout << me? regards, GeorgeBecause you defined (correctly) it as pointer to an array address (i.e. another pointer), i.e.
*me2_ptr
contains the address ofme
(the same address returned by&me
). Henceme2_ptr
isn't a pointer tow_char
s, but instead a pointer to a pointer ofw_chars
s (added: that turns out to be the same address...). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
modified on Tuesday, December 11, 2007 5:41:54 AM
-
Because you defined (correctly) it as pointer to an array address (i.e. another pointer), i.e.
*me2_ptr
contains the address ofme
(the same address returned by&me
). Henceme2_ptr
isn't a pointer tow_char
s, but instead a pointer to a pointer ofw_chars
s (added: that turns out to be the same address...). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
modified on Tuesday, December 11, 2007 5:41:54 AM
Hi CPallini, If you have Visual Studio at hand, you can debug my sample program, and you can find that me is the same as me2_ptr (you can watch it), so me2_ptr is the same as me, I do not know why *me2_ptr is also the same as me? regards, George
-
Hi CPallini, If you have Visual Studio at hand, you can debug my sample program, and you can find that me is the same as me2_ptr (you can watch it), so me2_ptr is the same as me, I do not know why *me2_ptr is also the same as me? regards, George
You're right.
*me2_ptr
andme2_ptr
both hold the same address (but are different types, this waywcout
behaves differently on them), this probably is related to the fact that dereferencing an array name (i.e.*me2_ptr
) should return the same address returned by the array name itself. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
You're right.
*me2_ptr
andme2_ptr
both hold the same address (but are different types, this waywcout
behaves differently on them), this probably is related to the fact that dereferencing an array name (i.e.*me2_ptr
) should return the same address returned by the array name itself. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
Thanks CPallini, I think you mean, 1. *me2_ptr is value of array type, and wcout is overloaded to print out the string array; 2. me2_ptr is address of array type, and wcount will print the address of array. Right? regards, George
-
Thanks CPallini, I think you mean, 1. *me2_ptr is value of array type, and wcout is overloaded to print out the string array; 2. me2_ptr is address of array type, and wcount will print the address of array. Right? regards, George
Technically the ostream extraction operator
<<
is overloaded, but, poorly speaking the answer is yes. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Hello everyone, I find strange result in the following program. 1. For string array, dereferencing it will result in the string itself, but for int array, dereferencing it will result in the address of the array; 2. When dereferencing it twice (operator **), why the result is always the 1st element in both string array sample and int array sample?
int main() { wchar_t me[] = L"Hello World \n"; wchar_t (*me2_ptr)[14] = &me; wcout << *me2_ptr << endl; // output Hello World wcout << **me2_ptr << endl; // output H int values[] = { 10, 20, 30, 40, 55, 60, 70, 80, 90, 100 }; int (*pval)[10] = &values; wcout << *pval << endl; // output 0x0017f6f0 wcout << **pval << endl; // output 10 return 0; }
thanks in advance, GeorgeCpallini has already explained at some length (to me too!) how the address of an array has the same value for convenience to printf (and it's relations). The two bits of code you've shown do the same thing with derefencing. The only difference is that the wcout is clever enough to go "oh, I won't give a number, I'll give a lump of text - aren't I helpful?". The short answer to question 2 BECAUSE. Longer answer... Because it has been defined that way by people cleverer than us. Is this feature causing you problems? Iain.
-
Cpallini has already explained at some length (to me too!) how the address of an array has the same value for convenience to printf (and it's relations). The two bits of code you've shown do the same thing with derefencing. The only difference is that the wcout is clever enough to go "oh, I won't give a number, I'll give a lump of text - aren't I helpful?". The short answer to question 2 BECAUSE. Longer answer... Because it has been defined that way by people cleverer than us. Is this feature causing you problems? Iain.
Thanks Iain, My question is answered. regards, George