Reputation: 653
I have this c++ struct
struct PACKET
{
BOOL isTCPPacket;
BOOL isUDPPacket;
BOOL isICMPPacket;
BOOL isIGMPPacket;
BOOL isARPPacket;
BOOL isIPPacket;
struct PETHER_HEADER
{
string DestinationHost;
string SourceHost;
struct PROTOCOL_TYPE
{
string Name;
WORD Identifier;
} ProtocolType;
} EthernetHeader;
};
and i have
PACKET* Packet;
PACKET* Packets[6];
how can i copy contents of Packet into Packets[3] for example, knowing that the Packet contents will vary for each array in Packets[INDEX]
I have tried memcpy as
memcpy((void*)&Packets[i],(void*)&Packet,sizeof(PACKET));
with no luck
Upvotes: 0
Views: 2949
Reputation: 18451
Ah! Just one thing:
PACKET* Packets[6];
is different than:
PACKET (*Packets)[6];
Former is an array of pointer (i.e. 6-pointers), but latter is a pointer to hold address-of a PACKET[6]
array!
In either case, you must allocate memory for your Packet
variable. memcpy
or assignment will not work, unless some heap or stack memory is allocated.
Upvotes: 0
Reputation: 2126
Hope Packet contains valid object. Also Packets[i] point to allocated objects.
Do this: Packet[i] = Packet;
As string objects store pointers in their objects and can only be duplicated using assignment operator.
Upvotes: 0
Reputation: 258678
How about Packets[3] = Packet;
? This does exactly what you asked (copies the content of Packet
, which is a pointer, into Packets[3]
).
If you want to copy the values, you can use assignment as well: *(Packets[3]) = *Packet;
, assuming the pointers are valid. The compiler generated assignment operator for PACKET
should work just fine.
I'd avoid memcpy
in this case, since some of the members are std::string
- you're bound to run into trouble.
Upvotes: 5