LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-12-2008, 11:59 PM   #1
fjkum
Member
 
Registered: Feb 2006
Posts: 38

Rep: Reputation: 15
strcat usage with char* and char []


Hi,

I wrote a simple program in C under linux but now it segfault on me when I run it, below is a snippet of the code:

char *fullstr;
char *pStr = "second";
char arStr[10] = "third";

if( pStr != NULL )
{
strcat( fullstr, pStr );
strcat( fullstr, arStr ); <--- this causes segfault
}

Any idea?
 
Old 12-13-2008, 08:37 AM   #2
elprawn
Member
 
Registered: Feb 2005
Distribution: Gentoo 2008
Posts: 138

Rep: Reputation: 15
Try this...

Code:
char *fullstr;
fullstr = new char[strlen(pStr) + strlen(arStr)];
strcpy(fullstr, pStr);
strcat(fullstr, arStr);
 
Old 12-13-2008, 02:32 PM   #3
mallux
Member
 
Registered: Jun 2007
Location: London, UK
Distribution: Ubuntu, RHEL, Fedora
Posts: 46
Blog Entries: 1

Rep: Reputation: 16
The reason that your program is segfaulting is because you haven't allocated any memory for "fullstr". On my system your program (quite rightly) falls over at the first strcat rather than the second.

elprawn's example will work, but you can only use "new" in C++ rather than C. In C you would have to use malloc (or one of its related cousins such as calloc etc), thus:
Code:
fullstr = malloc(strlen(pStr)+strlen(arStr));
Don't forget to free the memory when you've finished with it:
Code:
free(fullstr);
 
Old 12-13-2008, 05:25 PM   #4
elprawn
Member
 
Registered: Feb 2005
Distribution: Gentoo 2008
Posts: 138

Rep: Reputation: 15
Ah yeah in C. Sorry!
 
Old 12-13-2008, 07:46 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
!= NULL isn't ever necessary except for clarity (unless you have a class in C++ without an operator bool or similar.)

Also:
Code:
fullstr = calloc(strlen(pStr)+strlen(arStr)+1);
Or:
Code:
fullstr = malloc(strlen(pStr)+strlen(arStr)+1);
fullstr[0] = 0x00;
//or...
memcpy(fullstr, pStr, strlen(pStr) + 1);
strcat looks for the end of the first string; it doesn't automatically start from the beginning for the first call to it. You have a 1-(255/256)^n chance of not writing out of bounds with the first call to it, where n is the size of the second string + 1, if you don't make sure a null-character is in place.
ta0kira

Last edited by ta0kira; 12-13-2008 at 08:13 PM.
 
Old 12-13-2008, 10:35 PM   #6
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by elprawn View Post
Code:
fullstr = new char[strlen(pStr) + strlen(arStr)];
Incorrect.
Where is the extra byte for trailing zero?
 
Old 12-13-2008, 11:11 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by ErV View Post
Incorrect.
Where is the extra byte for trailing zero?
Already got that one.
ta0kira
 
Old 12-13-2008, 11:12 PM   #8
elprawn
Member
 
Registered: Feb 2005
Distribution: Gentoo 2008
Posts: 138

Rep: Reputation: 15
Quote:
Originally Posted by ErV View Post
Incorrect.
Where is the extra byte for trailing zero?
Are you talking about null termination? I realised this afterwards but someone else already pointed it out.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I think that is problem with convertin char to const char* shifter Programming 8 10-04-2008 06:00 AM
char by char read from serial port windell Programming 7 08-05-2008 09:40 PM
conversion from ‘const char*’ to ‘unsigned char*’ rubadub Programming 2 02-08-2008 05:45 PM
about C++ invalid conversion from 'const char*' to 'char' teoporta Programming 3 07-17-2007 09:24 AM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM


All times are GMT -5. The time now is 08:09 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration