开发者

memcopy and 2d arrays

struct item_CHECK_LIST_data
{

    char    list[MAX_CHECK_LIST_OPTIONS + 1][MAX_ITEM_TEXT_LEN];
    char    checkeditems[MAX_CHECK_LIST_OPTIONS + 1];//which are checked

    char    number_of_optio开发者_如何学Gons;
};

can i copy this with memcopy?

I have a weird bug that would be explained by memcopy not being able to...


Yes, you can memcpy two-dimensional arrays in C, provided that they're actually 2D arrays and not a 1D array of pointers. Two-dimensional arrays are laid out contiguously in memory, so memcpy will correctly read all the blocks. However, if your array is a 1D-array of pointers to more 1D arrays, then the memcpy will only copy the pointers and you'll end up with a shallow copy rather than a deep copy.


Did you use sizeof() to find how much to copy? The elements of the struct may be padded so the size is bigger than the sum of the parts


If it is just the struct as a whole that you want to copy, don't use memcpy. Just do an assignment, the = token is made for this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