c++ - 指针函数参数数组 - 奇怪的行为

标签 c++ arrays pointers struct

我正在尝试调试一个程序,但不太了解发生了什么。从用户输入填充书籍数组后,我试图按字母顺序(按书名)对指向书籍对象的指针数组进行排序。运行该程序会导致段错误,并且调试器向我显示,当我的 orderAlphabetically 函数被调用时,指针数组(到那时为止一切正常)仅包含三个元素。知道这是为什么吗?

谢谢!

#include <iostream>
#define MAXBKS 20
using namespace std;

typedef struct book{

    float price;
    string name;
    string author;

}book;


// addBooks():  Gather user input and add book structs to array.
// @Params: the main library array, and its size.
// 
void addBooks(book library[], int size){

    string name, author, price;

    for(int i = 0; i< size; ++i){

        cout<< "Enter the name of the book, author, and price, each separated by \"~\": "; 

        if (cin.peek() == '\n') break;

        getline(cin, name, '~');    
        getline(cin, author, '~');  
        getline(cin, price, '~');   

        library[i].name = name;
        library[i].author = name;
        library[i].price = stod(price);

        cin.get();
    }
}


// orderAlphabetically():   assign pointers to alphabetically ordered book 
//                          titles. 
// @Params: the main library array, and the pointer array to hold the order
// 
void orderAlphabetically(book *library_ptrs[], int size){

    int pos;
    book *temp;

    for (int i = size; i > 1; i--){
        pos = 0;
        for (int j = 0; j < i; ++j){
            if ((library_ptrs[i]->name).compare(library_ptrs[pos]->name) > 0 )
                pos = j;
        }

        temp = library_ptrs[pos];
        library_ptrs[pos] = library_ptrs[i-1];
        library_ptrs[i-1] = temp;
    }
}

void printLibrary(book library[], int size){

    int i = 0;

    while ((library[i].price != 0) && (i < size)){

        cout<< "\nBook "<< i+1<< ": "<< endl;
        cout<< library[i].name<< endl;
        cout<< library[i].author<< endl;
        cout<< library[i].price<< endl;

        cout<<endl;
        ++i;
    }

}


int main(){
    book library[MAXBKS] = {0};
    book *library_ptrs[MAXBKS] = {0};

    // Add books to the library until the user enters 'enter'
    addBooks(library, MAXBKS);

    // Print entered books
    cout<< "\nThese are the books you have added: "<< endl;
    printLibrary(library, MAXBKS);

    // Order Alphabetically
    for (int i = 0; i < MAXBKS; ++i)
        library_ptrs[i] = library + i;

    orderAlphabetically(library_ptrs, MAXBKS); 

    return 0;
}

编辑:修复循环

最佳答案

while ((library[i].price != 0) && (i < size)) // i <= size is wrong


if ((library_ptrs[i]->name).compare(library_ptrs[pos]->name) > 0) // i was initialized to size, so similar problem, out of bound.

请查看 std::sort() 而不是滚动您自己的排序函数。

关于c++ - 指针函数参数数组 - 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28707430/

相关文章:

c++ - 如何在构造时为 std::vector 保留内存?

c++ - 使用字符串流时如何停止 double 转换为科学计数法

c - 将数据写入 C 中另一个程序的指针?

mysql - 循环并将值插入到mysql中的表中

c - 函数执行后指针为空

dictionary - 如何将接口(interface){}转换为结构指针?

c++ - 由于 SSL3_GET_CLIENT_CERTIFICATE :no certificate returned,验证 SSL 客户端真实性失败

C++:如何检索已安装的 Firefox 插件列表?

ruby-on-rails - 将数组的数组拆分,[0] 拆分为一个,[1] 拆分为另一个

java - 将字符串数组从java传递给pl/sql函数