Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C memmove() Function

Posted in C Tutorial
Updated on Oct 06, 2024
By Mari Selvan
👁ī¸ 485 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
C memmove() Function

Photo Credit to CodeToFun

🙋 Introduction

In the realm of C programming, string manipulation is a common and crucial aspect.

The memmove() function is a powerful tool provided by the C Standard Library (string.h) that allows for copying a specified number of bytes from one memory location to another.

In this tutorial, we'll explore the usage and functionality of the memmove() function in C.

💡 Syntax

The syntax for the memmove() function is as follows:

Syntax
Copied
Copy To Clipboard
void *memmove(void *dest, const void *src, size_t n);
  • dest: A pointer to the destination array where the content is to be copied.
  • src: A pointer to the source of data to be copied.
  • n: The number of bytes to copy.

📄 Example

Let's dive into an example to illustrate how the memmove() function works.

memmove.c
Copied
Copy To Clipboard
#include <stdio.h>
#include <string.h>

int main() {
  char source[] = "Hello, C!";
  char destination[20];

  // Using memmove to copy the content
  memmove(destination, source, strlen(source) + 1);

  // Output the result
  printf("Source: %s\n", source);
  printf("Destination: %s\n", destination);

  return 0;
}

đŸ’ģ Output

Output
Source: Hello, C!
Destination: Hello, C!

🧠 How the Program Works

In this example, the memmove() function is used to copy the content of the source array to the destination array, ensuring that the strings do not overlap.

↩ī¸ Return Value

The memmove() function returns a pointer to the destination array (dest).

📚 Common Use Cases

The memmove() function is particularly useful when dealing with overlapping memory regions. Unlike memcpy(), memmove() can handle overlapping memory regions without issues.

📝 Notes

  • Unlike memcpy(), the memmove() function is designed to handle overlapping memory regions. This makes it safer for cases where the source and destination regions overlap.
  • It's essential to use strlen(source) + 1 as the size parameter (n) to ensure that the null terminator of the string is also copied.

đŸŽĸ Optimization

The memmove() function is generally optimized for performance. However, for specific use cases, consider using memcpy() if you are certain that the memory regions do not overlap.

🎉 Conclusion

The memmove() function in C is a valuable tool for copying a specified number of bytes from one memory location to another, especially in scenarios where overlapping memory regions need to be handled gracefully.

Feel free to experiment with different strings and explore the behavior of the memmove() function in various scenarios. Happy coding!

👨‍đŸ’ģ Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
11 months ago

If you have any doubts regarding this article (C memmove() Function) please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy