c++ - C++ 中结构数组指针的问题

标签 c++ arrays pointers structure

我的任务是使用结构将一个短语从 pig 拉丁语翻译成英语。到目前为止,我的项目接受一个字符串,删除所有大写字母,除结尾标点符号外的所有标点符号,并将字符串拆分为由单词组成的结构数组。然后,我应该专门通过 return 语句返回指向我的结构数组的指针。回到 main 后,我想创建另一个与我的 pigLat 函数中的结构相同的结构数组,我将能够将其发送到项目第二部分的新函数(其中包括将 pig 拉丁语翻译成英语) .

问题:尝试使用指针创建新数组会导致核心段错误。

任何帮助解释为什么这不起作用,并解释什么可能更好的方法,我们将不胜感激!

#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 是一个局部范围的对象,它会超出范围并被销毁。随后尝试取消引用返回的指针是未定义的行为。这可能是您的应用程序崩溃的原因。可能还有其他原因,我没有进一步看。

关于c++ - C++ 中结构数组指针的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35615951/

相关文章:

swift - 无法转换为带符号的 16 位数组

c - 递增 C 指针

javascript - Angular JS 图像数组编码麻烦

c - 无法获取整数数组的长度

c++ - 适用于 Windows Mobile 的 DirectShow、视频缩放和图像捕获

c++ - 如何实现我的自定义范围 for 循环?

arrays - 如何使用 Swift 将字典添加到数组?

java - 数组输出中的逻辑错误...?

c++ - 如何以编程方式永久删除程序上的防火墙检查

c++ - 跨不同线程删除插槽内的堆分配对象是否安全?