Pietro Pozzoli
Pietro Pozzoli

Reputation: 33

Writing replacement for strcpy

I have to write a replacement for strcpy without using pointers or the function having a return value... How could this be done?!

this is what i have so far but it uses return values.

void str_copy(char destination [], char source []) {
    int i;
    for (i = 0; source[i] != '\0'; ++i)
        destination[i] = source[i];
    destination[i] = '\0';
    return destination;
}

Upvotes: 0

Views: 344

Answers (3)

user3629249
user3629249

Reputation: 16540

suggest writing the function this way:

void str_copy( char destination [], const char source [] ) 
{
    for(size_t i=0; source[i]; i++ )  destination[i] = source[i];
    destination[i] = '\0';
}

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310920

Just remove the return statement.

The function can look the following way

void str_copy( char destination [], const char source [] ) {
    size_t i = 0;
    while ( destination[i] = source[i] ) i++;
}

Upvotes: 4

SteveO
SteveO

Reputation: 131

Why do you have to return destination? Since you passed the address of the first element by using the empty brackets, the destination array should be changed without the return.

Upvotes: 1

Related Questions