Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我可以将一个结构分配给另一个字段结构吗?_C_Structure_Assign - Fatal编程技术网

C 我可以将一个结构分配给另一个字段结构吗?

C 我可以将一个结构分配给另一个字段结构吗?,c,structure,assign,C,Structure,Assign,我正在使用两种结构:Car和List #define MAX_LEN 10 #define NUM 7 typedef struct{ char nr[NUM]; char model[MAX_LEN]; char categ[]; }Car; #define LEN 100 typedef struct{ Car elem[LEN]; int n; }List; 我想将car类型的元素添加到列表中。我试过了 void add(List l,

我正在使用两种结构:Car和List

#define MAX_LEN 10
#define NUM 7
typedef struct{
     char nr[NUM];
     char model[MAX_LEN];
     char categ[];
}Car;

#define LEN 100
typedef struct{
     Car elem[LEN];
     int n;
}List;
我想将
car
类型的元素添加到
列表中
。我试过了

void add(List l, Car c){
      l.elem[l.n] = c;
      l.n ++;
}

但是,当我打印列表时,会出现奇怪的字符,我怀疑这就是问题所在。

您可以通过
指针传递
列表
,并相应地接收

 void add(List* l, Car c){

        strcpy(l->elem[l->n].nr,c.nr);
        strcpy(l->elem[l->n].model,c.model);
        strcpy(l->elem[l->n].categ,c.categ);
        l->n= l->n + 1;
  }
// Call
add(&l,c);

//Function 
void add(List* l, Car c){
    l->elem[l->n] = c;
    l->n++;
}

因此,当您尝试打印时,问题就会出现,但我们看不到执行打印的代码?这是残酷和不寻常的。(实际上,您显示的代码有问题,但与打印问题无关。)阅读有关向函数传递参数的内容,您所做的是创建一个car副本和一个列表副本,并将此新车添加到新列表中。此列表不会编译。您将l作为指针传递,以便访问n,必须使用。