簡體   English   中英

C ++字符串和指針

[英]C++ strings and pointers

我正在學習C ++,目前我正在使用字符串和指針。

我正在按照練習冊和我創建的以下問題之一:

#include <iostream>
#include <string>

using namespace std;

int main(void){
    string * firstName=nullptr;
    string * lastName=nullptr;
    string * displayName=nullptr;

    cout << "Enter your first name: " << endl;
    getline(cin,*firstName);

    cout << "Enter your last name: " << endl;
    getline(cin,*lastName);

    displayName=new string;
    *displayName= *lastName + ", " + *firstName;

    cout << "Here's the information in a single string: " << displayName;
    cin.get();
    return 0;
}

為了使用更多的指針,我試圖將它與字符串混合在一起,並因此而使解決方案變得更加復雜。 當我運行這個時,我得到一個“未處理的異常:訪問沖突讀取位置xxxxxxxxx”。

有人可以通過仍然使用指針和字符串而不是char數組(我已經知道如何做)來建議解決方案嗎?

這是因為在使用對象之前沒有分配對象:

string * firstName = new string();
//...
delete firstName;

值得補充的是,在這種情況下使用指針是毫無意義的:標准C ++庫中的字符串對象從堆中分配字符串的數據; 無論如何,字符串通常不會超過一對指針。

我想,你根本不想使用pointers 您可以使用沒有指針的strings

#include <iostream>
#include <string>

using namespace std;

int main(void){
  string firstName;
  string lastName;
  string displayName;

  cout << "Enter your first name: " << endl;
  getline(cin,firstName);

  cout << "Enter your last name: " << endl;
  getline(cin,lastName);

  displayName= lastName + ", " + firstName;

  cout << "Here's the information in a single string: " << displayName;
  cin.get();
  return 0;
}

其他,如果你需要指針,你必須為變量分配內存:

  cout << "Enter your first name: " << endl;
  firstName = new string();
  getline(cin,*firstName);

...並使用解除引用運算符( * )打印結果:

cout << "Here's the information in a single string: " << *displayName;

它看起來像這樣:

int main()
{
    std::string* s = new std::string;
    std::getline(std::cin, *s);
    std::cout << *s;
    delete s;
}

但實際上沒有理由這樣做,只需在堆棧上定義一個普通的字符串變量。

您正在收到錯誤,因為您使用字符串作為指針並且您沒有初始化它們。 這樣做的正確方法是:

#include <iostream>
#include <string>

using namespace std;

int main(void){
    string firstName;
    string lastName;
    string displayName;

    cout << "Enter your first name: " << endl;
    cin >> firstName;

    cout << "Enter your last name: " << endl;
    cin >> lastName;

    displayName = firstname + ' ' + lastName;


    cout << "Here's the information in a single string: " << displayName << endl;
    return 0;
}

實際上,您可以使用指向字符串的指針,但它們應該用作本地對象並作為引用傳遞(或者如果您願意,則傳遞給const引用)。

訪問沖突是因為您要取消引用空指針。

在這里設置空指針

string * firstName=nullptr;

然后在這里取消引用

getline(cin,*firstName)

你需要有一個名字'指向'某事(在這種情況下是一個字符串)。 這是一個沒有例外的修改版本。

int main(void){
    string * firstName= new string();
    string * lastName=new string();
    string * displayName=new string();

    cout << "Enter your first name: " << endl;
    getline(cin,*firstName);

    cout << "Enter your last name: " << endl;
    getline(cin,*lastName);

    //displayName=new string;
    *displayName= *lastName + ", " + *firstName;

    cout << "Here's the information in a single string: " << displayName->c_str();
    cin.get();
    return 0;
}

既然你正在使用nullptr ,我想一個成熟的C ++ 11解決方案同樣很好:

#include <iostream>
#include <memory>
#include <string>

using namespace std;

int main(void){
    unique_ptr<string> firstName(new string());
    unique_ptr<string> lastName(new string());
    unique_ptr<string> displayName(new string());

    cout << "Enter your first name: " << endl;
    getline(cin,*firstName);

    cout << "Enter your last name: " << endl;
    getline(cin,*lastName);

    *displayName= *lastName + ", " + *firstName;

    cout << "Here's the information in a single string: " << *displayName;
}

當然使用nullptr並不是你想要的:你需要分配你想要使用的資源。

請注意,在這些簡單的情況下使用指針就是在語法方面和錯誤方面將自己射入腳中。

編輯我糾正了代碼(一個被遺忘的括號和main最后一行的* ),它成功地編譯並運行在GCC 4.7上。

閱讀c編程10條誡命 對於今天的開發者來說,有些或多或少已經過時,但有些仍然很重要,例如第二個:

你不應該遵循NULL指針,因為混亂和瘋狂等待着你。

那就是你在這里做的事情。 你的指針無處可尋(見std::nullptr的賦值)。

要糾正這個問題,您必須將正確的類/結構的新對象分配給指針。 另外,不要忘記稍后刪除它:

std::string *myString = new std::string(); // create an object and assign it's address to the pointer

// do something with it... (this part has been right)

delete myString; // free the memory used by the object

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM