Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

c - Find what's wrong with this function?

I think there is no problem with this segment of code.

void copydata(uint8_t *datato, uint8_t *datafrom, int size)
{
    uint8_t *CurrentAddress = datafrom;
    uint8_t *StopAddress = datafrom + size;
    
    for(;CurrentAddress <= StopAddress;CurrentAddress++)
        *CurrentAddress = *datafrom++;

}
question from:https://stackoverflow.com/questions/65943353/find-whats-wrong-with-this-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...