LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   strcat() error with gcc (https://www.linuxquestions.org/questions/programming-9/strcat-error-with-gcc-291787/)

sureshkellemane 02-18-2005 05:46 AM

strcat() error with gcc
 
Hi friends

I have one particular problem with this following short code written in C

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

int main()
{
printf("%s",strcat("Hello","World");
return 0;
}

I m getting the error as segmentation fault.

If I run the same code in Tourbo C or Borland C, it works fine.

Please help me what is the problem with this code or where i have gone wrong.

ssobeht 02-18-2005 05:59 AM

maybe it is this parethesis:

printf("%s",strcat("Hello","World"));

anyway thats asyntax error...

sureshkellemane 02-18-2005 06:21 AM

yeh.. sorry

it was typing mistake..

I am getting segmentation fault

sureshkellemane 02-18-2005 06:28 AM

The erronious program is as follows

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

int main()
{
printf("%s",strcat("Hello","World"));
return 0;
}

giving segmentation fault

Marius2 02-18-2005 07:10 AM

Does it work if you explicitly 0-terminate the strings? ( printf("%s",strcat("Hello\0","World\0")); )

sureshkellemane 02-18-2005 07:28 AM

No ..

Still it is giving the same error.

The thing is that the above code works properly under Turbo and Borland compilers

Marius2 02-18-2005 07:57 AM

Ah, silly me, should have RTFM before answering. Of course it's
char *strcat( char *strDestination, const char *strSource ),
therefore you're writing the result into the memory reserved for "Hello". The code should rather look like

#define PLENTY_OF_BYTES 32
char szresult[PLENTY_OF_BYTES];
szresult[0]=0;
strcat(szresult,"Hello");
strcat(szresult," World");

sureshkellemane 02-18-2005 08:06 AM

hi

that I can understand..

but why the same code works in borland compiler and tourbo compiler why not in gcc ..!!!!!

Any clue abt this ?

itsme86 02-18-2005 08:09 AM

Because gcc stores string literals in read-only memory. The same program will probably work if you compile it with gcc -fwritable-strings prog.c -o prog althought you'll have undefined results.

sureshkellemane 02-18-2005 08:29 AM

thanks..atlease we able to remove the segmentation fault.

yes..

it is not giving error like segmentation fault.

But it is giving only the part of the appended string. In the above example code it gives the output

orld

It means , it is missing the first character of second (source)string and not showing first string i.e destination .

You can try in your PC also...!

Why it is like this? Can anybody guide me ?

itsme86 02-18-2005 09:17 AM

Because the only correct way to use strcat() is the way Marius2 showed you.

Hivemind 02-18-2005 09:26 AM

gcc is correct, string literals are null-terminated and of type
Code:

const char*
and they live throughout the lifetime of the program. Writing to one yields undefined results. If a "lesser" compiler happens to accept it without bombing out it's still not something one should rely on. And if you're thinking of using -fwritable-strings option I can inform you that's it's being removed in the upcoming 4.0 version of gcc. So what you want is is what Marius2 showed. Just remember to check if the destination has room for the source (including the \0) or you will get in trouble.

exvor 02-18-2005 11:47 AM

Ohh god that code makes my brain cry

using strcat like this should be banned :P

sureshkellemane 02-18-2005 11:07 PM

Hi friends

there is a food for thought...!

To my knowledge,

strcat("Hello","World");

returning the pointer to string..am i right ?

For example, the below code works properly where p is the pointer to string.

int main()
{
char *p="Suresh";
printf("%s",p);
return 0;
}

why not in the first code or what is the difference between two approach.

awaiting your reply.

itsme86 02-18-2005 11:32 PM

Here's a way to code your own strcat() function:
Code:

char *mystrcat(char *s1, char *s2)
{
  char *s;

  for(s = s1;*s;++s)
    ;

  while(*s2)
    *s++ = *s2++;
  *s = '\0';

  return s1;
}

As you might be able to see, the string in the first parameter gets modified. Which means you can never pass a string literal as the first parameter to strcat() because string literals are stored in read-only memory. You can't modify read-only memory.

Here's a sample program that uses mystrcat() and will work the exact same way with C's strcat():
Code:

#include <stdio.h>

int main(void)
{
  char buf[100];

  strcpy(buf, "hello");
  puts(mystrcat(buf, "world"));

  return 0;
}

Code:

itsme@dreams:~/C$ ./mystrcat
helloworld

I don't know how to explain it any better than that.


All times are GMT -5. The time now is 01:06 PM.