differnt arrays output~

This is a discussion on differnt arrays output~ within the C++ Programming forums, part of the General Programming Boards category; Hi, all~ I have 2 arrays which contains chars and integers. To my surprise I get different answer when cout ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    differnt arrays output~

    Hi, all~

    I have 2 arrays which contains chars and integers. To my surprise I get different answer when cout them. Here is the code and output below :

    Code:
    #include <iostream>
    
    void main ()
    {
      int arr_int[] = {1, 2, 3};
      int arr_char[] = "Howdy!";
      cout << "arr_int: " << arr_int << endl;
      cout << "arr_char: " << arr_char << enl;
    }
    
    /* 
    here is the output :
    
    0x22ff60
    Howdy!
    for what reason we couldnt get the contents of int array as what char do ?
    Never end on learning~

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    Lightbulb Arrays

    It has to do with the iostream classes, i think..
    arr_int is a pointer to the first item of the int array, so if you use *arr_int, you should get the output "1".
    You are now just printing the address of the first element.
    When you're getting a readable output from sending the address of the first char array item, this has to be the way iostream handles chars. The logical thing to do in this case, is to print the array until first space or \0

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I think only char arrays are NULL terminated automatically, which is why you can "cout" the whole array, this is what makes them special.
    Couldn't think of anything interesting, cool or funny - sorry.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >cout << "arr_int: " << arr_int << endl;
    arr_int is an array of integers, cout has no special instructions to print the array as a whole, so it assumes you are trying to print the address of the first item, which is what you get. If you wanted the contents of the first item you would use this:

    cout << "arr_int: " << *arr_int << endl;

    >cout << "arr_char: " << arr_char << enl;
    Arrays of char are treated differently because cout will assume that it is a nul terminated string of characters and print every item up to the nul character. If you want the address of the first item or the contents of the first item, you have to say so explicitly:

    cout << "arr_char: " << &arr_char << endl;
    or
    cout << "arr_char: " << *arr_char << endl;

    Also, your example program has quite a few errors:

    >void main ()
    You've been told this before, several times. void main is undefined, do not use it.

    >int arr_char[] = "Howdy!";
    This needs to be an array of char for the initialization to a string literal to work.

    >cout << "arr_int: " << arr_int << endl;
    Unless you are using a weird compiler, you need to either place some using statements or prefix cout and endl with std:: to account for namespaces.

    >cout << "arr_char: " << arr_char << enl;
    endl is spelled wrong.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Ok, I'm splitting hairs here, but I'm pretty sure the most recent Bloodshed IDE, which is built on the GNU compiler, still allows use of standard headers without explicitly naming a namespace. Apparently, if no namespace is listed, the compiler defaults to namespace std, just like it will add default constructors, etc., under the appropriate circumstances. Also, many older compilers still in use aren't namespace compliant and are still good (not top of the line, and certainly not up to date, but still good) compilers, unless you are the type who has to have the latest and greatest with everything you use. Should you explicitly use namespaces if your compiler is compliant, yes, just as you should implement your own default constructor. But is the compiler weird if it lets you get by without it? IMO, not necessarily.

    The only array that can be handled by the << operator as a whole entity is a null terminated character array, that is, a string. You could wrap the array in a class and then overload the << operator for the class so << will have that type of functionality if you really wanted to. But that's probably a little further down the learning curve.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    If you don't mind using unnecessary non-standard features then I'm not stopping you, but I use and encourage standard C++ wherever possible because platform portability is one of C++'s greatest strengths.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. My Arrays!!!, My Arrays!!! Help!?
    By llcool_d2006 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 12:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21