really stupid c++ question (owned by the char)

Discussion in 'OT Technology' started by Joe_Cool, Oct 23, 2003.

  1. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,395
    Likes Received:
    581
    I'm just messing around brushing up on my basics before I start my C++ class in December, and I'm getting some really weird output.

    It has to be an error on my part, because I don't see how gcc would be making such a stupid error. Here's the deal:

    Code:
    #include <iostream>
    
    using namespace std;
    
    main()
    {
      char a='A';
      short int b = 'A';
      char c[5]="A\0";
    
      cout << "a = " << a << endl;
      cout << "address of a = " << &a << endl;
      cout << "b = " << b << endl;
      cout << "address of b = " << &b << endl;
      cout << "c = " << c << endl;
      cout << "address of c = " << &c << endl;
    }
    
    Seems pretty straightforward to me. :dunno:
    But when I compile and run it, I get this output:

    The problem is only when I'm using a single char variable. As you can see, when I change it to an int or a character array with a string assigned to it, it works fine. It's happening on 2 different machines, both redhat 9.0, gcc-3.2.2-5.

    What can I possibly be doing wrong?
     
  2. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,395
    Likes Received:
    581
    It does the same thing when I compile with Borland C++ Builder 5.0 under Windows XP.

    So what gay mistake am I making when I handle that char?
     
  3. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    use cout.put(a)
     
  4. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,395
    Likes Received:
    581
    Isn't cout.put() for character output? I'm having problems outputting the address of a char variable.

    Actually, I think I figured it out. I tried changing that line to
    cout << "address of a: " << hex << (int)&a << endl;

    and got something resembling sense:

    a = A
    address of a = bfffeb3f
    b = 41
    address of b = 0xbfffeb3c
    c = A
    address of c = 0xbfffeb20

    The only thing is that the leading 0x isn't there. So when I'm using a pointer to char and need the actual address, I need a typecast, even though an address is always an int? Weird.

    Weirder still: Why don't I need to typecast the address to a char[], only a single char?
     
  5. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    ah, sorry, I see the problem now. When you give cout a char*, it assumes it's a pointer to a null terminated string. That's why you get all those garbage characters. It printed out what ever followed 'A' on the stack until it encountered a 0. It appears that only pointers automatically get the 0xHEX format. If that's the case, cast &a to a pointer type, not an just an int. e.g. (int*)&a
     
  6. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,395
    Likes Received:
    581
    Sweet. That did it.

    Thanks a lot.
     

Share This Page