C++ 如何获取指向动态分配对象的指针的基址

C++ 如何获取指向动态分配对象的指针的基址,c++,pointers,dynamic-memory-allocation,C++,Pointers,Dynamic Memory Allocation,我在main()上面定义了一个名为“片段”的结构 在我的main()中,我有: int main() { Pieces *pieceptr; readFile(preceptor); cout << (*pieceptr).word << endl; //SEGFAULT occurs here return 0; } void readFile(Pieces *&pieceptr) { ifstream fin;

我在
main()
上面定义了一个名为“片段”的结构

在我的
main()
中,我有:

int main() {
    Pieces *pieceptr;
    readFile(preceptor);

    cout << (*pieceptr).word << endl; //SEGFAULT occurs here

    return 0;
}
void readFile(Pieces *&pieceptr) {

    ifstream fin;
    fin.open("data");
    int pieceCount;
    if(fin.is_open()) {
        fin >> pieceCount;

        pieceptr = new Pieces[pieceCount];

        for (int i = 0; i < pieceCount; i++) {
            char *tempWord = new char[20];
            fin >> tempWord >> (*pieceptr).jump;
            (*pieceptr).word = new char[stringLength(tempWord)];
            stringCopy((*pieceptr).word, tempWord);
            delete []tempWord; 
            tempWord = NULL;
            pieceptr++;
        }
    } else {
        cout << "Error opening file." << endl;
    }
    fin.close();
}
本项目的限制条件包括:

  • 创建我自己的字符串函数
  • 只有在动态声明或取消分配数组时才应使用方括号
  • 除了++和-或设置回主指针之外,不要使用指针算法
  • 不要在指针上使用箭头(->)符号

我已经对
readFile()
函数进行了广泛的测试,它按照我的要求运行(它正确地填充了碎片数组),但是在调用
main()
中的
readFile()
之后,我需要再次访问数组的第一个元素。正如现在的代码一样,由于指针的增量超出了数组的边界,我得到了一个segfault。如何在不使用指针算法的情况下将指针重置为指向数组中的第一个元素?

多个指针可以指向同一个内存点。指针也可以被复制。解决您的问题的最简单方法是创建另一个指针并对其执行所有递增操作

void readFile(Pieces *&pieceptr) {

    ifstream fin;
    fin.open("data");
    int pieceCount;
    if(fin.is_open()) {
        fin >> pieceCount;

        pieceptr = new Pieces[pieceCount];
        Pieces* pieceIterator = pieceptr;
        for (int i = 0; i < pieceCount; i++) {
            char *tempWord = new char[20];
            fin >> tempWord >> (*pieceIterator).jump;
            (*pieceIterator).word = new char[stringLength(tempWord)];
            stringCopy((*pieceIterator).word, tempWord);
            delete []tempWord; 
            tempWord = NULL;
            pieceIterator++;
        }
    } else {
        cout << "Error opening file." << endl;
    }
    fin.close();
}
void readFile(片段*&pieceptr){
流鳍;
财务公开(“数据”);
整数计件计数;
如果(fin.is_open()){
fin>>计件计数;
pieceptr=新件[件数];
Pieces*pieceIterator=pieceptr;
对于(int i=0;i>tempWord>>(*分段迭代器).jump;
(*pieceIterator.word=新字符[stringLength(tempWord)];
stringCopy((*pieceIterator.word,tempWord);
删除[]临时字;
tempWord=NULL;
分段迭代器++;
}
}否则{

无法复制
pieceptr
指针,并在读取时将其递增。无法“重置指针”;您可以按读取的项目数递减指针,但
readFile
不会返回该信息。您可能没有在
new char[stringLength]中为空终止符分配空间(tempWord)
(假设
stringLength()
的行为类似于
strlen()
)@MichaelBurr:谢谢,但我应该提到我们也不能在项目中使用->符号。我已经更新了帖子。只是一个反问:“不要在指针上使用箭头(->)符号”-我不明白这对教育有什么好处(不像让你自己编写字符串函数)。为什么讲师会施加这种限制?哦,我没想到。现在一切都正常了。谢谢你的回答。
void readFile(Pieces *&pieceptr) {

    ifstream fin;
    fin.open("data");
    int pieceCount;
    if(fin.is_open()) {
        fin >> pieceCount;

        pieceptr = new Pieces[pieceCount];
        Pieces* pieceIterator = pieceptr;
        for (int i = 0; i < pieceCount; i++) {
            char *tempWord = new char[20];
            fin >> tempWord >> (*pieceIterator).jump;
            (*pieceIterator).word = new char[stringLength(tempWord)];
            stringCopy((*pieceIterator).word, tempWord);
            delete []tempWord; 
            tempWord = NULL;
            pieceIterator++;
        }
    } else {
        cout << "Error opening file." << endl;
    }
    fin.close();
}