2014-09-12 53 views
-4

我是一個C++新手,我試圖把實踐中的指針與字符串。我所做的程序只是將用戶類型的字符串存儲在命令行中。但我得到段錯誤,不知道爲什麼。使用字符串指針的分段錯誤

這是代碼:

#include <cstdio> 
#include <cstdlib> 
#include <iostream> 

using namespace std; 

//This code is meant to learn how to use pointers and strings 
//Ask the user for who are they in the family and save it in string array! 


void print_string (string* Value, int const nSize); 
int get_names(string* Family_input); 

int main (int nNumberofArgs, char* pszArgs[]) 
{ 

    cout << "Tis program stores your family members\n"; 
    cout<< "Type the names and write 0 to exit\n"; 

    string familia_string; 
    string* familia = &familia_string; 

    int family_number; 

    family_number=get_names(familia); 

    cout << "The family members are: "; 

    print_string(familia, family_number); 

    cout << endl; 

    return 0; 
} 

int get_names(string* Family_input) 
{ 
    int i=0; 
    string input=""; 
    string old_input=""; 
    while (input!="0") 
    { 
    cout << "type " << i <<" member\n"; 
    //cin >> *(Family_input+i); 
    //input=*(Family_input+i); 
    cin >> input; 
    *(Family_input + old_input.length()) = input; 
    old_input=input; 
    i++; 
    } 
    return i; 

} 

void print_string (string* Value, int const nSize) 
{// I don't want to &psValue to be changed! 
    for (int i=0; i<nSize; i++) 
    { 
    cout << *(Value+i) << " "; 
    //&psValue++; 
    } 
} 

我不知道這是否是因爲我不接受字符串的正確尺寸,或者我沒有使用正確的指針或者是我有在使用偏移量之前分配內存。

+2

顯示代碼在這裏(最小但完整的例子),而不是在外部網站上。另外,C和C++不是同一種語言,一箇中的「自然」解決方案可能無法在另一箇中工作(所以只能標記正在編寫/編譯的語言)。 – crashmstr 2014-09-12 16:34:23

+3

「*(Family_input + old_input.length())= input'背後的想法是什麼? – NPE 2014-09-12 16:34:44

+0

[GDB](http://www.gnu.org/software/gdb/)和IDE調試器是你的朋友。瞭解如何使用它們,您將能夠搜索導致段錯誤的確切線路。 – skrrgwasme 2014-09-12 16:35:37

回答

0

由於您沒有爲字符串數組分配內存,因此會發生seg故障。

*(Family_input + old_input.length()) = input; 

這是完全廢話。如果你有一個數組,你只能增加一個索引而不是字符串的長度。

1

正如@kleszcz已經指出,該行

*(Family_input + old_input.length()) = input; 

是錯誤的。您正在訪問您不應該訪問的內存。

最簡單的解決方法是改變get_names略:

int get_names(string* Family_input) 
{ 
    int i=0; 
    string input=""; 
    while (input!="0") 
    { 
     cout << "type " << i <<" member\n"; 
     cin >> input; 
     *Family_input += input; // Just keep on appending to the input argument. 
     *Family_input += "\n"; // Add a newline to separate the inputs. 
     i++; 
    } 
    return i; 
} 

也改變print_string到:

void print_string (string* Value) 
{ 
    cout << *Value; 
} 

當然,print_string變得如此簡單,你不需要把它在所有。

您可以更改get_names以使用參考參數而不是指針參數。這是一個更好的做法。

int get_names(string& Family_input) 
{ 
    int i=0; 
    string input=""; 
    while (input!="0") 
    { 
     cout << "type " << i <<" member\n"; 
     cin >> input; 
     Family_input += input; // Just keep on appending to the input argument. 
     Family_input += "\n"; // Add a newline to separate the inputs. 
     i++; 
    } 
    return i; 
} 

然後,將呼叫更改爲get_names。除了使用

family_number=get_names(familia); 

使用

family_number=get_names(familia_string); 
0

如果你想保存在不同的字符串對象不同的名字,我建議:

#include <iostream> 
#include <string> 
#include <cstdlib> 

using namespace std; 

void print_string(string** Value, int const nSize); 
void get_names(string** Family_input, int count); 


int main(int nNumberofArgs, char* pszArgs[]) { 

cout << "This program stores your family members\n"; 
int family_number; 
cout << "Types the number of family members"; 
cin >> family_number; 

/*allocate the number of string pointers that you need*/ 
string** familia = new string*[family_number]; 

cout << "Type the names\n"; 

get_names(familia, family_number); 

cout << "The family members are: "; 

print_string(familia, family_number); 

cout << endl; 

return 0; 
} 

void get_names(string** Family_input, int count) { 

for(int i = 0 ; i < count; i++){ 
    cout << "type " << i << " member\n"; 
    /*create a string obj in the heap memory*/ 
    string *input = new string (""); 
    // using that way you get only a single word 
    cin >> *input; 
    /*create a string object on the stack and put its pointer in the family_array*/ 
    Family_input[i] = input; 
} 
} 

void print_string(string** Value, int const nSize) { 
for (int i = 0; i < nSize; i++) { 
    cout << *(Value[i]) << " "; 
} 
}