[英]Using memcpy from a pointer to an array
I have a typedef struct
named "item" that contains 2 char[254]
(Name of product and Name of company) and 9 int
variables. 我有一个名为“ item”的
typedef struct
,其中包含2个char[254]
(产品名称和公司名称)和9个int
变量。 (Price, Amount,etc..). (价格,金额等)。
I created a pointer from that typedef struct
and one array(1D) and one Two Dimensional array. 我从该
typedef struct
和一个array(1D)和一个二维数组创建了一个指针。
I have used scanf
to store data to the pointer's respective variables (No problem so far). 我已经使用
scanf
将数据存储到指针的各个变量中(到目前为止没有问题)。
Now, I want to "copy and store" the data of the pointer's variables into the array (1D) then "copy and store" the data of the 1D array to the 2D array. 现在,我想将指针变量的数据“复制并存储”到数组(1D)中,然后将1D数组的数据“复制并存储”到2D数组中。
For pointer to 1D array, this is what I did: 对于指向一维数组的指针,这是我所做的:
void pointer_conversion(item *a, item curr[10000], int total)
{
memcpy(&curr[total], a, sizeof(item*));
}
// Tried doing: memcpy(&curr[total],a,sizeof(item*) * 100);
// Why 100?= just to be safe. But still not working.
Now, this function copys and stores the first char[254]
of the pointer a
into 1D array curr
but the rest of the variables of the typedef struct
is NULL
. 现在,此函数将指针
a
的第一个char[254]
复制并存储到1D数组curr
但typedef struct
的其余变量为NULL
。
Any advice? 有什么建议吗?
typedef struct nodebase{
char productname[254];
char companyname[254];
int price;
int stocks;
//....
struct nodebase *next; //Use the struct as linked-list
}item;
Consider what the code fragment does, 考虑一下代码片段的作用,
function returns void/nothing 函数返回空/空
void
function name is pointer_conversion, takes three arguments 函数名称为pointer_conversion,带有三个参数
argument total is an int 参数total是一个int
pointer_conversion(item *a, item curr[10000], int total) {
memcpy takes three arguments, destination, source, and number of bytes to copy memcpy需要三个参数,目标,源和要复制的字节数
how many bytes do you want to copy? 您要复制多少字节? how big is sizeof(item)?
sizeof(item)有多大?
memcpy(&curr[total], a, sizeof(item*)); }
but you probably don't want to copy the item* next element of the item 但您可能不想复制项目*项目的下一个元素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.