char arrays and integer arrays... why the difference?

Discussion in 'C++' started by Bill Reyn, Jun 22, 2004.

  1. Bill Reyn

    Bill Reyn Guest

    I am a Java programmer, a newbie with c++, struggling a bit...
    Why does the code below return the starting address for an integer
    array, but for a char array it does not return the starting address,
    rather the actual total char array? Where's the address gone for the
    char array pointer? It was OK for the int array.
    Why the contradiction. What's the logic behind all this? Is it just a
    compiler fudge?


    #include <iostream>
    using namespace std;

    int main()
    {
    char l = 's';
    int num[] = {1,2,3,4,5,6,7,8,9,10};
    char* ch = {"Ritchie did this"};
    cout << " ch is " << ch << " num is " << num << endl ;
    return 0;
    }

    output is:

    ch is Ritchie did this num is 0012F77C
    Press any key to continue

    -----------------------------------
    If your interested in Cplusplus its:
    void SwapInt( int &nA, int &nB)
    {

    int nC;
    nC = nA;
    nA = nB;
    nB = nC;

    }
     
    Bill Reyn, Jun 22, 2004
    #1
    1. Advertising

  2. Bill Reyn

    JKop Guest

    Bill Reyn posted:

    > I am a Java programmer, a newbie with c++, struggling a bit...
    > Why does the code below return the starting address for an integer
    > array, but for a char array it does not return the starting address,
    > rather the actual total char array? Where's the address gone for the
    > char array pointer? It was OK for the int array.
    > Why the contradiction. What's the logic behind all this? Is it just a
    > compiler fudge?
    >
    >
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > char l = 's';
    > int num[] = {1,2,3,4,5,6,7,8,9,10};
    > char* ch = {"Ritchie did this"};
    > cout << " ch is " << ch << " num is " << num << endl ;
    > return 0;
    > }
    >
    > output is:
    >
    > ch is Ritchie did this num is 0012F77C
    > Press any key to continue
    >
    > -----------------------------------
    > If your interested in Cplusplus its:
    > void SwapInt( int &nA, int &nB)
    > {
    >
    > int nC;
    > nC = nA;
    > nA = nB;
    > nB = nC;
    >
    > }
    >


    That's nothing to do with C++ itself. It's to do with cout.

    When cout receives a char*, it pressumes it's a string, hence:

    cout << "Hello!!";

    When it receives any other type of pointer:

    int j;

    cout << &j;

    It'll print the address.

    Here's a solution

    cout << (void*)"Hello!!";

    -JKop
     
    JKop, Jun 22, 2004
    #2
    1. Advertising

  3. Bill Reyn

    Rolf Magnus Guest

    Bill Reyn wrote:

    > I am a Java programmer, a newbie with c++, struggling a bit...
    > Why does the code below return the starting address for an integer
    > array, but for a char array it does not return the starting address,
    > rather the actual total char array? Where's the address gone for the
    > char array pointer? It was OK for the int array.
    > Why the contradiction. What's the logic behind all this? Is it just a
    > compiler fudge?


    No. Arrays of char are the traditional C way of storing strings. This is
    supported in C++ too. Think about it. In your example program, you
    write " ch is " to cout. But " ch is " is an array of char, and you want
    cout to interpret that as a string and print that string. How is the
    compiler supposed to know that it should handle ch differently?
    OTOH, if you supply a pointer to int, there is nothing similar, therefore
    there is no operator<< that takes a pointer to int. There is however one
    that takes a pointer to void, and the pointer to int is converted into
    that. This operator just prints the address that the pointer points to.
    If you want the same to happen for a pointer to char, you have to
    explicitly convert it to a pointer to void:

    cout << " ch is " << static_cast<void*>(ch) << " num is " << num << endl ;
     
    Rolf Magnus, Jun 22, 2004
    #3
  4. On 22 Jun 2004 04:42:42 -0700, (Bill Reyn)
    wrote:

    >I am a Java programmer, a newbie with c++, struggling a bit...
    >Why does the code below return the starting address for an integer
    >array, but for a char array it does not return the starting address,
    >rather the actual total char array? Where's the address gone for the
    >char array pointer? It was OK for the int array.
    >Why the contradiction. What's the logic behind all this? Is it just a
    >compiler fudge?
    >
    >
    >#include <iostream>
    >using namespace std;
    >
    >int main()
    >{
    >char l = 's';
    > int num[] = {1,2,3,4,5,6,7,8,9,10};
    > char* ch = {"Ritchie did this"};


    Braces are not necessary here. Besides, it would be more correct to
    declare ch as const char * (why doesn't BCC 5.5.1 give me a warning
    here??)

    > cout << " ch is " << ch << " num is " << num << endl ;
    > return 0;
    >}
    >
    >output is:
    >
    > ch is Ritchie did this num is 0012F77C
    >Press any key to continue


    The reason lies in the different overloads for operator<< in
    std::eek:stream.

    According to the C++ standard, arrays are implicitly changed to
    pointers; presumably there is no overload for operator<< which takes
    an int*, so the one for void* is used which prints the memory address
    of the pointer. The overload for char*, however, prints the contents
    of the character array up to the delimiting null byte (which is
    implicitly added to a literal string).

    The solution is to cast the [const] char* to a [const] void* (an
    unsigned int should work, too), i.e.:

    cout << " ch is " << reinterpret_cast<void*>(ch) << " num is "
    << num << endl ;


    --
    Bob Hairgrove
     
    Bob Hairgrove, Jun 22, 2004
    #4
    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. wwj
    Replies:
    7
    Views:
    558
  2. wwj
    Replies:
    24
    Views:
    2,520
    Mike Wahler
    Nov 7, 2003
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,996
    Smokey Grindel
    Dec 2, 2006
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,061
    Ian Collins
    May 9, 2006
  5. Replies:
    5
    Views:
    230
    Tassilo v. Parseval
    Nov 22, 2005
Loading...

Share This Page