pointers and array

This is a discussion on pointers and array within the C++ Programming forums, part of the General Programming Boards category; hi everybody, today i was reading a tutorial on C++ on <http://www.cplusplus.com/doc/tutorial/pointers/> and there it is wriitten in pointer and ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    1

    pointers and array

    hi everybody,
    today i was reading a tutorial on C++ on <http://www.cplusplus.com/doc/tutorial/pointers/> and there it is wriitten in pointer and array Initialization section:"It is important to indicate that terry contains the value 1702, and not 'h' nor "hello", although 1702 indeed is the address of both of these."
    Now i write a very simple program and compile it with gcc:
    Code:
    #include<iostream>
    using namespace std;
    int main(){
    	char * terry = "hello";
    	cout << terry << endl;
    	
    }
    and its output is : "hello"
    If terry does not contain "hello", the output should have been some address.

    Pls help me to understand the concept.

    Regards,
    arpanm

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    char* is a special case, in that 99.9% of the time it makes more sense to print the string it points to rather than the pointer itself.

    To print the actual pointer itself, try
    Code:
    cout << reinterpret_cast <void*> (terry) << endl;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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