character pointer worries

This is a discussion on character pointer worries within the C++ Programming forums, part of the General Programming Boards category; this may sound strange, but each time i try to print the address of a character, it prints out the ...

  1. #1
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49

    character pointer worries

    this may sound strange, but each time i try to print the address of a character, it prints out the character.

    for example,

    Code:
         char c='a',*p;
         p=&c;
         cout<<p;
    gives the output 'a'.

    i can get over it using void pointers, but why does the above happen and is there any other way out?
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I think the second line should be:
    Code:
    *p = &c;
    Try that.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    135

    Re: character pointer worries

    Originally posted by kiss_psycho
    why does the above happen and is there any other way out?
    That's usually what you want to happen - after all, we use char *'s for C-style strings, so that's how cout handles it. Casting to a void pointer should be just fine.

  4. #4
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    oh no josh, &c gives an address which can only be stored in a character type pointer. the syntax i wrote is absolutely correct, there must be some problem with the character representation in C++ since i have used tc++ v3.0, borland c++, and vc++ 6.0, all to no avail - they all give the same result.
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  5. #5
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    but omnius, & should produce an address. strange, isnt there any explainable reason behind this?
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Originally posted by kiss_psycho
    but omnius, & should produce an address. strange, isnt there any explainable reason behind this?
    It does produce an address ... of a character. That's what variable p contains. In C++ the output stream handles this as a C-string. That's all there is to it. If you're still not convinced try some simple exercises using C-style strings and pointers and you'll realise what's going on.

  7. #7
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    thanks omni, guess i got confused.
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Originally posted by joshdick
    I think the second line should be:
    Code:
    *p = &c;
    Try that.
    That's incorrect. The problem is, when you pass the address of the character to cout, it thinks you are handing it an array of chars, and so tries to print a string. The same effect if the OP had done:

    cout<<&c;

    To get around that, you could do:

    cout<<(int)p;

    That way, cout interprets the address as an integer. To view the out put in hex I think you can do:

    cout<<ios::hex<<(int)p;
    Code:
    #include <ip.hpp>

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Originally posted by kiss_psycho
    thanks omni, guess i got confused.
    That's understandable. Remember that the << operator is overloaded for all of the fundamental types such as int, double, and char *. You were passing it a char *.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character pointer arrays
    By axe in forum C Programming
    Replies: 5
    Last Post: 11-15-2007, 03:25 AM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 06:23 PM
  5. character pointer clarification
    By theweirdo in forum C Programming
    Replies: 3
    Last Post: 01-29-2002, 09:51 AM

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