473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

strcat (run time error)

The following code is giving run-time error.....

char buf[100] = "Hello" ;
strcat(buf,buf) ;

Second line is giving the run-time error.
Any comments please............

Feb 15 '06 #1
6 2292

santosh wrote:
The following code is giving run-time error.....

char buf[100] = "Hello" ;
strcat(buf,buf) ;

Second line is giving the run-time error.
Any comments please............


To concatenate two strings you have to find the end of the first string
and copy each character in the second string into the first until you
reach
the end of that string.
Now, the initial string: "Hello" is:
|H|e|l|l|o|\0|
What you do is go the end and overwrite \0 with the first element from
the
second argument until you get to \0 in the second string. Unfortunately
you
are modifying the string and you will never reach 0. The error appears
when you go beyond the automatically allocated memory:
HelloHelloHello..... until you go beyond the 100 characters. You will
never
reach \0 as you've overwritten it already. The function never stops.
Well,
it stops when it runs out of the allocated memory (100 chars).

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Feb 15 '06 #2
"santosh" <sa***********@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
The following code is giving run-time error.....

char buf[100] = "Hello" ;
strcat(buf,buf) ;

Second line is giving the run-time error.
Any comments please............


When using strcat(char *dest,const char *src), dest and src may not overlap.
Feb 15 '06 #3
santosh wrote:

char buf[100] = "Hello" ;
strcat(buf,buf) ;

Second line is giving the run-time error.


What were you expecting?What were you expecting?
What were you expecting?What were you expecting?
What were you expecting?What were you expecting?
What were y+++NO CARRIER

Feb 15 '06 #4
On 14 Feb 2006 21:16:59 -0800, "santosh" <sa***********@gmail.com>
wrote:
The following code is giving run-time error.....

char buf[100] = "Hello" ;
strcat(buf,buf) ;

Second line is giving the run-time error.
Any comments please............


When you use strcat, you are obligated to insure that the area from
which data is being extracted does not overlap the area into which
data is being deposited. Your code invokes undefined behavior. Be
thankful it resulted in an error as opposed to something pernicious.
Remove del for email
Feb 16 '06 #5
Barry Schwarz wrote:
"santosh" <sa***********@gmail.com> wrote:
The following code is giving run-time error.....

char buf[100] = "Hello" ;
strcat(buf,buf) ;

Second line is giving the run-time error.
Any comments please............


When you use strcat, you are obligated to insure that the area from
which data is being extracted does not overlap the area into which
data is being deposited. Your code invokes undefined behavior. Be
thankful it resulted in an error as opposed to something pernicious.


The way to avoid errors with strcpy and strcat is to not use them.
There is a (non-standard for now) replacement set called strlcpy
and strlcat, which do intelligent things and return intelligent
information. They came out of the FreeBSD operation, I believe.
At any rate an implementation in standard C, with suitable
references, is readily available at:

<http://cbfalconer.home.att.net/download/strlcpy.zip>

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
Feb 17 '06 #6
On 15 Feb 2006 14:18:17 -0800, "Old Wolf" <ol*****@inspire.net.nz>
wrote:
santosh wrote:

char buf[100] = "Hello" ;
strcat(buf,buf) ;

Second line is giving the run-time error.


What were you expecting?What were you expecting?
What were you expecting?What were you expecting?
What were you expecting?What were you expecting?
What were y+++NO CARRIER


<OT>
With (directly interfaced) Hayes-style modems, three plus signs are
_sent_ to enter command mode, which prompts with OK. For older
(non-error-controlled) modulations the modem will usually 'receive'
garbage characters before detecting carrier loss but the probability
of exactly three plus signs and nothing else is vanishingly small.
</>

- David.Thompson1 at worldnet.att.net
Feb 20 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: Nicolas | last post by:
On most implementations of the standard C library, the functions that copy characters in a previously allocated buffer, like strcpy or strcat, are returning the pointer to that buffer. On my...
4
by: lynology | last post by:
I need help trying to figure why this piece of code gives me a "Bad File descriptor error" everytime I try to run it and invoke fflush. This piece of code simple outputs a char string to the output...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
1
by: rw | last post by:
I have a Microsoft C/C++ compiler version 12.00.8168 installed on my PC running Windows 2000. I have no problem to use the MS C/C++ compiler to compile the following 'C' program and get an...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
4
by: nick048 | last post by:
Hi, I have this problem: int n; char nToChar; char firstString = "The number is: "; char resp; /* HERE MAIN WITH THE INPUT OF n*/
4
by: MimiMi | last post by:
I'm trying to put together a http response header. It seems to work, except for that I don't get the data string added correctly! I wouldn't be surprised if my problems have something to do with...
28
by: Mahesh | last post by:
Hi, I am looking for efficient string cancatination c code. I did it using ptr but my code i think is not efficient. Please help. Thanks a lot
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.