C 向数组中添加结构
所以我的问题如下,我有一个类似超市产品的产品,它的结构由一个标识符标签,它的重量和它的数量组成,我有一个c命令 我想做的是将产品添加到数组中并打印其重量C 向数组中添加结构,c,arrays,structure,C,Arrays,Structure,所以我的问题如下,我有一个类似超市产品的产品,它的结构由一个标识符标签,它的重量和它的数量组成,我有一个c命令 我想做的是将产品添加到数组中并打印其重量 #include <stdio.h> int i = 0; // counts the number of products struct product { char ident[64]; // string that identifies the product eg. "bread" int weight;
#include <stdio.h>
int i = 0; // counts the number of products
struct product
{
char ident[64]; // string that identifies the product eg. "bread"
int weight;
int quant;
};
struct product sistem[10]; // array of products
void add(char ident,int weight,int quant);
struct product make_product(char ident,int weight,int quant)
{
struct product p1 = {ident,weight,quant}; // creates a product and returns the created product
return p1;
}
int main() {
char c; int weight; char ident; int quant;
scanf("%c %[^:]:%d:%d",&c,ident,&weight,&quant);
add(ident,weight,quant);
return 0;
}
void add(char ident,int weight,int quant)
{
printf("New product %d\n",i); //
sistem[i] = make_product(ident,weight,quant); // adds a new product into an array of products
printf("%d\n",sistem[i].weight); //
i++;
}
因此,基本上这并没有保存我在阵列中创建的产品,我似乎无法理解为什么它没有保存
因此,如果有人能帮忙,我将不胜感激。在scanf中,您使用ident作为单个字符,但它应该是一个64字符的缓冲区。此更改将需要更改代码的其他部分以获得char*ident。此外,您不能像这样使用编译时未知的字符串初始化结构成员,因此您必须使用strcpy作为示例。这应该行得通
#include <stdio.h>
#include <string.h>
int i = 0; // counts the number of products
struct product
{
char ident[64]; // string that identifies the product eg. "bread"
int weight;
int quant;
};
struct product sistem[10]; // array of products
void add(char *ident,int weight,int quant);
struct product make_product(char *ident,int weight,int quant)
{
struct product p1 = {"",weight,quant}; // creates a product and returns the created product
strcpy(p1.ident, ident);
return p1;
}
int main() {
char c; int weight; char ident[64]; int quant;
scanf("%c %[^:]:%d:%d",&c,ident,&weight,&quant);
add(ident,weight,quant);
return 0;
}
void add(char *ident,int weight,int quant)
{
printf("New product %d\n",i); //
sistem[i] = make_product(ident,weight,quant); // adds a new product into an array of products
printf("%d\n",sistem[i].weight); //
i++;
}
令人困惑的是,在某些地方,您使用ident来表示字符,而在其他地方,您使用ident来表示字符串。
#include <stdio.h>
#include <string.h>
int i = 0; // counts the number of products
struct product
{
char ident[64]; // string that identifies the product eg. "bread"
int weight;
int quant;
};
struct product sistem[10]; // array of products
void add(char *ident,int weight,int quant);
struct product make_product(char *ident,int weight,int quant)
{
struct product p1 = {"",weight,quant}; // creates a product and returns the created product
strcpy(p1.ident, ident);
return p1;
}
int main() {
char c; int weight; char ident[64]; int quant;
scanf("%c %[^:]:%d:%d",&c,ident,&weight,&quant);
add(ident,weight,quant);
return 0;
}
void add(char *ident,int weight,int quant)
{
printf("New product %d\n",i); //
sistem[i] = make_product(ident,weight,quant); // adds a new product into an array of products
printf("%d\n",sistem[i].weight); //
i++;
}