×

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing area. This function can copy objects from one memory block to another only if the two objects do not overlap at any point.

Implementing the memcpy() function in the C programming language is relatively simple. The logic behind the memcpy() function is elementary.

To implement the memcpy() function, you must cast the source and destination addresses to char*(1 byte). After the type conversion is complete, copy the contents from the source array to the destination address.

You have to split the data by bytes. Repeat this step until n units are completed. where n is the specified bytes of data to copy.

The syntax of the memcpy() function in C: The syntax of the memcpy() function in C is given below

void *memcpy(void *arr1, const void *arr2, size_t n);  

The memcpy() function copies the specified n characters from the source array or location. In this case, arr1 to the destination, which is arr2. Both arr1 and arr2 are pointers to the source and destination locations, respectively.

Parameter of the memcpy() function in C: There are three parameters of the memcpy() function in C. These are-

arr1: This is the first parameter of the function specifying the location of the source memory block. This represents the array that will be copied to the destination.

 arr2: The second parameter of the function specifies the location of the target memory block. This represents the destination array of the memory block.

n: Specifies the number of characters copied from the source to the destination.

Return value of the memcpy() function in C:

The memcpy() function in C is return some value. Returns the pointer that is arr1.

Header file of the memcpy() function in C: The memcpy() function is defined in the string.h header file and must be included in the code that implements the function.

#include <string.h>

Example 1: Here we give an example of the memcpy() function in C.

#include <stdio.h>
#include <string.h>
Int main()
{
const char src[50] = “Tutorial and examples”;
char dest[50];
strcpy (dest, “Hello!”);
printf(“Before memcpy dest= %s\n”, dest);
memcpy(dest, src, strlen(src)+1);
printf(“After memcpy dest = %s\n”, dest);
return 0;
}	

Result: We compile the above program and also share the output of it. The result is given below:

Before memcpy dest= Hello!
After memcpy dest = Tutorial and examples

Example 2: Here we give another example of the memcpy() function in C.

#include <stdio.h>
#include <string.h>
Int main()
{
char str1[] = “Tree”;
char str2[] = “Flower”;
puts(“str1 before memcpy”);
puts(str1);
memcpy (str1, str2, sizeof(str2));
puts(“\nstr1 after memcpy”);
puts(str1);
return 0;
}

Result: We compile the above program and also share the output of it. The result is given below:

str1 before memcpy
Tree
str1 after memcpy
Flower

Example 3: Here we give another example of the memcpy() function in C.

#include <stdio.h>  
#include <string.h>  
int main()  
{  
//initialize the source array,  
//the data will be copied from source to destination/  
int sourcearr[100] = {1,2,3,4,5};  
//this is the destination array  
//data will be copied at this location.  
int destarr[100] = {0};  
//copy the data stored in the sourcearr buffer into destarr buffer  
memcpy(destarr,sourcearr,sizeof(sourcearr));  
//print the data copied into destarr  
printf("destination array content is now changed to\n");  
for(int i=0;i<5;i++){  
printf("%d", destarr[i]);  
}
return 0;
}  

Result: We compile the above program and also share the output of it. The result is given below:

 destination array content is now changed to 12345

Example 4: Here we give another example of the memcpy() function in C.

#include <stdio.h>  
#include <string.h>  
struct  
{  
char name[40];  
int age;  
} prsn1, prsn2;  
int main()  
{  
char firstname[]="Rudra";  
//Using the memcpy() function to copy the data from  
//firstname to the struct  
//add it is as prsn1 name  
memcpy ( prsn1.name, firstname, strlen(firstname)+1 );  
//initialize the age of the prsn1  
prsn1.age=23;  
//using the memcpy () function to copy one person to another  
//the data will be copied from prsn1 to prsn2  
memcpy ( &prsn2, &prsn1, sizeof(prsn1) );  
//print the stored data  
//display the value stored after copying the data  
//from prsn1 to prsn2  
printf ("person2: %s, %d \n", prsn2.name, prsn2.age );  
return 0;  
}  

Result: We compile the above program and also share the output of it. The result is given below:

person2: Rudra, 23

Related Topics

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Break, continue and goto statement in C

Break statement Break statement is used to break the process of a loop (while, do while and for) and switch case. Syntax: break; Example 1 while(test Expression) { // codes if(condition for break){ break; } // codes } Example 2 For(int it, condition,...

1 minute read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Variables in C

A variable can be defined as a name allocated to a storage space that can be manipulated by our programs. Every variable in C arbitrates about the overall layout and...

5 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.