memcpy Function in C

Level 4267 keystrokes

Uses memcpy to copy an entire struct from source to destination memory.

C code

// Define simple Point structure
1struct Point {
2 int x, y;
3};
4
// Source struct with values
5struct Point src = {100, 200};
// Destination struct (uninitialized)
6struct Point dest;
// memcpy(destination, source, byte count) copies the raw bytes
7memcpy(&dest, &src, sizeof(struct Point));
// Print source values
8printf("Source: (%d, %d)\n", src.x, src.y);
// Print copied values in destination
9printf("Dest: (%d, %d)\n", dest.x, dest.y);
// Two 4-byte ints: 8 bytes on common systems
10printf("Size copied: %lu bytes\n", sizeof(struct Point));

Output

Source: (100, 200)
Dest: (100, 200)
Size copied: 8 bytes
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • ,× 11
  • ;× 8
  • (× 8
  • )× 8
  • "× 6
  • %× 5
  • .× 4
  • :× 3
  • \× 3
  • {× 2

More C examples at level 4

← C typing practice