The function is wrong.
For starters it should be declared like
void copydata( uint8_t *datato, const uint8_t *datafrom, size_t size );
Or even better to declare it like
uint8_t * copydata( uint8_t *datato, const uint8_t *datafrom, size_t size );
Secondly the condition in this loop
for(;CurrentAddress <= StopAddress;CurrentAddress++)
shall look like
for( ; CurrentAddress < StopAddress; CurrentAddress++ )
or
for( ; CurrentAddress != StopAddress; CurrentAddress++ )
and at last you are trying to copy the array to itself
uint8_t *CurrentAddress = datafrom;
//...
*CurrentAddress = *datafrom++;
The function can look the following way
uint8_t * copydata( uint8_t *datato, const uint8_t *datafrom, size_t size )
{
for ( const uint8_t *StopAddress = datafrom + size; datafrom != StopAddress; ++datafrom )
{
*datato++ = *datafrom;
}
return datato;
}
Here is a demonstrative program.
#include <stdio.h>
#include <stdint.h>
uint8_t * copydata( uint8_t *datato, const uint8_t *datafrom, size_t size )
{
for ( const uint8_t *StopAddress = datafrom + size; datafrom != StopAddress; ++datafrom )
{
*datato++ = *datafrom;
}
return datato;
}
int main(void)
{
enum { N = 10 };
uint8_t a[N];
uint8_t b[N / 2] = { 1, 2, 3, 4, 5 };
uint8_t c[N / 2] = { 5, 4, 3, 2, 1 };
copydata( copydata( a, b, N / 2 ), c, N / 2 );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '
' );
return 0;
}
The program output is
1 2 3 4 5 5 4 3 2 1
Another approach is just to use the standard function memcpy
declared in the header <string.h>
. For example
#include <stdio.h>
#include <stdint.h>
#include <string.h>
uint8_t * copydata( uint8_t *datato, const uint8_t *datafrom, size_t size )
{
return ( uint8_t * )memcpy( datato, datafrom, size * ( sizeof( uint8_t ) ) ) + size;
}
int main(void)
{
enum { N = 10 };
uint8_t a[N];
uint8_t b[N / 2] = { 1, 2, 3, 4, 5 };
uint8_t c[N / 2] = { 5, 4, 3, 2, 1 };
copydata( copydata( a, b, N / 2 ), c, N / 2 );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '
' );
return 0;
}
The progran output is the same as shown above.