我们说我有一家太阳镜店。 我的结构(从下到上):
typedef struct Model
{
int m_Num;
int m_amount;
}model;
typedef struct Brand
{
char b_name[20];
int b_amount;
int b_NumOfModels;
model ** models;
}brand;
typedef struct Store
{
char * s_Name;
int s_NumOfBrands;
int s_TotalSg;
brand ** brands;
}store;
我已经建立了#34; createStore / Brand / Model"功能 - 检查,完美工作。 我想要注意的下一部分是在我创建了一个商店之后,我希望能够添加更多商店/品牌/型号。 我试过添加一些随机品牌:
store * insertBrand(store*, char*);
作为反馈,如果品牌存在或不存在,我想收到一条消息:
int main()
{
store* moshSstore = createStore();
store* insertBrand();
return 0;
}
store * insertBrand(store *s, char* brandName)
{
int i, j, found;
for (i = 0; i < s->s_NumOfBrands; i++)
{
if (strcmp(s->brands[i]->b_name, brandName) == 0)
break;
}
if (i < s->s_NumOfBrands) //found
printf("brand has been found");
else //not found
printf("brand not found");
}
这是我的代码。我试图调试,但我不明白为什么insertBrand
函数不起作用(根本不工作)。
答案 0 :(得分:2)
首先,您必须检查您的创建功能。您也已将此行放在main中而没有任何参数store* insertBrand();
您没有将任何值返回到insertBrand
- 但您在定义函数中有一个返回类型store*
。您必须确保在创建结构时初始化结构并确保品牌初始化。
在你的main函数中你输入了一个函数声明:
store* insertBrand();
这不是对insertBrand
的调用;它只是一个(非原型)函数声明。
答案 1 :(得分:0)
我很抱歉这个烂摊子,不想用很多代码提出太大的问题。 但是,对于那些问:
store * createStore()
{
char name[SIZE];
int i, j;
store* newStore = (store*)malloc(sizeof(store));
if (newStore == NULL)
{
puts("allocating error");
free(newStore);
return NULL;
}
puts("enter store name : (32 ch. MAX");
getchar(name);
gets(name);
newStore->s_Name = strdup(name);
if (newStore->s_Name == NULL)
{
puts("allocating error");
free(newStore->s_Name);
free(newStore);
}
puts("enter total number of sunglasses : ");
scanf("%d", &(newStore->s_TotalSg));
puts("enter number of brands : ");
scanf("%d", &(newStore->s_NumOfBrands));
newStore->brands = (brand**)calloc(sizeof(newStore->s_NumOfBrands), sizeof(brand*));
if (newStore->brands == NULL)
{
puts("allocating error");
free(newStore->s_Name);
free(newStore->s_NumOfBrands);
free(newStore->s_TotalSg);
free(newStore->brands);
free(newStore);
return NULL;
}
for (i = 0; i < newStore->s_NumOfBrands; i++)
{
newStore->brands[i] = createBrand();
if (newStore->brands[i] == NULL)
{
puts("allocating error");
for (j = 0; j < i; j++)
free(newStore->brands[j]);
free(newStore->s_Name);
free(newStore->s_NumOfBrands);
free(newStore->s_TotalSg);
free(newStore->brands);
free(newStore);
return NULL;
}
}
return newStore;
}
对我的问题有帮助吗?
答案 2 :(得分:0)
您好,您的功能包含一些错误,所以我会在这里列出它们: 1.为什么要尝试在之前测试过的指针上应用自由函数,它是NULL和整数
free(newStore); // line 9 of the function
free(newStore->s_Name); // line 19
free(newStore->s_NumOfBrands); // line 33 and 48
free(newStore->s_TotalSg); // line 34 and 49
我认为此行不正确
newStore-&gt; brands =(品牌**)calloc(sizeof(newStore-&gt; s_NumOfBrands),sizeof(品牌*));
你想这样做我想:
newStore->brands = (brand**)calloc(newStore->s_NumOfBrands, sizeof(brand*));
这是一个快速清理你的代码检查并改进它也检查你createBrand
store * createStore()
{
char name[SIZE];
int i, j;
store* newStore = (store*)malloc(sizeof(store));
if (newStore == NULL) {
puts("allocating error");
return NULL;
}
puts("enter store name : (32 ch. MAX");
getchar(name); // you can use scanf instead of this two lines
gets(name);
newStore->s_Name = strdup(name);
if (newStore->s_Name == NULL) {
puts("allocating error");
free(newStore);
return NULL;
}
puts("enter total number of sunglasses : ");
scanf("%d", &(newStore->s_TotalSg));
puts("enter number of brands : ");
scanf("%d", &(newStore->s_NumOfBrands));
newStore->brands = (brand**)calloc(newStore->s_NumOfBrands, sizeof(brand*));
if (newStore->brands == NULL) {
puts("allocating error");
free(newStore->s_Name);
free(newStore);
return NULL;
}
for (i = 0; i < newStore->s_NumOfBrands; i++) {
newStore->brands[i] = createBrand();
if (newStore->brands[i] == NULL) {
puts("allocating error");
for (j = 0; j < i; j++)
free(newStore->brands[j]);
free(newStore->s_Name);
free(newStore->brands);
free(newStore);
return NULL;
}
}
return newStore;
}