Code:
#include<iostream>
using namespace std;
typedef struct mystruct {
unsigned char uDirFlag;
unsigned int uFileSize;
char uFilename[255];
} teststruct;
int main()
{
char *flatbuf;
teststruct *a = (teststruct*)malloc(sizeof(teststruct));
a->uDirFlag = '0';
a->uFileSize = 55;
strcpy(a->uFilename,"Test message");
int k = sizeof(teststruct);
flatbuf = (char*)malloc(k);
memset(flatbuf,0,k);
strncpy(flatbuf, (char *)a, k); //strncpy fails to copy uFilename properly
teststruct *b = (teststruct*)flatbuf; // b has uFilename field all set to 0
return 0;
}
Can anyone Pls explain me why the the character values in the struct(uFilenam e field) were not copied properly??
Comment