Like Tree1Likes
  • 1 Post By iMalc

The purpose of NULL pointer?

This is a discussion on The purpose of NULL pointer? within the C++ Programming forums, part of the General Programming Boards category; 1. Write a function that prompts the user to enter his or her first name and last name, as two ...

  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
    21,588
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,588
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,422
    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,245
    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%.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    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.
    Elkvis likes this.
    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,245
    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%.

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-22-2012, 11:05 PM
  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, 02: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, 05:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21