Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C++ pointer Question

  • 24-07-2007 02:59PM
    #1
    Closed Accounts Posts: 1,788 ✭✭✭


    Hi folks ,

    I have a question about pointers.
    From this tutorial
    http://www.cplusplus.com/doc/tutorial/pointers.html

    it has
    char * terry = "hello"; 
    

    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.

    ************************
    The use of 1702 was just an example for an address.
    but my problem is when i do this and then cout terry this code exactly
    char * terry = "hello";
    
    cout<<terry<<endl;
    

    it outputs "hello" !!!
    not the address , *terry outputs "h" which makes sense ...

    but to get the address i need to cout &terry


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 10,093 Mod ✭✭✭✭marco_polo


    jackdaw wrote:
    Hi folks ,

    I have a question about pointers.
    From this tutorial
    http://www.cplusplus.com/doc/tutorial/pointers.html

    it has
    char * terry = "hello"; 
    

    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.

    ************************
    The use of 1702 was just an example for an address.
    but my problem is when i do this and then cout terry this code exactly
    char * terry = "hello";
    
    cout<<terry<<endl;
    

    it outputs "hello" !!!
    not the address , *terry outputs "h" which makes sense ...

    but to get the address i need to cout &terry

    More of a java man its been a while since I done C++ but I think I know.

    terry does contain the memory address value such as 1702. However in standard C\C++ char* arrays are treated diferently by stdout functions suct as cout. It assumes a that it is a null terminated string and prints all the chars untill it hits a null.

    If it was of type int* for example the actual memory address would be printed


  • Closed Accounts Posts: 1,788 ✭✭✭jackdaw


    yes i was thinking it had to do with it being Char, with ints it prints the address


  • Registered Users, Registered Users 2 Posts: 5,333 ✭✭✭Cake Fiend


    If you used something like

    printf("The value of 'terry' is: %d\n", terry);

    You should get the memory address.


  • Registered Users, Registered Users 2 Posts: 26,449 ✭✭✭✭Creamy Goodness


    would it not be %p - for pointer - instead of %d as the address will more than likely be in hex


  • Closed Accounts Posts: 669 ✭✭✭pid()


    It would actually be %s for character. The difference is in printing the pointers memory address, or the contents of the pointer (& or omitting it).

    A pointer variable is declared by giving it a type and a name (e.g. int *ptr) where the asterisk tells the compiler that the variable named ptr is a pointer variable and the type tells the compiler what type the pointer is to point to (integer in this case).
    Once a variable is declared, we can get its address by preceding its name with the unary & operator, as in &ptr.

    For instance, if j is a char:
    printf("j has the value %s and is located at memory address %s", j, &j);
    

    You can also use %p like so:
        printf("%p\n", (void *)j);
    

    %p is for pointers of type (void *), so if j isn't a void * pointer then you need to typecast it, as shown above.

    %x prints it as hex (0xce01d4)
    %d prints the memory address as decimal (123)
    %p prints it as hex (0xce01d4)
    %s also prints it as hex.

    It really depends on the format you want to print the address as. You could even use %u.


  • Advertisement
Advertisement