×

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

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

Difference between while and for loop in C

While Loop The syntax that can be used for the ‘while’ loop in the C programming language is mentioned below: while (test expression or termination condition)  {   // the main body of the...

6 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes 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.

Recursion in C

Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a...

1 minute read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.