-1

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!

user3754974
  • 279
  • 1
  • 6
  • 15
  • I see you are a new user. Did you know that you should choose one answer as accepted? For this you use the green bird icon next to the number between triangles to the left of the answer. You can also use the up triangle (upvote) on any answer to indicate that it was useful, or the down triangle (downvote) to indicate that it is counterproductive. – Alexander Gelbukh Jul 29 '14 at 04:40
  • isn't give you error like `error: redefinition` for your `char *mess` variable? Also where you allocated space for `char *aess;`?. First make code error free then think about result. – Jayesh Bhoi Jul 29 '14 at 04:40

4 Answers4

0

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.

Alexander Gelbukh
  • 2,104
  • 17
  • 29
0

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;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 4
    [Don't cast malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – M.M Jul 29 '14 at 04:24
  • 1
    @Matt McNabb I am sorry but I do not follow bad advices. – Vlad from Moscow Jul 29 '14 at 04:25
  • 1
    @VladfromMoscow, why do you think that is bad advice? – R Sahu Jul 29 '14 at 04:27
  • 2
    @RSahu It is simply a stupid advice. It had a sense when compilers were very old but now there is no any sense. First of all can you say what type you allocate in this statement p = malloc( n * sizeof( p ) ); ? Secondly it is not excluded that the code will be used with a C++ compiler then your life will not be enough to insert everywhere the casting because C++ compiler does allow such constructions. It is a good style explicitly to specify the casting that to make the program more readable. – Vlad from Moscow Jul 29 '14 at 04:31
  • 1
    @VladfromMoscow it is not excluded that this code will be used with a Perl interpreter, please fix – M.M Jul 29 '14 at 04:45
  • 1
    @VladfromMoscow to make your code more readable, please replace with `(int)((int (*)(char const *,...))(printf))((char const *)"%s\n", (char *)s );` – M.M Jul 29 '14 at 04:49
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.

macfij
  • 3,093
  • 1
  • 19
  • 24
0

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;

}

Farouq Jouti
  • 1,657
  • 9
  • 15