-3

Unfortunately, this Code seems to prints something before it prints the actual text. What is this? And why is it printed?

"\300\367\277\357\376"

I realized this more often in my code and I am sure it implies I have done something wrong.

char* concat(const char *s1, const char *s2);
int main(int argv, char* args[]){
    char lastchars[50];
    char *buf;
    while(1){
        gets(lastchars);
        if(strlen(lastchars) == 0)break;
        buf = concat(buf, lastchars);
    }
    printf("%s",buf);
 }
 char* concat(const char *s1, const char *s2)
 {
    char *result = malloc(strlen(s1) + strlen(s2) + 1); 
    strcpy(result, s1);
    strcat(result, s2);
    return result;
 }
TVSuchty
  • 133
  • 6

1 Answers1

0

Even if we look past the call to the unsafe and no longer supported function gets, there are numerous problems with this code, starting with your concat function:

char* concat(const char *s1, const char *s2)
{
   char *result = malloc(strlen(s1) + strlen(s2) + 1); 
   strcpy(result, s1);
   strcat(result, s2);
   return result;
}

You are calling concat with the line: buf = concat(buf, lastchars); when buf is unintialized. In other words:

char *result = malloc(strlen(buf) + strlen(lastwords) + 1);
strcpy(result, buf);
strcat(result, lastwords);
return result;

What is strlen(buf)? This is undefined behavior since buf doesn't contain a string with a null-terminator.

Then, strcpy(result, buf) is also undefined behavior since buf isn't initialized.

Then you return result and assign it back into buf.

It is very difficult to decipher your intent with this code, so I cannot offer a solution - only point out the numerous problems.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • I actually use a book of network programming, which code was written 1999. I never used C before... It is difficult to once learn network programming (or at least standard IO stuff, so far), learn C and differentiate between the old C and the new C... The old C is not well supported in those ages (Basically why it should...) I am just trying to read my typed code into a file (excersice) Eventually my Overflow account will get banned soon, because of to many stupid questions... Thanks for your answer! – TVSuchty Feb 13 '19 at 18:26
  • 1
    Which 1999 networking text book, @TVSuchty? Are you sure there isn't a more recent one that would do the job better. Are you sure you copied the code correctly? (It's far from impossible that there is an error such as you've typed, but it is not likely to be in a top-notch book, even from that era.) – Jonathan Leffler Feb 13 '19 at 20:56
  • No: I wrote the code, trying to extract the essence of the lecture. It is a german book: https://www.amazon.de/Unix-Netzwerkprogrammierung-mit-Threads-Sockets-X-systems-press/dp/3540002995/ref=sr_1_2?ie=UTF8&qid=1550094008&sr=8-2&keywords=netzwerkprogrammierung – TVSuchty Feb 13 '19 at 21:40
  • 1
    I don't read German sufficiently well to be able to assess the book — I'm sorry. I've not heard of it before; I've not seen any commentary on it. Be cautious if this is really what it says, @TVSuchty. It might be an isolated problem; it might be indicative of other problems in the examples. It gets reasonable reviews at Amazon, but that isn't as reliable as anyone would like. – Jonathan Leffler Feb 13 '19 at 22:00