Reputation: 67
I have the following struct defined in the header file:
typedef struct _wfs_cdm_physicalcu
{
LPSTR lpPhysicalPositionName;
CHAR cUnitID[5];
ULONG ulInitialCount;
ULONG ulCount;
ULONG ulRejectCount;
ULONG ulMaximum;
USHORT usPStatus;
BOOL bHardwareSensor;
} WFSCDMPHCU, * LPWFSCDMPHCU;
And in my code file I try to use it like this:
LPWFSCDMPHCU cdm_physical_cass;
strcpy(cdm_physical_cass->cUnitID, "1234");
cdm_physical_cass->lpPhysicalPositionName = "DISP1";
cdm_physical_cass->bHardwareSensor = FALSE;
cdm_physical_cass->ulInitialCount = 100;
The code compiles fine, however I get access violation on that strcpy so I think the struct is not initialized properly. Any thoughts?
Upvotes: 0
Views: 983
Reputation: 22224
LPWFSCDMPHCU cdm_physical_cass;
is not a struct it's a pointer to a struct of type WFSCDMPHCU
. You must allocate memory for the struct pointed to by cdm_physical_cass
. The function WFMAllocateBuffer
is recommended for that.
Upvotes: 1
Reputation: 800
Since LPWFSCDMPHCU is a pointer and not a struct , you'll need to allocate memory(malloc or new depending of the language) for that pointer ( LPWFSCDMPHCU ) then you can use the fields.
Upvotes: 0
Reputation: 217255
cdm_physical_cass
is not initialized/allocated.
You have to call
LPWFSCDMPHCU cdm_physical_cass = new WFSCDMPHCU;
Better would be to use smart pointers (as std::unique_ptr
).
Upvotes: 4