Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C++ pointer Question

  • 24-07-2007 1: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,088 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,335 ✭✭✭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,584 ✭✭✭✭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