c - 如何将 typedef 结构数组的变量复制并传输到另一个 - C

标签 c arrays struct

我目前有两个名为“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 bNULL。有什么建议吗?

*调用: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/

相关文章:

c - free() 错误(使用 valgrind 调试)?

c++ - 当你逻辑不是 float 时会发生什么?

c# - 将 Javascript 对象传递给 C#

javascript - 正则表达式:如何删除所有未连接到图形的字符(特定字符旁边)

c - 在 C 中为节点分配了多少内存?

c - 如何从名称与结构重合的 Swift 中调用 C 函数?

c - 使用自 Action 为参数

c - C 中带有 malloc 和 realloc 的动态字符串数组不会退出循环

c - 新程序员来了,这个堆栈程序有什么问题吗?

c++ - 为什么我不能打印出我的宏创建结构的所有成员名称?