Q:

memcpy() function in C with Example

0

memcpy() function in C with Example

memcpy() function

memcpy() is a library function, which is declared in the “string.h” header file - it is used to copy a block of memory from one location to another (it can also be considered as to copy a string to another).

Syntax of memcpy():

    memcpy(void*str1, const void* str2, size_t n);

It copies n bytes of str2 to str1.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Example 1) Copying a string to another (all bytes of a string to another)

#include <stdio.h>
#include <string.h>
#define MAX_CHAR 50

int main(void) {
	char str1[MAX_CHAR] = "Hello World!";
	char str2[MAX_CHAR] = "Nothing is impossible";

	printf("Before copying...\n");
	printf("str1: %s\n",str1);
	printf("str2: %s\n",str2);

	//copying all bytes of str2 to str1
	memcpy(str1, str2, strlen(str2));

	printf("After copying...\n");
	printf("str1: %s\n", str1);
	printf("str2: %s\n", str2);
	
	return 0;
}

Output

Before copying...
str1: Hello World!
str2: Nothing is impossible
After copying...
str1: Nothing is impossible
str2: Nothing is impossible

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now