[英]Trouble with a Pointer to an Array of Structures in C++
我的任務是使用結構將豬拉丁語翻譯成英語。 到目前為止,我的項目接收一個字符串,刪除所有大寫字母,除末尾標點符號外的所有標點符號,並將字符串拆分成由單詞組成的結構數組。 然后,我應該通過具體的return語句,返回指向我的結構數組的指針。 回到main時,我想創建另一個結構數組,該結構與我的pigLat函數中的結構相同,可以將其發送到項目第二部分的新函數中(這包括將豬拉丁語翻譯成英語) 。
問題:嘗試使用指針創建新數組會導致核心分段錯誤。
任何幫助解釋為什么它不起作用,並解釋可能更好的方法將不勝感激!
#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;
}
pigLat()
函數實例化本地函數范圍內的sentence
:
Word sentence[sizeOf];
PigLat()
返回一個指向此數組第一個元素的指針:
Word *ptrToSet = sentence;
return ptrToSet;
但是,一旦pigLat()
返回,則因為sentence
是一個局部作用域的對象,它將超出范圍並被銷毀。 后續嘗試取消對返回的指針的引用是未定義的行為。 這是您的應用程序崩潰的可能原因。 可能還有其他原因,我沒有再看了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.