Memcpy in Arduino How to use it any simple example

What is memcpy function in Arduino?
how can I write below program in Arduino and prints its output overserial monitor.

#include <stdio.h>
#include <string.h>

int main () {
   const char src[12] = "Kashifjaved";
   char dest[12];
   strcpy(dest,"Heloooo!!");
   printf("Before memcpy dest = %s\n", dest);
   memcpy(dest, src, strlen(src)+1);
   printf("After memcpy dest = %s\n", dest);
   
   return(0);
}

memcpy() works the same on an Arduino as any other C/C++ compiler

void setup()
{
  Serial.begin(115200);
  const char src[12] = "Kashifjaved";
  char dest[12];
  strcpy(dest, "Heloooo!!");
  Serial.print("Before memcpy dest = ");
  Serial.println(dest);
  memcpy(dest, src, strlen(src) + 1);
  Serial.print("After memcpy dest = ");
  Serial.println(dest);
}

void loop()
{
}
2 Likes

can i use baud rate 9600 ?

You can use any baud rate that suits you, just like in any other Arduino sketch, but why go so slow ?

1 Like

Make it sizeof(src)

And memcpy in arduino is no different from memcpy in C

i used 9600 it gives me below output but works fine for 115200
□□□□□□□□□□□□□□□□□□□□□□□□□
Before memcpy dest = Heloooo!!
After memcpy dest = Kashifjaved

What is baud rate of your serial monitor? The monitor window has drop down box to select speed

Did you change the baud rate in the sketch to 9600 and set the Serial Monitor to 9600 baud ?

my serial monitor is set for 9600 baud rate

when I changed baud rate from 115200 to 9600 it gives me some weird characters
and when baud rate is 115200 it gives output like this

Before memcpy dest = Heloooo!!
After memcpy dest = Kashifjaved
□□□□□□□□□□□□□□□□□□□□□□□□□

for both scenarios baud rate of serial monitor is 9600

If this is true then your arduino has some serious issues with clock (unlikely)

when i match the baud rate in arduino sketch and serial monitor it works well for 115200 but when I changed both to 9600 it gives some weird character before actual output

what board?

Arduino UNO
When baud rate is 115200


When baud rate is 9600

yes

Then there is something wrong with your setup sketch.ino - Wokwi Arduino and ESP32 Simulator

1 Like

Not surprisingly, when I set both the code and Serial monitor to 9600 baud it still works fine for me

I realise that this is a silly question, but after changing the baud rate in the sketch you did upload the code again, didn't you ?

By the way, I see that you are using IDE 2.0 so that is what I used to test the baud rates

codes is working well when i set both baud rate to 115200
but give unusual output when I set baud rate in sketch and serial monitor to 9600

Anyways thanks for support.
Atleast code is working for me now .

You did not answer my question in reply #16

yes i upload it every time i change baud rate

not sure you're really interested in memcpy

consider


char s [80];

int cnt;

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);

    Serial.println ("ready");
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    cnt++;

    sprintf (s, " %6d cnt", cnt);
    Serial.println (s);

    delay (1000);
}