Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Pointers freak me out!

  1. Feb 5, 2006 #1
    In my program have a function like this:

    Code (Text):

    void * changeInfo(bool *flag, string *NewName)
    {
         if (!(*flag))
         {
         cout << endl << "Enter the new Name: ";
         getline(cin, *NewName);
         cout << *NewName;
        }
    }
     
    My problem is reading the input using the pointer to a string. The program simply skips that part. It is wierd that "getline" works if I'm using the commands within the main. Yes, I prototype and call the function in addition to passing the inputs. It does not seem to be trivial. I've spent a lot of time trying to fix this, but no success so far.

    Any help is highly appreciated.
     
  2. jcsd
  3. Feb 6, 2006 #2

    jtbell

    User Avatar

    Staff: Mentor

    How are you calling the function, in main() or wherever? If a function expects to receive pointers, and main() has the actual objects, you need to specify when you call the function that you're passing the addresses of the objects rather than the objects themselves:

    Code (Text):

    bool f;
    string s;
    changeInfo (&f, &s);
     
    Also, your function doesn't change the value of 'flag', so there's no need to pass it by pointer. Bools are small enough that they can be passed by value efficiently.

    (Personally, in C++ I prefer to pass parameters that need to be modified in the function, by reference instead of by pointer, but this is basically a matter of personal preference only, so I won't press this issue.)

    Finally, why did you declare the function as 'void *'? If the function doesn't return anything via a 'return' statement, it should be declared simply as 'void'.
     
  4. Feb 6, 2006 #3
    Unfortunately, I can only use pointers since this is one of the requirements of my project. I'm using * after void because I thought it was the right procedure if the inputs are pointers. I have a lot to learn. :)

    I'm going to remove the flag and inspect my main function. I'll get back to you if I have more questions.

    Thank you
     
  5. Feb 6, 2006 #4

    jtbell

    User Avatar

    Staff: Mentor

    What I meant about the flag is that you can pass it this way:

    In main():

    Code (Text):

    bool f;
    string s;
    // I assume that you set the value of f somewhere in between here...
    changeInfo (f, &s);
     
    In the function:

    Code (Text):

    void changeInfo(bool flag, string *NewName)
    {
         if (! flag)
         {
         cout << endl << "Enter the new Name: ";
         getline(cin, *NewName);
         cout << *NewName;
        }
    }
     
    Passing the flag by value is more efficient than passing by pointer, because the function doesn't have to dereference a pointer first, before using the value of the flag. It just uses the value directly.
     
  6. Feb 7, 2006 #5

    dduardo

    User Avatar
    Staff Emeritus

    I though it was cin.getline(char*,size)
     
  7. Feb 7, 2006 #6

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    There's a global "getline" function that lets you read a line into a std::string.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Pointers freak me out!
  1. This pointer (Replies: 2)

  2. Pointers and classes (Replies: 1)

  3. Use of pointers (Replies: 3)

  4. Pointers and strings (Replies: 19)

Loading...