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?

[Code]
#include <iostream>
#include <string>

using namespace std;

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;
}
[/Code]


thanks in advance,
George

Re: string array v.s. int array by Brian

Brian
Mon Dec 10 22:21:34 PST 2007


"George" <George@discussions.microsoft.com> wrote in message news:96A9EEF0-72D5-47E3-A635-159869181EC1@microsoft.com...
> 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;

*pval is a pointer to an int array.
*me2_ptr is a pointer to a wchar_t array.

wcout << (pointer to wchar_t array) is conveniently overloaded to print the string for you.

Brian



Re: string array v.s. int array by George

George
Mon Dec 10 22:43:00 PST 2007

Thanks Brian,


What makes me confused is, I think the value of pval variable should be the
value of the address of the int array (and I have used Visual Studio to
verify it is true), dereferencing it (by operator *) should result in the
value of the array, right?

Why wcout << *pval will not result in *value*, but still the address of the
int array?

wcout << *me_ptr2 looks good since it results in the value of the array
("Hello World" printed on console).


regards,
George

"Brian Muth" wrote:

>
> "George" <George@discussions.microsoft.com> wrote in message news:96A9EEF0-72D5-47E3-A635-159869181EC1@microsoft.com...
> > 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;
>
> *pval is a pointer to an int array.
> *me2_ptr is a pointer to a wchar_t array.
>
> wcout << (pointer to wchar_t array) is conveniently overloaded to print the string for you.
>
> Brian
>
>
>

Re: string array v.s. int array by Bo

Bo
Tue Dec 11 02:51:04 PST 2007

George wrote:
:: Thanks Brian,
::
::
:: What makes me confused is, I think the value of pval variable
:: should be the value of the address of the int array (and I have
:: used Visual Studio to verify it is true), dereferencing it (by
:: operator *) should result in the value of the array, right?
::
:: Why wcout << *pval will not result in *value*, but still the
:: address of the int array?

Because it IS ! :-)

int (*pval)[10];

is a pointer to an array of ten ints. So *pval is the array.

::
:: wcout << *me_ptr2 looks good since it results in the value of the
:: array ("Hello World" printed on console).

No, it looks good because there (1) is an overloaded operator<< that
outputs a pointer to wchar_t as a string, and (2) the array decays to
a pointer to its first element. This (1) works for char and wchar_t,
but not for arrays of other types.


Please don't spend to much time in this darkest corner of the C
language. C-style arrays are just crazy, and arrays of characters are
even worse!

Please!


Bo Persson


::
::
:: regards,
:: George
::
:: "Brian Muth" wrote:
::
:::
::: "George" <George@discussions.microsoft.com> wrote in message
::: news:96A9EEF0-72D5-47E3-A635-159869181EC1@microsoft.com...
:::: 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;
:::
::: *pval is a pointer to an int array.
::: *me2_ptr is a pointer to a wchar_t array.
:::
::: wcout << (pointer to wchar_t array) is conveniently overloaded to
::: print the string for you.
:::
::: Brian




Re: string array v.s. int array by George

George
Tue Dec 11 04:34:00 PST 2007

Thanks Bo,


I can not understand why using ** operator on either me_ptr2 or pval will
result in the first element of the array? What is the internal type
conversion which results in the first element?


regards,
George

"Bo Persson" wrote:

> George wrote:
> :: Thanks Brian,
> ::
> ::
> :: What makes me confused is, I think the value of pval variable
> :: should be the value of the address of the int array (and I have
> :: used Visual Studio to verify it is true), dereferencing it (by
> :: operator *) should result in the value of the array, right?
> ::
> :: Why wcout << *pval will not result in *value*, but still the
> :: address of the int array?
>
> Because it IS ! :-)
>
> int (*pval)[10];
>
> is a pointer to an array of ten ints. So *pval is the array.
>
> ::
> :: wcout << *me_ptr2 looks good since it results in the value of the
> :: array ("Hello World" printed on console).
>
> No, it looks good because there (1) is an overloaded operator<< that
> outputs a pointer to wchar_t as a string, and (2) the array decays to
> a pointer to its first element. This (1) works for char and wchar_t,
> but not for arrays of other types.
>
>
> Please don't spend to much time in this darkest corner of the C
> language. C-style arrays are just crazy, and arrays of characters are
> even worse!
>
> Please!
>
>
> Bo Persson
>
>
> ::
> ::
> :: regards,
> :: George
> ::
> :: "Brian Muth" wrote:
> ::
> :::
> ::: "George" <George@discussions.microsoft.com> wrote in message
> ::: news:96A9EEF0-72D5-47E3-A635-159869181EC1@microsoft.com...
> :::: 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;
> :::
> ::: *pval is a pointer to an int array.
> ::: *me2_ptr is a pointer to a wchar_t array.
> :::
> ::: wcout << (pointer to wchar_t array) is conveniently overloaded to
> ::: print the string for you.
> :::
> ::: Brian
>
>
>
>

