Sunscreen
Sunscreen

Reputation: 3564

Error compiler, cannot convert parameter 2 from MyStruct1 to const void*, for memcpy

I am trying to copy the data from on structure to another. The bytes are identical that each struct can handle are the same. My declarations and the memcpy are below:

    typedef struct{
        CString strNumber;
        CString strAlpha;

    } _Number;

    typedef struct{
        CString strIterration;
        _Number NumberOne;
        _Number NumberTwo;
    } _Store;

_Store Data1;
_Store Data2;

Now let's say that the first struct Data1 has data and the second is just declared.

I am using the following code:

memcpy(&Data2, Data1,   sizeof(_Store));

I cannot compile as the error appears. Any ideas? Any other approaches to copy the data?

Upvotes: 1

Views: 4110

Answers (2)

Geoff Chappell
Geoff Chappell

Reputation: 29

The obvious other approach is simple assignment, i.e., Data2 = Data1;

This saves you from caring how many bytes are in the _Store structure and also from whether CString has an operator =.

Upvotes: 2

sharptooth
sharptooth

Reputation: 170509

You need to use & on both structs:

memcpy(&Data2, &Data1, sizeof(_Store));

Beware: _Store contains CString member variable which (if it is like MFC CString) is not bitwise copyable. You should only use memcpy() on types that are bitwise copyable, otherwise you risk running into undefined behavior.

Upvotes: 7

Related Questions