简体   繁体   English

在我的C程序中遇到结构和数组问题

[英]Having trouble with structures and arrays in my C program

***** CODE UPDATED WITH NEW QUESTION ***** *****用新问题更新代码*****

I'm having trouble with building the 1st table from my list below of what I'm trying to output. 我在尝试输出的下面的列表中构建第一个表时遇到问题。

[This is a photo of how the output is SUPPOSED look][1] (notice the 1st table) [这是有关输出外观的照片] [1](请注意第一个表)

[This is a screenshot of what my below code is outputting][2] [这是我下面的代码输出的屏幕截图] [2]

I can't get the first table to descend by quantity of each item sold. 我无法使第一个表格按所售商品的数量下降。


My objective: 我的目标:

I'm trying to create a C program to calculate the total sales of a company that collects data from 2 input files and outputs these 3 things: 我正在尝试创建一个C程序来计算一家公司的总销售额,该公司从2个输入文件中收集数据并输出以下3件东西:

1) Quantity of items sold listed in descending order of quantity of each item sold displayed in a table categorized with headers "Item Number", "Item Description" and "Quantity Sold". 1) 出售商品 数量,按表中显示的每个出售商品数量从大到小的顺序分类,标题为“商品编号”,“商品描述”和“出售数量”。

2) Total sales of each item listed in descending order of total sales (total sales = quantity sold * unit price) displayed in a table categorized with headers "Item Number", "Item Description", "Quantity Sold", "Unit Price" and "Total Sales". 2)在标题为“项目编号”,“项目说明”,“已售数量”,“单价”分类的表中显示总销售额的降序列出的每个项目 总销售额 (总销售额=售出数量*单价)和“总销售额”。

3) The total sales value (the sum of each total from #2 above) 3) 总销售额 (上述#2的总和)

Lets take the prototype declaration 让我们接受原型声明

int readItemList(struct items[]);

Here you declare a function readItemList which returns an int , and as argument take a pointer to a struct item . 在这里,您声明一个函数readItemList ,该函数返回一个int ,并以指向struct item的指针作为参数。 Almost but not quite what you want. 几乎但不是您想要的。

What you want is for the function to take a pointer to struct Item (note upper-case I ): 您想要的是该函数采用一个指向struct Item的指针(请注意大写的I ):

int readItemList(struct Item *items);

The second error is because you don't include <string.h> . 第二个错误是因为您不包含<string.h>


However those are just problems that the compiler detects, you have an even worse error that will lead to undefined behavior and most likely lead to a crash when you run the program: You use an uninitialized local variable. 但是,这些仅仅是编译器检测到的问题,您有一个更糟糕的错误,它将导致未定义的行为,并且很可能在运行程序时导致崩溃:您使用未初始化的局部变量。

In the main function you have main功能中

//Creating dynamic array of structure
struct Item *items;

The problem is that you don't actually create anything. 问题是您实际上没有创建任何东西。 You just pass the pointer, uninitialized (which means its value is indeterminate ) to the readItemList function, and then readItemList treat it as an already allocated array. 您只需将未初始化的指针(这意味着其值是不确定的 )传递给readItemList函数,然后readItemList将其视为已分配的数组。 Nowhere do you allocate memory for this pointer. 您无处为该指针分配内存。

The easiest solution is to make items an array in the main function: 最简单的解决方案是在main函数中使items成为数组:

struct Item items[200];

Unless you have more than 200 items in the file that would suffice, you might even make the array smaller. 除非文件中有200多个项目就足够了,否则您甚至可以缩小数组。

Another solution is to actually dynamically allocate the memory for the items, either in main before passing it to the readItemList function. 另一个解决方案是在将内存传递给readItemList函数之前,先在main动态分配内存。 Or have the readItemList function allocate it, but then you need to change it to take a pointer to the variable, emulating pass by reference, and have readItemList allocate and reallocate as needed. 或者让readItemList函数分配它,但是然后您需要对其进行更改以获取指向该变量的指针,模拟按引用传递,并让readItemList根据需要分配和重新分配。

In your prototypes, you are not declaring a function which takes an array of struct Item . 在您的原型中,您没有声明一个函数,该函数采用an array of struct Item struct items[] does not mean anything sensible here (there is no struct like struct items . You must use struct Item items[] , like type identifier[] . Another example may be struct Item array_name[] . For the problem with strcmp , you need to include string.h . That's why it gives the implicit declaration warning. struct items[]在这里并不意味着任何意义(没有像struct items这样的struct items 。您必须使用struct Item items[] ,如type identifier[] 。另一个示例可能是struct Item array_name[] 。对于strcmp的问题,您需要包含string.h ,这就是为什么它会给出implicit declaration警告的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM