在C ++中取消引用NULL指针

时间:2013-01-20 14:41:40

标签: c++ pointers null

所以我试图熟悉c ++。这是应该使用指针的任务。这是怎么回事:

  

编写一个提示用户输入其名字的功能   和姓氏,作为两个单独的值。这个功能应该返回   两个值都通过附加指针调用给调用者。它应该提示   仅当调用者传入最后一个NULL指针时的姓氏   名。

我尝试了几个版本。我现在坚持的是:

#include <iostream>
#include <string>

using namespace std;


void getFullName(string *p_first, string *p_last) {
    cout << "First name:";
    getline(cin, *p_first);
    if (!p_last) {
        cout << "Last name:";
        getline(cin, *p_last);
    }
}


int main() {

    string first;
    string *p_first = &first;
    string *p_last = NULL;

    getFullName(p_first, p_last);

    cout << *p_first << endl << *p_last << endl;
    return 0;
}

嗯,它崩溃了。而且我试图传递对“最后一个”的引用。然后指向它。 但退出函数后,指针再次为NULL。

5 个答案:

答案 0 :(得分:7)

我认为练习的内容有错误,应该是:

  

编写一个功能,提示用户输入他或她的名字和姓氏,作为两个单独的值。此函数应通过附加指针将两个值都返回给调用者。只有当调用者传入一个非NULL 指针作为姓氏时,它才会提示输入姓氏。

目前,您的代码通过取消引用空指针

导致未定义的行为
void getFullName(string *p_first, string *p_last) {
    cout << "First name:";
    getline(cin, *p_first);
    if (!p_last) {    /* <-- This test should be inverted */
        cout << "Last name:";
        /* Now, you get here only when p_last == NULL. On the next line, 
         * you dereference that null-pointer and try to read a string into 
         * non-existing memory: recipe for disaster.
         * With the condition inverted, you would only get here if you have 
         * a string to store the text in. */ 
        getline(cin, *p_last);
    }
}

答案 1 :(得分:0)

不要使用指针,你不需要指针。只需通过引用传递参数:

void getFullName(string& p_first, string& p_last) 

然而,问题是您要取消引用p_last NULL - &gt;未定义的行为:

if (!p_last) {  //this evaluates to true, because p_last==NULL
    cout << "Last name:";
    getline(cin, *p_last);
}

答案 2 :(得分:0)

鉴于您的问题表明您使用了额外的,我会将其写为:

string getFullName()
{
    string first, last;
    cout << "First name:";
    getline(cin, first);
    cout << "Last name:";
    getline(cin, last);
    return first + last;  // note use of additional
}

答案 3 :(得分:0)

它不起作用的原因是因为对getline的调用采用字符串引用(basic_string<>&)作为第二个参数,您将取消引用空指针并将其传递通过。这几乎总是会出错。你需要new一个新的字符串传递给它,但这是一个坏主意,因为它几乎可以保证导致内存泄漏,因为你没有办法将它传回去。

如果你传递一个指针,期望它指向的值被改变,从而作为一个返回值,你需要提供一个有效的指针。

答案 4 :(得分:0)

首先,分配是错误的(如果这是完整的分配),或者您对分配的解释是错误的。这是一个将读取姓氏的版本,如果p_last指向应存储它的字符串:

void getFullName(string *p_first, string *p_last) {
    cout << "First name:";
    getline(cin, *p_first);
    if (p_last) {
        cout << "Last name:";
        getline(cin, *p_last);
    }
}

这是一个使用p_last作为姓氏的版本,如果它不是NULL,否则从用户读取姓氏,并且在两种情况下都返回p_first中的全名:

void getFullName(string *p_first, string *p_last) {
    cout << "First name:";
    getline(cin, *p_first);
    string lastname;
    if (!p_last) {
        cout << "Last name:";
        getline(cin, lastname);
    } else {
        lastname = *p_last;
    }
    *p_first += lastname;
}

这是一个版本,如果它是NULL,则使用对指针的引用来改变p_last

void getFullName(string *p_first, string *&p_last) {
    cout << "First name:";
    getline(cin, *p_first);
    if (!p_last) {
        p_last = new string;
        cout << "Last name:";
        getline(cin, *p_last);
    }
}

但这些都不符合您的作业。

相关问题