Pointer confusion. Please explain :)

This is a discussion on Pointer confusion. Please explain :) within the C++ Programming forums, part of the General Programming Boards category; Today I've learned some concepts about Pointer in C++ and I have a confusion in this code snippet and its ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    6

    Question Pointer confusion. Please explain :)

    Today I've learned some concepts about Pointer in C++ and I have a confusion in this code snippet and its result.

    Source Code:
    Code:
    // Pointer array
    #include <iostream>
    using namespace std;
    
    void main () {
    	char * say = "Hello";		
    	//
    	cout << "say = " << say << endl;
    	cout << "&say = " << &say << endl;
    	cout << "*say = " << *say << endl;
    	cout << "say[1]=" << say[1] << endl;
    	cout << "*(say+1)=" << *(say+1) << endl;	
    	//
    	cout << endl << endl;
    	for (int i=0; i<5; i++) {
    		cout << "say["<< i <<"]=" << say[i] << endl;	
    	}
    	//
    	cout << endl << endl;
    	for (int i=0; i<5; i++) {	
    		cout << "&say["<< i <<"]=" << &say[i] << endl;
    	}
    }
    And this is the result:
    Code:
    say = Hello
    &say = 0012F3A4
    *say = H
    say[1]=e
    *(say+1)=e
    
    say[0]=H
    say[1]=e
    say[2]=l
    say[3]=l
    say[4]=o
    
    &say[0]=Hello
    &say[1]=ello
    &say[2]=llo
    &say[3]=lo
    &say[4]=o
    I try to understand why it gave this result, but I cannot understand the section in bold.

    Please explain it for me.

    Thanks very much,
    Nichya.
    Attached Files Attached Files

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If say[i] is a character, then what is &say[i]? The address of the character, or char*.

    char* is treated as a special case by cout to represent a C-style null-terminated string. If you want to output the address of each character, you could cast the resulting pointer to another pointer type that doesn't receive this special treatment (void*).

    Code:
    for (int i=0; i<5; i++) {
        cout << "&say["<< i <<"]=" << static_cast<void*>(&say[i]) << endl;
    }
    Last edited by anon; 01-20-2010 at 12:36 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    6
    Thank you. I think I must read more to understand what you said ^^ It's a little bit complicated with me.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    19
    Quote Originally Posted by nichya88 View Post
    Thank you. I think I must read more to understand what you said ^^ It's a little bit complicated with me.
    Basicly, when you got "&say[0]=Hello", that's because you gave cout the address for the character 'H', and it just kept printing them all until it stumbled upon a null-termination (which is no character, the value 0, used to mark the end of the string). Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    If say = "Hello", then say[0] would be 'H'.
    That would mean that &say[0] would return the address where that character H is stored, which is the address of say[0], and also the address that say points to in the first place, since the poiner points to the first char in the string.
    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
    Jan 2010
    Posts
    6
    Thank all of you, now I have to understand it.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    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.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    6

    Smile

    Quote Originally Posted by Elysia View Post
    I read some tutorials, and found that they always use int main() {} despite the fact that void main(){} is OK! . I wondered why? So, this article tells me the answer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Pointer confusion
    By Blackroot in forum C++ Programming
    Replies: 11
    Last Post: 09-12-2007, 01:44 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM
  4. Pointer confusion...
    By fkheng in forum C Programming
    Replies: 13
    Last Post: 06-23-2003, 11:18 PM
  5. Can someone explain the "->" pointer?
    By Ryeguy457 in forum C++ Programming
    Replies: 9
    Last Post: 08-10-2002, 09:19 PM

Tags for this Thread


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