[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
Microsoft » bus mastering » SUN » GIF » QbasicFAQ_HowToPaste » TurboCpp » PCI » CppInt » BeginnersGuideToCurl » CSharpKeyword » CMemcpy

(C) memcpy

function for copying one buffer to another.

You can also use it to copy a  ?struct to a char array:

  1. include <stdio.h>
  2. include <assert.h>
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; }


'memcpy' links



last edited (May 9, 2007) by bilderbikkel, Number of views: 2939, Current Rev: 2 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Site Management by Lars Hagelin at Kontantkort.se.