Thread: The purpose of NULL pointer?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77

    The purpose of NULL pointer?

    1. Write a function that prompts the user to enter his or her first name and last name, as two separate
    values. This function should return both values to the caller via additional pointer (or reference)
    parameters that are passed to the function. Try doing this first with pointers and then with references.
    (Hint: the function signature will look similar to the swap function from earlier!)

    3. Modify the program you wrote for exercise 1 so that instead of always prompting the user for a last
    name, it does so only if the caller passes in a
    NULL pointer for the last name.

    My answer for question 1:

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    void get_name(string *first_name, string *last_name)
    {
        std::cout << "First name: ";
        std::getline(cin,*first_name);
        std::cout <<endl;
        std::cout << "Last name: ";
        std::getline(cin,*last_name);
        std::cout << endl;
      
    }
    int main()
    {
        string first_name, last_name;
        get_name(&first_name,&last_name);
    
    
        std::cout << "Hello " << first_name << " " << last_name << endl;
    
    
        return 0;
    }
    I'm not really sure what the question want me to do, or what is the purpose in it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The idea is that if the caller passes in a null pointer, you don't prompt for the last name (and of course you don't read in a last name). Thus, the last name parameter becomes optional.

    Note that you cannot do this for reference parameters because a null reference is illegal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    " it does so only if the caller passes in a
    NULL pointer for the last name."


    kind of contradicts with "if the caller passes in a null pointer, you don't prompt for the last name".

    Sorry if I read the question wrong or anything. But from what I've read, it says that to prompt the user for last name only if the caller passes in NULL pointer for the last name.

    Do I need to initialize the last_name to NULL, and then make an if statement?


    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    void get_name(string *first_name, string *last_name)
    {
        last_name = 0;
    
    
        std::cout << "First name: ";
        std::getline(cin,*first_name);
        std::cout <<endl;
        if( last_name == 0)
        {
            std::cout << "Last name: ";
            std::getline(cin,*last_name);
            std::cout << endl;
        }
    }
    int main()
    {
        string first_name, last_name;
        get_name(&first_name,&last_name);
    
    
        std::cout << "Hello " << first_name << " " << last_name << endl;
    
    
        return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Meerul264
    " it does so only if the caller passes in a
    NULL pointer for the last name."

    kind of contradicts with "if the caller passes in a null pointer, you don't prompt for the last name".

    Sorry if I read the question wrong or anything. But from what I've read, it says that to prompt the user for last name only if the caller passes in NULL pointer for the last name.
    Oh sorry, I misread that. You are right: your question 2 states that if the caller passes a null pointer for the last name, then you should prompt for a last name, and hence presumably read it in.

    Unfortunately, that presents a problem: if you only have a null pointer, then you have no place to store the input read. You could use dynamic memory allocation, but then the last name parameter should be a reference to a pointer, not just a pointer, and then the caller would become responsible for using delete. That would be unnecessary complication.

    I suspect that your teacher made a mistake in the setting the question, so perhaps you should consult him/her about it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    this is a ridicolously constructed question by the tutor, badly phrased may be more accurate, it must be very confusing for the students
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by rogster001 View Post
    this is a ridicolously constructed question by the tutor, badly phrased may be more accurate, it must be very confusing for the students
    Try working with real requirements from customers or stakeholders, and you will understand that poorly or ambiguously written requirements are the norm. It is the job of a professional developer to clarify such requirements in order to be able to produce a working solution.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by grumpy View Post
    Try working with real requirements from customers or stakeholders, and you will understand that poorly or ambiguously written requirements are the norm. It is the job of a professional developer to clarify such requirements in order to be able to produce a working solution.
    Don't I know it. Sometimes the requirements for an entire project can be just a one-liner.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    Unfortunately, that presents a problem: if you only have a null pointer, then you have no place to store the input read. You could use dynamic memory allocation, but then the last name parameter should be a reference to a pointer, not just a pointer, and then the caller would become responsible for using delete. That would be unnecessary complication.

    I suspect that your teacher made a mistake in the setting the question, so perhaps you should consult him/her about it.
    An alternative would be for the function to return a string. If the last argument is NULL, read a (local) std::string, and return it.

    I do agree it is a flawed requirement.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does static initialization of pointer make it null pointer
    By Saurabh Mehta in forum C Programming
    Replies: 1
    Last Post: 11-23-2012, 12:05 AM
  2. Null Pointer
    By saswatdash83 in forum C Programming
    Replies: 3
    Last Post: 07-15-2008, 04:12 AM
  3. pointer ot NULL
    By anthonye in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2003, 03:43 PM
  4. pointer always NULL?
    By endo in forum C++ Programming
    Replies: 5
    Last Post: 08-14-2002, 03:10 AM
  5. Null pointer
    By MethodMan in forum C Programming
    Replies: 2
    Last Post: 03-11-2002, 06:49 PM