我目前有两个名为“drinks”的 typedef struct
二维数组
//Global Variable
int typenum = 0;
int typetotal = 0;
int classtotal = 0;
/*..skipped other variables... */
typedef struct nodebase{
char productname[254];
char companyname[254];
int productencoding;
int rownum;
int colnum;
int price;
struct nodebase *next;
}drinks;
/* 跳过将使用 typenum、typetotal 和 classtotal 的部分 */
void copy_data(drinks a[3][100],drinks b[3][100],int typenum,int typetotal,int classtotal)
{
memcpy(&b[typenum][classtotal],&a[typenum][typetotal],sizeof(drinks));
}
假设drinks a
肯定拥有typedef struct
中所述的变量中的所有数据,我只想将这些数据“复制并粘贴”到drinks中b
.
但是,用VS2012(Windows)编译代码后,drinks b
为NULL
。有什么建议吗?
*调用:copy_data(a,b,typenum,typetotal,classtotal)
,假设我已声明饮料 a
和 饮料 b
并在我将调用 copy_data
最佳答案
赋值运算符,但要小心结构中包含的指针:
void copy_data(drinks a[3][100],drinks b[3][100],int typenum,int typetotal,int classtotal)
{
// Direct assignment
b[typenum][classtotal] = a[typenum][typetotal];
// This pointer will still be to the old list - probably needs updating?
b[typenum][classtotal].Next = NULL;
}
关于c - 如何将 typedef 结构数组的变量复制并传输到另一个 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19162386/