Copying a string to another string variable using C memcpy() function

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Copying a string to another string variable using C memcpy() function

To show: How to use memcpy() C function to copy a string to another string variable

 

// Using memcpy()

#include <stdio.h>

#include <string.h>

 

int main(void)

{

char s1[50], s2[] = "Copying this string into s1";

 

memcpy(s1, s2, 50);

printf(" Using memcpy()\n");

printf(" --------------\n");

printf("s1[50] = %s\n", s1);

printf("s2[] = %s\n", s2);

printf("\nAfter s2 is copied into s1 with memcpy(),\n");

printf("using memcpy(s1, s2, 50)\n");

printf("\ns1 contains \"%s\"\n", s1);

return 0;

}

 

Output example:

 

Using memcpy()

--------------

s1[50] = Copying this string into s1

s2[] = Copying this string into s1

After s2 is copied into s1 with memcpy(),

using memcpy(s1, s2, 50)

s1 contains "Copying this string into s1"

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index