2016-02-25 99 views
1

我的任務是使用結構將短語從豬拉丁語翻譯成英語。到目前爲止,我的項目接受了一個字符串,除了結尾的標點符號之外,刪除了所有大寫字母,所有標點符號,並將字符串拆分爲由單詞組成的數組結構。然後,我應該通過return語句特別返回一個指向我的結構數組的指針。回到主界面時,我想創建另一個與我的pigLat函數中相同的結構數組,以便能夠發送給我的項目的第二部分的新函數(這將包括將拉丁文翻譯成英文) 。指向C++結構數組的指針的問題

問題:嘗試使用指針創建新數組會導致核心分段錯誤。

任何幫助解釋爲什麼這不起作用,並解釋什麼可能會更好地工作,將不勝感激!

#include <iostream> 
#include <string> 
#include <algorithm> 
#include <cctype> 
using namespace std; 

struct Word           //Variable type to hold each word 
{ 
    string piglatin; 
    string english; 
}; 

Word *pigLat(int &); 

int main() 
{ 
    int size;          
    Word *phrase; 
    phrase = pigLat(size);    //Passes value of size by reference, and returns ptr to structure 
    Word pigSent[size]; 

    //THIS IS WHERE I GET SEGMENTATION FAULT************************************ 
    for (int count = 0; count < size; count++) 
    { 
     pigSent[count].piglatin = phrase[count].piglatin; 
    } 
    //************************************************************************* 
    return 0; 
} 

//Receives a phrase in pig latin, finds # of words in phrase, seperates pig latin from english, returns pig latin 
Word *pigLat(int &sizeOf) 
{ 
    string phrase;         //Variable to hold pig latin phrase 

    cout << "Enter a phrase in pig latin: ";  //User enters pig latin phrase 
    getline(cin, phrase); 

    char punctuation = phrase[phrase.length() - 1]; //Assumes last char is punctuation, and saves it 

    //Removes all characters besides last period 
    char removch[] = "&,'?.!-"; 
    for (int count = 0; count < 7; count++) 
    { 
     phrase.erase(remove(phrase.begin(), phrase.end(), removch[count]), phrase.end()); 
    } 

    int length = phrase.length();     //Number of elements in updated string 
    phrase.insert(length, 1, punctuation);   //Inserts final punctuation at end of phrase 

    //Removes all capitalization 
    for (int count = 0; count < length; count++) 
    { 
     if(phrase[count] >= 'A' && phrase[count] <= 'Z') 
     { 
     phrase[count] = tolower(phrase[count]); 
     } 
    } 

    int index = 0; 
    int count = 0; 
    int *spaceElements = 0; 
    spaceElements = new int[length];    //Dynamically allocates spaceElements memory 

    for (count; count < length; count++)   //Gives number of white spaces in phrase 
    { 
     if (phrase.find(' ', count) != -1) 
     { 
     int space = phrase.find(' ', count); 
     count = space; 
     spaceElements[index] = space; 
     index++; 

     } 
    } 
    sizeOf = (index + 1); 
    Word sentence[sizeOf]; 
    int start = 0; 
    int end = 0; 
    count = 0; 

    //Copies, word by word, into Word array sentence 
    for (count; count < sizeOf; count++) 
    { 
     for (count; count < index; count++) 
     { 
      end = spaceElements[count] - start; 
      sentence[count].piglatin = phrase.substr(start, end); 
      start = spaceElements[count] + 1; 
     } 
     sentence[count].piglatin = phrase.substr(start, length); 
    } 

    //Testing*************************************************** 
    for (count = 0; count < sizeOf; count++) 
     cout << endl << sentence[count].piglatin << endl; 
    //********************************************************** 

    delete [] spaceElements; 

    Word *ptrToSet = sentence;    //assigns pointer to memory address of sentence array 

    return ptrToSet; 
} 
+0

標準C++中不允許使用'Word pigSent [size];'。編譯時必須知道數組的維數。相反,你可以使用'std :: vector pigSent(size);'。在其他功能 –

回答

1

pigLat()功能在本地函數範圍實例sentence

Word sentence[sizeOf]; 

PigLat()指針返回到該陣列的第一個元素:

Word *ptrToSet = sentence; 
return ptrToSet; 

然而,一旦pigLat()回報,因爲sentence是一個本地範圍的對象,它會超出範圍並被銷燬。隨後試圖取消引用返回的指針是未定義的行爲。這是您的應用程序崩潰的可能原因。也可能有其他原因,我沒有再看。

+0

也有類似的問題,這加起來....是否有可能通過返回語句返回句子數組? – TingRay

+0

不,數組無法按值返回。你必須在堆上實例化數組,使用'new []'(並用'delete []'去掉它)。 –

+0

修正了它!感謝您的幫助。 – TingRay