I have a structure and a enum,
struct __value
{
int a;
enum xsd__boolean *ptr;
}
enum xsd__boolean
{
__true = 1,
__false = 0
};
Is this the right way of doing a malloc to enum and assigning the value,I want to assign the value of __true or __false to *__StructPtr->ptr.Will the size of __true be same as that of int ?
struct __value *__StructPtr;
__StructPtr->ptr = (int *)malloc(sizeof(int));
*__StructPtr->ptr = __true;
Is this is the right way, please provide me some insight about this with some example.
This is incorrect. Your
__StructPtrpointer is not pointed at allocated memory, and thus assigning through the pointer leads to memory corruption.The correct way is something like:
In this case, you’re allocating a
struct __valueon the stack, and then dynamically creating the memory pointed to by__StructPtr->ptr.If you wanted to allocate
__StructPtrand the memory__StructPtr->ptrpoints at both dynamically, try: