Question

I am trying to implement

void strcpyy(char *s, char *t){
    while(*s++ = *t++){
    }
}

which is an example from K&R. The implementation should be fairly easy but for some reason, that is not the case for me at the moment. So I have the following

int main(){
    char *mess = "hello world";
    char *mess = (char *) malloc(strlen(mess) + 1);
    char *aess;

    strcpyy(aess, mess);

    printf("%s", aess);

    return 0;
}

Every time I run the program, I keep on getting a big list of errors each time I run with -Wall. I would think that to implement and use strcpyy, you would have to malloc space to copy the string and once you do so, you should be able to print out aess which theoratically should contain a copy of mess. Any help would be much appreciated!

Was it helpful?

Solution

It is always a good practice to pay attention to the error messages, especially to the first message (others often are the consequences of the first error). The error message surely indicated the line number corresponding to the line with malloc, and most probably told you what's the problem there.

Correct your program to read:

char *mess = "hello world";
char *aess = (char *) malloc(strlen(mess) + 1);

or the complete function is:

int main(){
    char *mess = "hello world";
    char *aess = (char *) malloc(strlen(mess) + 1);

    strcpyy(aess, mess);

    printf("%s", aess);

    return 0;
}

The problem is that your line

char *mess = (char *) malloc(strlen(mess) + 1);

overwrites the first line

char *mess = "hello world";

and the line

char *aess;

leaves the variable unassigned.

The compile error is because the compiler cannot choose between

char *mess = "hello world";

and

char *mess = (char *) malloc(strlen(mess) + 1);

which introduce a new variable each but with the same name.

OTHER TIPS

The program will look the following way

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

void strcpyy( char *s, const char *t )
{
    while ( *s++ = *t++ );
}

int main( void )
{
    char *t = "hello world";
    char *s = ( char * )malloc( strlen( t ) + 1 );

    strcpyy( s, t );

    printf( "%s\n", s );

    free( s );

    return 0;
}

change

char *mess = "hello world";
char *mess = (char *) malloc(strlen(mess) + 1);
char *aess;

to

char *mess = "hello world";
char *aess = malloc(strlen(mess) + 1);

mess is a string literal, which is hold in read-only memory. you were overwriting it and losing string that you wanted to copy. you need to assign some space for place where you want to copy characters, in your case aess variable. also, remember to free(aess); at the end of your program. if not, you'll get memory leaks.

why are you declaring and defining mess two times? never declare two variables with the same name if they have the same scope ,by doing this you confuse the compiler; I'd recommed using strdup like this :

int main(){
char *mess = strdup("hello world");
char *aess;

strcpyy(aess, mess);

printf("%s", aess);

return 0;

}

or just a plain array like this

int main(){

char mess[] = "hello world";
char *aess;

strcpyy(aess, mess);

printf("%s", aess);

return 0;

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top