arrays - 如何遍历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;
}
}
解决方案
您的return语句在您的for循环中,因此您的函数在第一次迭代后返回。
推荐阅读
- python - 在 python 脚本中正确使用 sudo
- python - 如何将带有存储登录信息的配置文件传递给 selenium 并强制自动登录过程
- sql - SQL 查询以查找除纯数字或空白值之外的所有列
- plotly - 如何在 Plotly 等值线地图中填充索马里兰的值?
- javascript - Jquery 在目标 div 之后插入了所有相同的选择器
- excel - 根据变量定义的路径从多个关闭的工作簿中导入数据
- javascript - Javascript - 仅当输入值长度从 3 个字符变为 2 个字符时才执行函数?
- django - 使用 rest 框架更新相关对象
- docker - 为什么我的金丝雀部署不适用于 istio?
- stored-procedures - Azure Cosmos DB - 在写入数据之前检查容器中的数据