Re: string array v.s. int array by Igor

Igor
Tue Dec 11 04:57:43 PST 2007

"George" <George@discussions.microsoft.com> wrote in message
news:29DB7086-B5FD-4428-BA15-CF7ABA79F6A8@microsoft.com
> I can not understand why using ** operator on either me_ptr2 or pval
> will result in the first element of the array? What is the internal
> type conversion which results in the first element?

pval is a pointer to an array. *pval is the array itself. The array
"decays" (is implicitly converted, in almost all scenarios) to the
pointer to its first element. *(*pval) thus dereferences the pointer to
the first element of the array, and results in the value of the first
element of the array. *(*pval) can be equivalently written as simply
**pval.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: string array v.s. int array by Bo

Bo
Tue Dec 11 04:59:55 PST 2007

George wrote:
:: Thanks Bo,
::
::
:: I can not understand why using ** operator on either me_ptr2 or
:: pval will result in the first element of the array? What is the
:: internal type conversion which results in the first element?

It is beacuse of the rule that, in most places, an array "decays" into
a pointer. The address of the array is also the address of its first
element.

If you haven't got enough of this already, why don't you try this on:

const char s[] = "Hello World!";

Now, what is the difference between s[0] and 0[s] ? Not much!

The first one evaluates to *(s + 0) and the second one to *(0 + s).
Both have the value 'H'.


Do you still wonder why we recommend using std::string and
std::vector? :-)


Bo Persson


::
:: "Bo Persson" wrote:
::
::: George wrote:
::::: Thanks Brian,
:::::
:::::
::::: What makes me confused is, I think the value of pval variable
::::: should be the value of the address of the int array (and I have
::::: used Visual Studio to verify it is true), dereferencing it (by
::::: operator *) should result in the value of the array, right?
:::::
::::: Why wcout << *pval will not result in *value*, but still the
::::: address of the int array?
:::
::: Because it IS ! :-)
:::
::: int (*pval)[10];
:::
::: is a pointer to an array of ten ints. So *pval is the array.
:::



Re: string array v.s. int array by George

George
Tue Dec 11 05:34:00 PST 2007

Cool and great Igor!


regards,
George

"Igor Tandetnik" wrote:

> "George" <George@discussions.microsoft.com> wrote in message
> news:29DB7086-B5FD-4428-BA15-CF7ABA79F6A8@microsoft.com
> > I can not understand why using ** operator on either me_ptr2 or pval
> > will result in the first element of the array? What is the internal
> > type conversion which results in the first element?
>
> pval is a pointer to an array. *pval is the array itself. The array
> "decays" (is implicitly converted, in almost all scenarios) to the
> pointer to its first element. *(*pval) thus dereferences the pointer to
> the first element of the array, and results in the value of the first
> element of the array. *(*pval) can be equivalently written as simply
> **pval.
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
>
>

Re: string array v.s. int array by George

George
Tue Dec 11 05:36:01 PST 2007

Cool Bo!


I can not believe a program could be written in that way until I tried your
sample. :-)


regards,
George

"Bo Persson" wrote:

> George wrote:
> :: Thanks Bo,
> ::
> ::
> :: I can not understand why using ** operator on either me_ptr2 or
> :: pval will result in the first element of the array? What is the
> :: internal type conversion which results in the first element?
>
> It is beacuse of the rule that, in most places, an array "decays" into
> a pointer. The address of the array is also the address of its first
> element.
>
> If you haven't got enough of this already, why don't you try this on:
>
> const char s[] = "Hello World!";
>
> Now, what is the difference between s[0] and 0[s] ? Not much!
>
> The first one evaluates to *(s + 0) and the second one to *(0 + s).
> Both have the value 'H'.
>
>
> Do you still wonder why we recommend using std::string and
> std::vector? :-)
>
>
> Bo Persson
>
>
> ::
> :: "Bo Persson" wrote:
> ::
> ::: George wrote:
> ::::: Thanks Brian,
> :::::
> :::::
> ::::: What makes me confused is, I think the value of pval variable
> ::::: should be the value of the address of the int array (and I have
> ::::: used Visual Studio to verify it is true), dereferencing it (by
> ::::: operator *) should result in the value of the array, right?
> :::::
> ::::: Why wcout << *pval will not result in *value*, but still the
> ::::: address of the int array?
> :::
> ::: Because it IS ! :-)
> :::
> ::: int (*pval)[10];
> :::
> ::: is a pointer to an array of ten ints. So *pval is the array.
> :::
>
>
>

