Pointer Query

This is a discussion on Pointer Query within the C++ Programming forums, part of the General Programming Boards category; Hi, I have declared: Code: char c[] = "Good Morning"; char *pc = &c[2]; Code: cout << "pc is " ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Cool Pointer Query

    Hi,

    I have declared:
    Code:
    	char c[] = "Good Morning";
    	char *pc = &c[2];
    Code:
    cout << "pc is " << pc << endl;
    The result I get is "od Morning"

    Why does this happen? Can anyone explain how and why this happens?
    And what the assignment statement for the pointer is doing.


    What I expected...
    I expected the output to be the address of the 3rd element of the array.
    I was wrong.


    Thanks!

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    You're using a reference on the character array ch (not the character pointer pch). So it is taking a pointer to the character array, and since pointers point to the first element of an array (i'm almost sure that's right) it will also point to everything after the index of 2. Try removing the & and see if it copies just the one character.

    EDIT: you could always try something like this too
    Code:
    char ch[] = "Good Morning";
    char ch2 = ch[2];
    char *pch = &ch2;
    Last edited by scwizzo; 10-30-2008 at 11:33 AM.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    If I remove the & address operaotr it will not compile

    thanks

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because a string in C is an pointer to an array of characters, terminated with a 0 ('\0').

    In the case of C++, it still supports "old" C style strings, and for cout the << operator that takes a char-pointer treats it as a string. You can print the value of the pointer by casting it to a non-character pointer - reinterpret_cast<void *>(pc)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Quote Originally Posted by scwizzo View Post
    You're using a reference on the character array ch (not the character pointer pch). So it is taking a pointer to the character array, and since pointers point to the first element of an array (i'm almost sure that's right) it will also point to everything after the index of 2. Try removing the & and see if it copies just the one character.
    Uh-oh. Careful with your wording there.
    Since C++ actually has references (and you are not using or creating a reference), that wording is incorrect...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    If you want the third element it would be
    Code:
    char pc = c[2];
    
    char* pc = &c[2];//This gets the string until '\0' is encountered.
    strings are zero-terminated so when you create a pointer to the third [3] letter it continues to get the string until the '\0' character is found.

    edit: wow im slow...

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    pc outputs the way it does thanks to operator << for c-strings, where a c-string is char *. To print the one character, dereference pc.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Quote Originally Posted by Raigne View Post
    If you want the third element it would be
    Code:
    char pc = c[2];
    
    char* pc = &c[2];//This gets the string until '\0' is encountered.
    strings are zero-terminated so when you create a pointer to the third [3] letter it continues to get the string until the '\0' character is found.

    edit: wow im slow...
    Ok, Great. Thanks everyone who replied.

    I understand that what you said Raigne is what happens. I just thought that the assignment statement worked like this:

    Code:
    char *pc = &(c[2]);
    Whereby, the pointer takes the value of the address of the the 3rd element of the array.
    But what you are saying happens is that:
    The pointer to the third element continues to take all the elements of the string that it is part of until it encounters a null terminating charcter.

    Is my understanding right?

    thanks again

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Actually,
    Whereby, the pointer takes the value of the address of the the 3rd element of the array.
    This is correct.
    What happens is when you pass a char* pointer to cout, it actually interprets that as that it is a string, and thus prints every character until it finds the '\0' char.
    So basically, from the position from the pointer, it goes one step forward, print, forward, etc.
    This is a left-over from C, since arrays cannot be passed by value or something else, so a pointer is instead passed. A pointer to the first element in the array. The functions would then interpret that pointer passed as a string and begin printing every character until the '\0' is found.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by matsp View Post
    You can print the value of the pointer by casting it to a non-character pointer - reinterpret_cast<void *>(pc)
    Cast with a static_cast, not a reinterpret_cast. Object*->void* is an implicit conversion that you want explicit, so static_cast is definitely the way to go.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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