2015-10-21 65 views
1

修復:http://pastebin.com/71QxqGk5修復:訪問衝突讀取位置(指向字符串數組的指針)

第一篇文章/問題。

所以這是C++,我試圖打印一個單詞的數組。

#include <cstdlib> 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <cstring> 
#include <cctype> 
#include <ctime> 
using namespace std; 

//structs 
struct Input 
{ 
    int size; 
    string* word; 
    bool is_palindrome[]; 
}; 

//prototypes 
bool openInputFile(ifstream &ifs); 
void File_to_Array(string* word, int &size); 
void PrintArray(string* word, int size); 

//main 
int main() 
{ 
    Input myInput = { 0, nullptr, false }; 
    File_to_Array(myInput.word, myInput.size);//copy arr and get size 
    cout << myInput.word; //this outputs 00000000 
    cout << *myInput.word; //this breaks and throws exception as commented below 

    //Exception thrown at 0x0098BB6B in Project1.exe: 0xC0000005: Access violation reading location 0x00000014. 

    PrintArray(myInput.word, myInput.size);//print array of strings 

    system("PAUSE"); 
    return 0; 
} 

//functions 
bool openInputFile(ifstream &ifs) 
{ 
    string filename; 

    cout << "Enter the input filename: " << endl; 
    getline(cin, filename); 
    ifs.open(filename.c_str()); 
    return ifs.is_open(); 
} 

void File_to_Array(string* word, int &size)//copies file to dyn arr and assigns size from first elem 
{ 
    ifstream myFile; 
    while (!openInputFile(myFile)) 
     cout << "Could not open file" << endl; 
    string tempstr = ""; 
    getline(myFile, tempstr);//first line is size of dyn arr 
    size = stoi(tempstr);//now we have max size of dyn arr of strings 
    word = new string [size];//now we have the array of strings, *word[index] = string1 
    int i; 
    for (i = 0; getline(myFile, word[i]) && i < size; ++i);//for each line 
    //copy line of string from file to string arr within "bool" test, second param of for loop //copying done 
    size = i; 
    myFile.close();//done with file, no need, close it 
} 

void PrintArray(string* word, int size) 
{ 
    //for (int i = 0; i < size; ++i) 
    //cout used to be here, but now its in main, for debugging 
} 

所以我想知道如果我的問題是傳遞一個結構的成員,如果我應該已經過去了,而不是整個結構類型「myInput」進入功能和使用 - >操作者進入myInput的成員。

下面

是一個文本文件的例子

5 
month 
Runner 
NEON 
digit 
ferret 
nothing 

5將是動態分配的數組的大小,其餘都是字符串,你可以看到有6個字符串,所以我在for循環測試文件是否仍然向字符串傳輸字符串。

+0

'Input :: is_palindrome'應該是一個數組,但是你用'false'初始化它。 – Downvoter

回答

1

File_to_Array的這部分導致了問題:

word = new string [size]; 

你認爲你正在設置myInput對象的指針指向字符串數組,而你不是。當你將指針傳遞給函數時:

File_to_Array(myInput.word, myInput.size) 
       ^^^^^^^^^^^^ 

你確實傳遞了指針的副本。所以在File_to_Array裏面,這個副本被重新指向新創建的字符串數組,但myInput中的實際指針沒有改變。你應該通過一個參考指針來代替:

void File_to_Array(string*& word, int &size) 
        \___________/ 
         ^--reference to a pointer 

我也建議你使用vector[string]代替。最後,你的bool is_palindrome[];成員和它的初始化看起來很奇怪,但是很難進一步評論,因爲它們從來沒有用在代碼中。

+0

哦,所以我正在創建一個指針的副本...好吧,我現在明白了。所以我必須將一個指向字符串數組的指針傳遞給函數。 非常感謝,我現在就試試,並報告成功或失敗。希望前者。 而我們(我的班級)還沒有學過矢量,這是一個hw assn的一部分,需要使用特定的東西,其中之一是數組,指針,字符串,所有這些東西,而不是向量。 – Roopan

+0

非常感謝您的即時幫助。我對這個論壇有點新,但第一印象到目前爲止是非常棒的。 我回來告訴你它已經工作了,我把bool []改成了bool * – Roopan

+0

http://pastebin.com/71QxqGk5 FIXED – Roopan