C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-07-2009, 02:47 AM   #1
Registered User
 
Join Date: Mar 2008
Posts: 39
Post question about strcat() in C

Code:
int main()
{
   
   char* date1="Dec 29, 8:30";
   char* date2="Dec 28, 7:30";
   char* year =" 2009";
   strcat(date1,year);      
   strcat(date2,year);
   return 0;
}
After compiling and executing, the compiler gives an error
Code:
red 312 % time.out
Segmentation fault
After looking at the function strcat(), it takes 2 args of type of pointer and it is what i am passing to strcat(). I dont know i got the error. I need a explanation for it.
Thanks
thungmail is offline   Reply With Quote
Old 11-07-2009, 03:20 AM   #2
Registered User
 
Join Date: Feb 2009
Posts: 35
strcat adds on the second string to the end first string. so you have to leave some space at the end of the first string. for example

Code:
   char date1[20]="Dec 29, 8:30";
   char date2[20]="Dec 28, 7:30";
   char* year =" 2009";
   strcat(date1,year);
   strcat(date2,year);
   printf("%s\n%s\n", date1, date2);
date1 is an array that has a bit of room left over at the end. same with date2. so you can use strcat now since there is room to add year on the end
Brain_Child is offline   Reply With Quote
Old 11-07-2009, 06:57 AM   #3
Jack of many languages
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 1,929
Also, when declaring strings like that, your compiler may mark those strings to be placed into read-only memory when the program is loaded, and thus adding more room to them to do a strcat() later might be a fruitless effort.
__________________
Mac and Windows cross platform programmer. Ruby lover.

Memorable Quotes From Recent Posts:

I can't remember.
Dino is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
An interesting question about strcat meili100 C++ Programming 3 07-07-2009 12:59 PM
Question about pointers #2 maxhavoc C++ Programming 28 06-21-2004 12:52 PM
Question... TechWins A Brief History of Cprogramming.com 16 07-28-2003 09:47 PM
opengl DC question SAMSAM Game Programming 6 02-26-2003 09:22 PM
Question, question! oskilian A Brief History of Cprogramming.com 5 12-24-2001 01:47 AM


All times are GMT -6. The time now is 03:48 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22