首页 > 解决方案 > 如何遍历c中函数传递的结构数组?

问题描述

我一直在尝试迭代一个结构数组,但由于某种原因它只经过一次。我仍在尝试掌握 c 及其对指针的使用。对于该函数,我必须使用 **books 数组并对其进行迭代。例如,我需要退还多少价值低于 50 美元的书籍。我不知道为什么它只迭代一次。当我使用打印语句检查“number_of_books”时,我得到了五个数组的正确大小。帮助将不胜感激!

// calling the function in main as : int b = get_cost(books, 50)

int get_cost(book **books, int cost){

    int get_book_total = 0; 
                                                                                                 
    for(int i = 0; i < number_of_books; i++){                                                                                           
          if(books[i]->cost < cost){
                 get_book_total++;
          }                                                                                                            
          return get_book_total; 
    }
}

标签: arrayscfunctionpointers

解决方案


您的return语句在您的for循环中,因此您的函数在第一次迭代后返回。


推荐阅读