string array v.s. int array

Discussion in 'C++' started by LinLMa@gmail.com, Dec 11, 2007.

  1. Guest

    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;
    }
    

    thanks in advance,
    George
    , Dec 11, 2007
    #1
    1. Advertising

  2. wrote:
    > 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;


    Streams make special accommodations for a pointer to const char
    (or wchar_t). When a char* (wchar_t*) is output, it is presumed
    to point to a C string (a wide C string). Pointers to int do not
    get the same special treatment.

    To bring them both to the common denominator, cast both (*me2_ptr)
    and (*pval) to (void*). You'll see the value of the pointer in
    both cases.

    >
    > 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;
    > }
    > 
    >
    >
    > thanks in advance,
    > George


    HTH

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 11, 2007
    #2
    1. Advertising

  3. James Kanze Guest

    On Dec 11, 6:52 am, wrote:
    > 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>[/color]
    [color=blue]
    > using namespace std;[/color]
    [color=blue]
    > int main()
    > {
    >         wchar_t me[] = L"Hello World \n";[/color]
    
    Here's probably part of your misunderstanding.  This isn't a
    string array, but rather an array of characters (wchar_t).
    [color=blue]
    >         wchar_t (*me2_ptr)[14] = &me;
    >         wcout << *me2_ptr << endl;  // output Hello World
    >         wcout << **me2_ptr << endl; // output H[/color]
    [color=blue]
    >         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[/color]
    [color=blue]
    >         return 0;
    > }
    > 


    ostreams don't support output of an array. On the other hand,
    array's do convert implicitly to pointers, and ostreams do
    support output of pointers. In the case of pointers to
    character types, there is a special overload, which allows them
    to treat the pointer as the beginning of a C style string;
    there's nothing similar for pointers to int, so you just output
    the pointer.

    As for dereferencing twice: that's just the way C (and thus C++)
    works. Arrays are second class citizens in C: formally, there
    is no indexing, but rather pointer arithmetic and
    dereferencing---the expression "a" is defined to be "*(a+i)".
    And of course, *a is the same thing as *(a+0).

    Because of such anomalies, it's better to avoid C style arrays
    (and strings) as much as possible: just use std::wstring and
    std::vector, and things will behave rationally.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Dec 11, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,196
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,612
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    430
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    931
    Jim Langston
    Oct 20, 2005
  5. Alain Spineux
    Replies:
    6
    Views:
    620
    Eric Sosman
    May 17, 2011
Loading...

Share This Page