473,698 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2314

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******** *************@f 14g2000cwb.goog legroups.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*****@inspir e.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.ne t
Feb 20 '06 #7

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

Similar topics

44
5802
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 NetBSD system, man strcpy gives the following prototype : char *strcpy(char * restrict dst, const char * restrict src); What's the point in returning a pointer we already know before the call? Thank you in advance for an explanation.
4
5987
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 stream. What ends up happening instead is that when outputting the first character string to the channel CG_cdukeypad_CHA.Scrpad, its gives the bad file descriptor error, causing the char string not to output. When a second char string is...
81
7305
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 be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
1
1209
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 executable. ********************************* #include <stdio.h> #include <errno.h>
87
5123
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 the library, and other resources related to managed strings are available for download from the CERT web site at: http://www.cert.org/secure-coding/managedstring.html
4
4703
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
3084
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 my still existing confusion over arrays and pointers in C since I'm still a newbie, but anyhow, here's the code: #include <time.h> #include <stdio.h> #define WEBBUF_SIZE 32768
28
3894
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
0
8672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9021
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2323
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.