Re: string array v.s. int array by Carl

Carl
Tue Dec 11 07:47:12 PST 2007

George wrote:
> wchar_t me[] = L"Hello World \n";
...
> int values[] = { 10, 20, 30, 40, 55, 60, 70, 80, 90, 100 };

On top of all of the other fine answers, one thing stood out at me as
something you're possibly unclear on: You do not have an array of string
and an array of int. Rather, you have an array of char and an array of int.

std::ostream "helps" you by supplying an overloaded operator << that, given
a char*, pulls a zero-terminated string from that address, while an int*
sent to an ostream will simply yield the address of the array since there's
no overloaded operator << that accepts an int*, so it matches the overload
for void*. If you were to cast the pointer to your char array to a void*,
thus disabling the overload for char*, you'd get identical treatment for the
two cases:

[Code]
#include <iostream>
#include <string>

using namespace std;

int main()

{
wchar_t me[] = L"Hello World \n";
wchar_t (*me2_ptr)[14] = &me;
wcout << (void*)*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;
}
[/Code]

-cd



Re: string array v.s. int array by Norbert

Norbert
Tue Dec 11 14:00:58 PST 2007

George,

even more strange C code is possible because of this a[b] == b[a] rule.
I have seen code (well, from the IOCCC) like this:

int i = 0;
while (putchar(i++["Hello"]));

C is a strange language ...

Norbert



George schrieb:
> Cool Bo!
>
>
> I can not believe a program could be written in that way until I tried your
> sample. :-)
>
>
> regards,
> George
>
> "Bo Persson" wrote:
>
>> George wrote:
>> :: Thanks Bo,
>> ::
>> ::
>> :: I can not understand why using ** operator on either me_ptr2 or
>> :: pval will result in the first element of the array? What is the
>> :: internal type conversion which results in the first element?
>>
>> It is beacuse of the rule that, in most places, an array "decays" into
>> a pointer. The address of the array is also the address of its first
>> element.
>>
>> If you haven't got enough of this already, why don't you try this on:
>>
>> const char s[] = "Hello World!";
>>
>> Now, what is the difference between s[0] and 0[s] ? Not much!
>>
>> The first one evaluates to *(s + 0) and the second one to *(0 + s).
>> Both have the value 'H'.
>>
>>
>> Do you still wonder why we recommend using std::string and
>> std::vector? :-)
>>
>>
>> Bo Persson
>>
>>
>> ::
>> :: "Bo Persson" wrote:
>> ::
>> ::: George wrote:
>> ::::: Thanks Brian,
>> :::::
>> :::::
>> ::::: What makes me confused is, I think the value of pval variable
>> ::::: should be the value of the address of the int array (and I have
>> ::::: used Visual Studio to verify it is true), dereferencing it (by
>> ::::: operator *) should result in the value of the array, right?
>> :::::
>> ::::: Why wcout << *pval will not result in *value*, but still the
>> ::::: address of the int array?
>> :::
>> ::: Because it IS ! :-)
>> :::
>> ::: int (*pval)[10];
>> :::
>> ::: is a pointer to an array of ten ints. So *pval is the array.
>> :::
>>
>>
>>

Re: string array v.s. int array by George

George
Wed Dec 12 00:05:02 PST 2007

Great cd!


regards,
George

"Carl Daniel [VC++ MVP]" wrote:

> George wrote:
> > wchar_t me[] = L"Hello World \n";
> ....
> > int values[] = { 10, 20, 30, 40, 55, 60, 70, 80, 90, 100 };
>
> On top of all of the other fine answers, one thing stood out at me as
> something you're possibly unclear on: You do not have an array of string
> and an array of int. Rather, you have an array of char and an array of int.
>
> std::ostream "helps" you by supplying an overloaded operator << that, given
> a char*, pulls a zero-terminated string from that address, while an int*
> sent to an ostream will simply yield the address of the array since there's
> no overloaded operator << that accepts an int*, so it matches the overload
> for void*. If you were to cast the pointer to your char array to a void*,
> thus disabling the overload for char*, you'd get identical treatment for the
> two cases:
>
> [Code]
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> int main()
>
> {
> wchar_t me[] = L"Hello World \n";
> wchar_t (*me2_ptr)[14] = &me;
> wcout << (void*)*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;
> }
> [/Code]
>
> -cd
>
>
>

Re: string array v.s. int array by Cholo

Cholo
Wed Dec 12 05:07:45 PST 2007

> C is a strange language ...

...and there are a lot of C/C++ programmers with brain damage, or with unusual
creativity :-P. I've seen a lot of code like your sample :-(


Regards

--
Cholo Lennon
Bs.As.
ARG


