【发布时间】:2016-07-24 17:45:23
【问题描述】:
我正在尝试创建一个指向单个库存结构(一条记录)的函数,并将其内容复制到inventory.c 中定义的gobal 数组中的一个元素。我想逐个成员地复制它们,如果复制失败,则返回整数-1。在我的主要功能中,我想遍历我的示例数据并为 sampleData Array 中的每个元素调用 invSetRecord 函数。我不知道该怎么做,需要一些帮助。我更新了我的 inventory.c 文件。我对我的功能感到困惑。我是自己编译的。我不明白如何让 productName 复制。有什么建议么?我也很难为库存记录设置计数器。
这是我老师的指示,
使用为实验 9(第 2 部分)创建的 petstore 文件来完成此作业。
将以下示例数据复制到petstore_main.c 文件中:
# define SAMPLE_SZ 5
struct inventory_s sampleData[SAMPLE_SZ] = {
{ 1000, 1.49, 3.79, 10, 0, "Fish Food" },
{ 2000, 0.29, 1.59, 100, 1, "Angelfish" },
{ 2001, 0.09, 0.79, 200, 1, "Guppy" },
{ 5000, 2.40, 5.95, 10, 0, "Dog Collar, Large" },
{ 6000, 49.99, 129.99, 3, 1, "Dalmation Puppy" }
};
在inventory.c文件中定义一个函数,其原型如下(将原型放在inventory.h文件中)
int invSetRecord(struct inventory_s *ipx);
此函数将获取一个指向单个库存结构(一条记录)的指针,并将其内容复制到inventory.c 中定义的全局数组中的一个元素。目标位置(全局数组元素)将由数组的当前大小确定,如果数组为空,则将记录复制到元素 0,如果当前大小为 1,则将记录复制到元素 1,依此类推。意味着您需要在 inventory.c 中定义一个全局计数器,用于跟踪数组中的元素数量。在 invSetRecord 函数中,您需要使用适合类型的方法逐个成员复制结构成员,例如原始数据类型的简单赋值,字符串数据类型的 strcpy()。该函数返回一个整数值:如果复制操作失败(例如全局库存数组已满),该函数返回-1,否则返回0。
在petstore_main.c 的主函数中,您需要创建一个循环,该循环遍历样本数据并为sampleData 数组中的每个元素调用invSetRecord。对于对 invSetRecord 的每次调用,您需要检查函数的返回值,如果操作失败或成功,则打印错误,以及相应的记录编号。例如,如果所有 5 个样本数据记录的操作都成功,则这是从您的 main 函数打印的输出:
record #1 set successfully
record #2 set successfully
record #3 set successfully
record #4 set successfully
record #5 set successfully
如果对 invSetRecord 的调用失败(函数返回 -1),则会打印以下消息:
error: could not set record 1
如果发生错误,您必须在该点跳出循环。
到目前为止,我需要一些帮助。我整个星期都在拔头发,试图弄清楚这一点。提前感谢您的帮助。
//inventory.c
#include <stdio.h>
#include "inventory.h"
int i;
#define invSetRecord main
struct inventory_s inventory[MAX_INVENTORY];
int i;
int invSetRecord(struct inventory_s *ipx)
{
int result;
i = sizeof(MAX_INVENTORY)/sizeof(inventory[0]);
if (i > MAX_INVENTORY)
{
result = -1;
printf("%i", i);
}
if (i < MAX_INVENTORY)
{
result = 0;
printf("%i", i);
inventory[i].productNumber = ipx->productNumber;
inventory[i].mfrPrice = ipx->mfrPrice;
inventory[i].retailPrice = ipx->retailPrice;
inventory[i].numInStock = ipx->numInStock;
inventory[i].liveInv = ipx->liveInv;
//inventory[i].productName= (ipx->productName);
i++;
}
return result;
}
这是我的 inventory.h 文件...
#ifndef _INVENTORY_H_ //ensures that inventory.h does not run more than once
#define _INVENTORY_H_
#define PRODUCTNAME_SZ 20
#define MAX_INVENTORY 50
struct inventory_s
{
int productNumber;
float mfrPrice;
float retailPrice;
int numInStock;
char liveInv;
char productName[PRODUCTNAME_SZ];
};
int invSetRecord(struct inventory_s *ipx);
#endif //_INVENTORY_H_
这是我的 main() 文件...
//main.c
#include <stdio.h>
#include <stdlib.h>
#include "inventory.h"
#define SAMPLE_SZ 5
extern struct inventory_s inventory[MAX_INVENTORY];
struct inventory_s sampleData[SAMPLE_SZ]={
{ 1000, 1.49, 3.79, 10, 0, "Fish Food" },
{ 2000, 0.29, 1.59, 100, 1, "Angelfish" },
{ 2001, 0.09, 0.79, 200, 1, "Guppy" },
{ 5000, 2.40, 5.95, 10, 0, "Dog Collar, Large" },
{ 6000, 49.99, 129.99, 3, 1, "Dalmation Puppy"}
};
int main()
{
int i;
for(i = 0; i < SAMPLE_SZ; i++);
invSetRecord(sampleData);
printf("The product number is %i for sampleData element[]", sampleData[0].productNumber);
return 0;
}
【问题讨论】:
-
对winmain的未定义引用是由于链接器找不到主函数的定义。你自己编译inventory.c吗?您需要同时编译
main.c和inventory.c,以便链接器可以找到int main()的定义(在main.c中)以及在inventory.h 中声明的函数的定义(在inventory.c中) -
我自己编译它,然后在顶部添加了#define invSetRecord main,它现在运行良好。我仍然对将 productName 复制到指针感到困惑。有什么建议吗?
-
我更新了一些 inventory.c 文件。我的结构分配有问题。我的格式看起来正确吗?我试图让结构的指针复制它在函数中指向的内容,所以我可以在我的主函数中为每个 sampleData 调用它。
标签: c function pointers struct structure