Thread: Please Help Me A Bit

  1. #1
    Unregistered
    Guest

    Please Help Me A Bit

    i got a question about "cout"

    Program 1:
    #include <iostream.h>

    int main()
    {
    char nx[]="hello";

    cout << nx << "\n";
    return 0;
    }

    Program 2:
    #include <iostream.h>

    int main()
    {
    int nx[]={1,2,3,4,5};

    cout << nx << "\n";
    return 0;
    }


    Why in program 1,the output result is "hello"
    but program 2 output the memory address?

  2. #2
    Registered User Unreg1stered's Avatar
    Join Date
    Jul 2002
    Posts
    25
    Let me try to answer, and if someone find this is wrong, please correct me.

    In the first example, nx is a c string. Therefore when you cout nx, you'll see the string "hello". However, in the second example, nx is an array of integer. When you cout nx, it will print the address of that array, the address of nx[0].
    Last edited by Unreg1stered; 07-20-2002 at 08:50 PM.

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Blame C.

    ostream has an overloaded operator the does special things with char*, for outputting C style strings.

    ostream& operator << (char*)

    While for any other type of pointer, it will simply print the memory address.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Akilla
    Guest

    whow

    You can't cout integer arrays that way..

    you have to insert the element number..
    i.e. nx[1], nx[0], etc

    MY HOMEPAGE www.akilla.tk

  5. #5
    Unregistered
    Guest
    Thx for thr reply ^^
    i got the problem solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit manipulation
    By Neo1 in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2008, 11:53 AM
  2. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Bitwise Operators - Setting and Retreiving a Bit
    By Spono in forum C Programming
    Replies: 2
    Last Post: 11-04-2003, 02:09 PM
  5. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM