[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CMemcpy
You can also use it to copy a ?struct to a char array:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CMemcpy
(C) memcpy
function for copying one buffer to another.You can also use it to copy a ?struct to a char array:
typedef struct { char name[12]; /* No terminator */ char date[8]; /* No terminator */ char age[2]; /* No terminator */ } MyStruct; int main(int argc, char* argv[]) { /* Create a struct */ MyStruct m; /* Create an array */ char a[sizeof(MyStruct) + 1] = { '\0' }; { /* Some compile-time asserts */ const char temp1[ (sizeof(m.name ) == 12 ? 1 : 0) ] = { '\0' }; const char temp2[ (sizeof(m.date ) == 8 ? 1 : 0) ] = { '\0' }; const char temp3[ (sizeof(m.age ) == 2 ? 1 : 0) ] = { '\0' }; const char temp4[ (sizeof(MyStruct) == 22 ? 1 : 0) ] = { '\0' }; } /* Initialize the struct */ memcpy(m.name,"Bilderbikkel",sizeof(m.name)); /* No terminator */ memcpy(m.date,"20070509",sizeof(m.date)); /* No terminator */ memcpy(m.age,"26",sizeof(m.age)); /* No terminator */ /* Copy the struct to the array */ memcpy(a,&m,sizeof(MyStruct)); /* Add a terminator to the array */ a[sizeof(MyStruct)] = '\0'; /* print the array */ printf("'%s'",a); /* Finished ! */ return 0; }
- include <stdio.h>
- include <assert.h>
'memcpy' links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