"Norbert Unterberg" <nunterberg@newsgroups.nospam> wrote in message
news:eBf#QFEPIHA.6036@TK2MSFTNGP03.phx.gbl...
> George,
>
> even more strange C code is possible because of this a[b] == b[a] rule.
> I have seen code (well, from the IOCCC) like this:
>
> int i = 0;
> while (putchar(i++["Hello"]));
>
> C is a strange language ...
>
> Norbert
>
>
>
> George schrieb:
> > Cool Bo!
> >
> >
> > I can not believe a program could be written in that way until I tried your
> > sample. :-)
> >
> >
> > regards,
> > George
> >
> > "Bo Persson" wrote:
> >
> >> George wrote:
> >> :: Thanks Bo,
> >> ::
> >> ::
> >> :: I can not understand why using ** operator on either me_ptr2 or
> >> :: pval will result in the first element of the array? What is the
> >> :: internal type conversion which results in the first element?
> >>
> >> It is beacuse of the rule that, in most places, an array "decays" into
> >> a pointer. The address of the array is also the address of its first
> >> element.
> >>
> >> If you haven't got enough of this already, why don't you try this on:
> >>
> >> const char s[] = "Hello World!";
> >>
> >> Now, what is the difference between s[0] and 0[s] ? Not much!
> >>
> >> The first one evaluates to *(s + 0) and the second one to *(0 + s).
> >> Both have the value 'H'.
> >>
> >>
> >> Do you still wonder why we recommend using std::string and
> >> std::vector? :-)
> >>
> >>
> >> Bo Persson
> >>
> >>
> >> ::
> >> :: "Bo Persson" wrote:
> >> ::
> >> ::: George wrote:
> >> ::::: Thanks Brian,
> >> :::::
> >> :::::
> >> ::::: What makes me confused is, I think the value of pval variable
> >> ::::: should be the value of the address of the int array (and I have
> >> ::::: used Visual Studio to verify it is true), dereferencing it (by
> >> ::::: operator *) should result in the value of the array, right?
> >> :::::
> >> ::::: Why wcout << *pval will not result in *value*, but still the
> >> ::::: address of the int array?
> >> :::
> >> ::: Because it IS ! :-)
> >> :::
> >> ::: int (*pval)[10];
> >> :::
> >> ::: is a pointer to an array of ten ints. So *pval is the array.
> >> :::
> >>
> >>
> >>



Re: string array v.s. int array by George

George
Wed Dec 12 05:17:04 PST 2007

Hi Norbert,


I have debugged your code and it looks cool!


regards,
George

"Norbert Unterberg" wrote:

> George,
>
> even more strange C code is possible because of this a[b] == b[a] rule.
> I have seen code (well, from the IOCCC) like this:
>
> int i = 0;
> while (putchar(i++["Hello"]));
>
> C is a strange language ...
>
> Norbert
>
>
>
> George schrieb:
> > Cool Bo!
> >
> >
> > I can not believe a program could be written in that way until I tried your
> > sample. :-)
> >
> >
> > regards,
> > George
> >
> > "Bo Persson" wrote:
> >
> >> George wrote:
> >> :: Thanks Bo,
> >> ::
> >> ::
> >> :: I can not understand why using ** operator on either me_ptr2 or
> >> :: pval will result in the first element of the array? What is the
> >> :: internal type conversion which results in the first element?
> >>
> >> It is beacuse of the rule that, in most places, an array "decays" into
> >> a pointer. The address of the array is also the address of its first
> >> element.
> >>
> >> If you haven't got enough of this already, why don't you try this on:
> >>
> >> const char s[] = "Hello World!";
> >>
> >> Now, what is the difference between s[0] and 0[s] ? Not much!
> >>
> >> The first one evaluates to *(s + 0) and the second one to *(0 + s).
> >> Both have the value 'H'.
> >>
> >>
> >> Do you still wonder why we recommend using std::string and
> >> std::vector? :-)
> >>
> >>
> >> Bo Persson
> >>
> >>
> >> ::
> >> :: "Bo Persson" wrote:
> >> ::
> >> ::: George wrote:
> >> ::::: Thanks Brian,
> >> :::::
> >> :::::
> >> ::::: What makes me confused is, I think the value of pval variable
> >> ::::: should be the value of the address of the int array (and I have
> >> ::::: used Visual Studio to verify it is true), dereferencing it (by
> >> ::::: operator *) should result in the value of the array, right?
> >> :::::
> >> ::::: Why wcout << *pval will not result in *value*, but still the
> >> ::::: address of the int array?
> >> :::
> >> ::: Because it IS ! :-)
> >> :::
> >> ::: int (*pval)[10];
> >> :::
> >> ::: is a pointer to an array of ten ints. So *pval is the array.
> >> :::
> >>
> >>
> >>
>