CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2013
    Posts
    1

    Resolved Question on simple C++ program with pointers

    Question on the below c++ code, 3rd line from bottom of below code, the line :

    cout << ps << " at " << (int *) ps << endl;

    It prints out "fox at 0xb82b78"

    when I change it to :
    cout << ps << " at " << ps << endl;

    It still prints out "fox at 0xb82b78"
    expected behavior - print "fox at fox"

    I thought if you want to see the Address of the string (ps) you have to type cast
    the pointer to another pointer type, such as (int *) ?


    // ------------------------------
    // c++ program
    #include <iostream>
    #include <cstring>

    using namespace std;

    int main()
    {
    char animal[20] = "bear"; // animal holds bear
    const char* bird = "wren"; // bird holds address of string
    char* ps; // uninitialized

    cout << animal << " and "; // display bear
    cout << bird << "\n"; // display wren
    //cout << ps << "\n"; // may display garbage, may cause crash

    cout << "Enter a kind of animal: ";
    cin >> animal; // ok if input is < 20 characters
    //cin >> ps; // to horrible to try; ps doesn't point
    // to allocated space

    ps = animal; // set ps to point to string
    cout << ps << "!\n"; // ok, same is using animal
    cout << "Before using strcpy():\n";
    cout << animal << " at " << (int *) animal << endl;
    cout << ps << " at " << (int *) ps << endl;

    ps = new char[strlen(animal) + 1]; // get new storage
    strcpy(ps, animal); // copy string to new storage

    cout << "After using strcpy():\n";
    cout << animal << " at " << (int*) animal << endl;
    cout << ps << " at " << (int *) ps << endl;
    delete [] ps;

    return 0;
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Question on simple C++ program with pointers

    Quote Originally Posted by rjp1 View Post
    Question on the below c++ code, 3rd line from bottom of below code, the line :

    cout << ps << " at " << (int *) ps << endl;

    It prints out "fox at 0xb82b78"

    when I change it to :
    cout << ps << " at " << ps << endl;

    It still prints out "fox at 0xb82b78"
    What you mentioned is impossible, unless the compiler is horribly broken. Why would the first "ps" print out "fox", but the second "ps" in that line print out an address? Maybe you are confused as to what program you were running, or some other issue.

    Also, why didn't you post the program you mentioned above? Instead you posted a different program (and without code tags -- please use code tags when posting code).
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
        char animal[20] = "bear"; // animal holds bear
        const char* bird = "wren"; // bird holds address of string
        char* ps; // uninitialized
    
        cout << animal << " and "; // display bear
        cout << bird << "\n"; // display wren
    
        cout << "Enter a kind of animal: ";
        cin >> animal; // ok if input is < 20 characters
    
        ps = animal; // set ps to point to string
        cout << ps << "!\n"; // ok, same is using animal
        cout << "Before using strcpy():\n";
        cout << animal << " at " << (int *) animal << endl;
        cout << ps << " at " << (int *) ps << endl;
    
        ps = new char[strlen(animal) + 1]; // get new storage
        strcpy(ps, animal); // copy string to new storage
    
        cout << "After using strcpy():\n";
        cout << animal << " at " << (int*) animal << endl;
        cout << ps << " at " << (int *) ps << endl;
        delete [] ps;
    
        return 0;
    }
    So change that program above to one you say doesn't work as expected.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 17th, 2013 at 07:54 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured