Copy a char array?

Hello,

I'm trying to copy the contents of one array

volatile uint8_t dataString[512];

into another array

uint8_t* pCache=volume.cacheClear();

dataString is a volatile because it is being changed in an interrupt.
pCache is made using the SdFat library by Bill Greiman.
Right now I am using the following call to copy them

strlcpy((char*)pCache,(char*)dataString,512);

This probably isn't the best way to do it... and I'm trying to make sure this goes very fast.
Any thoughts?

Thanks!

This probably isn't the best way to do it.

The string handling routines from the string.h file ARE about the best way to deal with strings. They are extremely well tested, and highly optimized.

I'm concerned that the pCache pointer may not be pointing to enough space to copy the whole dataString array to, but, if you are sure that it is, go ahead.

Thanks for the reply. It seemed like that would be a good function to use, but since I have to cast both of the arguments, does that take more time?

And pCache does point to enough space to hold all 512 bytes. I'm using it now and it works, it just takes more time than I would like.

1 Like

but since I have to cast both of the arguments, does that take more time

No, you're just telling the compiler to ignore any doubts it may have, and reassure it that you know what you are doing.

Interesting...

so it seems like if I were to make a function such as

void copyStrings(uint8_t* to, volatile uint8_t from, int n) {
  for(int ii=0 ; ii<n ; ii++) {
    to[ii] = from[ii];
  }
}

of

void copyStrings(char *dst, const char *src)
{
while (*to++ = *from++) ;
}

it would probably be less computationally efficient?

As far as the actual copying of data, no. But, your function has some limitations. For one thing, it does not assure that from and to are different. For another, it does no checking on the size of the arrays.