473,685 Members | 3,165 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strcat problem again

Hi,
Continuing my strcat segmentation fault posting-
I have a problem which occurs when appending two sting literals using
strcat.
I have tried to fix it by writing my own function that does the strcat
(mystract). Program below.
However this appears not to have fixed the problem and I don't know why it
shouldn't ?
Any further help as to what else I am doing wrong will be appreciated
regards
Ian.
The idea of the program is to:
#enter a integer to convert to words
#99 -- input
#ninety nine --output
Ps tried returning sprintf and using macros with no luck.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* convertLessThan OneThousand(int number);
char* convert(int number);
char* mystrcat(char* dest, char* source);
static int num;
/*static char dest[1024];*/
static char *numNames[] = {
"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen"
};
static char *tensNames[] = {
"",
" ten",
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
};
char *majorNames[] = {
"",
" thousand",
" million",
" billion",
" trillion",
" quadrillion",
" quintillion"
};
int main(){
printf("enter a integer to convert to words ");
scanf("%d", &num);
printf("convert ed to words: %s\n", convert(num));
return 0;
}

char* convertLessThan OneThousand(int number) {
char* soFar;
/*char buffer[10000];
char* soFar;
soFar = buffer;*/
/* char result[256];*/
/*char* hundred = "hundred";*/
char hundred[] = "hundred";
if (number % 100 < 20){
soFar = numNames[number % 100];
number /= 100;
}
else {
soFar = numNames[number % 10];
number /= 10;

soFar = mystrcat(tensNa mes[number % 10], soFar);
number /= 10;
}
if (number == 0)
return soFar;
/*return numNames[number] + " hundred" + soFar;*/
/*sprintf(result , "%s%s%s", numNames[number], hundred, soFar);*/
return mystrcat(numNam es[number], mystrcat(hundre d, soFar));
}

char* convert(int number) {
char* zero = "zero";
if (number == 0) {
return zero; }
char* prefix = "";
char* soFar = "";
int place = 0;
do {
int n = number % 1000;
if (n != 0){
char* s = convertLessThan OneThousand(n);
/*soFar = s + majorNames[place] + soFar;*/
soFar = mystrcat(s, mystrcat(majorN ames[place], soFar));
}
place++;
number /= 1000;
} while (number > 0);
/*return (prefix + soFar).trim();*/
return (mystrcat(prefi x, soFar));
}

char* mystrcat(char* dest, char* source){
while(*dest){}
dest--;
while(*dest++ = *source++){}
return dest;
}
Nov 13 '05 #1
18 6166
"Ian Stanley" <ia********@hot mail.com> wrote in
news:3f******** @news.chariot.n et.au:
Hi,
Continuing my strcat segmentation fault posting-
I have a problem which occurs when appending two sting literals using
strcat.
I have tried to fix it by writing my own function that does the strcat
(mystract). Program below.
However this appears not to have fixed the problem and I don't know why
it shouldn't ?


Is there *any* reason you can't fire up a debugger and single step through
this code? This is what I do when I simply cannot see my mistake.

--
- Mark ->
--
Nov 13 '05 #2
srtcat requires that the memory it should write to is already provided
... sounds like you're writing over your buffer and the system seg faults
...
i didn't read the full source .. so maybe i'm just writing nonsense..

Clemens

On Thu, 18 Sep 2003 00:05:28 +0930
"Ian Stanley" <ia********@hot mail.com> wrote:
Hi,
Continuing my strcat segmentation fault posting-
I have a problem which occurs when appending two sting literals using
strcat.
I have tried to fix it by writing my own function that does the strcat
(mystract). Program below.
However this appears not to have fixed the problem and I don't know
why it shouldn't ?
Any further help as to what else I am doing wrong will be appreciated
regards
Ian.
The idea of the program is to:
#enter a integer to convert to words
#99 -- input
#ninety nine --output
Ps tried returning sprintf and using macros with no luck.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* convertLessThan OneThousand(int number);
char* convert(int number);
char* mystrcat(char* dest, char* source);
static int num;
/*static char dest[1024];*/
static char *numNames[] = {
"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen"
};
static char *tensNames[] = {
"",
" ten",
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
};
char *majorNames[] = {
"",
" thousand",
" million",
" billion",
" trillion",
" quadrillion",
" quintillion"
};
int main(){
printf("enter a integer to convert to words ");
scanf("%d", &num);
printf("convert ed to words: %s\n", convert(num));
return 0;
}

char* convertLessThan OneThousand(int number) {
char* soFar;
/*char buffer[10000];
char* soFar;
soFar = buffer;*/
/* char result[256];*/
/*char* hundred = "hundred";*/
char hundred[] = "hundred";
if (number % 100 < 20){
soFar = numNames[number % 100];
number /= 100;
}
else {
soFar = numNames[number % 10];
number /= 10;

soFar = mystrcat(tensNa mes[number % 10], soFar);
number /= 10;
}
if (number == 0)
return soFar;
/*return numNames[number] + " hundred" + soFar;*/
/*sprintf(result , "%s%s%s", numNames[number], hundred, soFar);*/
return mystrcat(numNam es[number], mystrcat(hundre d, soFar));
}

char* convert(int number) {
char* zero = "zero";
if (number == 0) {
return zero; }
char* prefix = "";
char* soFar = "";
int place = 0;
do {
int n = number % 1000;
if (n != 0){
char* s = convertLessThan OneThousand(n);
/*soFar = s + majorNames[place] + soFar;*/
soFar = mystrcat(s, mystrcat(majorN ames[place], soFar));
}
place++;
number /= 1000;
} while (number > 0);
/*return (prefix + soFar).trim();*/
return (mystrcat(prefi x, soFar));
}

char* mystrcat(char* dest, char* source){
while(*dest){}
dest--;
while(*dest++ = *source++){}
return dest;
}

Nov 13 '05 #3
Ian Stanley wrote:
Hi,
Continuing my strcat segmentation fault posting-
I have a problem which occurs when appending two sting literals using
strcat.
You can't concatenate string literals *at all*. To concatenate them,
you'd have to update the first one, but you're not permitted to update
a string literal. BOOM today.

Witness:
static char *numNames[] = {
"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen" return mystrcat(numNam es[number], mystrcat(hundre d, soFar)); char* mystrcat(char* dest, char* source){
while(*dest){}
dest--;
while(*dest++ = *source++){}
return dest;
}


If mystrcat worked at all [which it won't, since the ++ is missing in
the first while loop], you'd be updating one of the string literals
in numNames. No can do. You'll need to copy your literals into a
BIG ENOUGH buffer anyway.

Also witness:

char* convert(int number) {
char* zero = "zero";
if (number == 0) {
return zero; }
char* prefix = "";

You're either using C99 or C++, not the usual C, which doesn't allow
declarations following statements.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #4
"Ian Stanley" <ia********@hot mail.com> wrote:
However this appears not to have fixed the problem and I don't know why it
shouldn't ?


It doesn't fix the problem because, despite repeated hints and indeed
downright shoves and pushes in that direction, you seem incapable of
understanding that _one cannot write to a string literal_. Stop doing
that, and you've solved half of your problem.
Get your loops right and think about what you actually want to do, and
you just might solve the other half. But in any case, stop asking the
same question over and over again without really thinking about the
answers you get, because you won't solve anything _this_ way.

Richard
Nov 13 '05 #5

"Ian Stanley" <ia********@hot mail.com> wrote in message

char* mystrcat(char* dest, char* source){
while(*dest){}
dest--;
while(*dest++ = *source++){}
return dest;
}

You missed a ++ on the first while loop.

In general, don't write this sort of C. You are using C idioms that are
meaningless to anyone not fluent in the language. Even for those who know C
well, this sort of cryptic code is hard to read and dry run.

How about

char *mystrcat(char *dest, char *source)
{
while(*dest)
dest++;

do
{
*dest = *source;
dest++;
} while(*source++ );

return dest;
}
Nov 13 '05 #6
On Thu, 18 Sep 2003 00:05:28 +0930, "Ian Stanley"
<ia********@hot mail.com> wrote:
Hi,
Continuing my strcat segmentation fault posting-
I have a problem which occurs when appending two sting literals using
strcat.
[snip]
char* mystrcat(char* dest, char* source){
while(*dest){}
dest--;
while(*dest++ = *source++){}
return dest;
}


This is UB. You cannot write to a string literal. You have
to allocate a new buffer for the newly created string thus:

char* mystrcat( char* dest, char* source )
{
char *newstring;

newstring = malloc( strlen( dest ) + strlen( source ) + 1 );

if ( newstring )
{
strcpy( newstring, dest );
strcat( newstring, source );
}
return newstring;
}

Nick.

Nov 13 '05 #7
"Mark A. Odell" <no****@embedde dfw.com> wrote:
"Ian Stanley" <ia********@hot mail.com> wrote:
Continuing my strcat segmentation fault posting-
I have a problem which occurs when appending two sting literals using
strcat.
I have tried to fix it by writing my own function that does the strcat
(mystract). Program below.
However this appears not to have fixed the problem and I don't know why
it shouldn't ?


Is there *any* reason you can't fire up a debugger and single step through
this code? This is what I do when I simply cannot see my mistake.


The debugger tells them that the error is on (*dst++ = *src++) ... dst
is as expected as is src. How does the debugger help in this case?

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Nov 13 '05 #8
qe*@pobox.com (Paul Hsieh) wrote in
news:79******** *************** ***@posting.goo gle.com:
> Continuing my strcat segmentation fault posting-
> I have a problem which occurs when appending two sting literals using
> strcat.
> I have tried to fix it by writing my own function that does the
> strcat (mystract). Program below.
> However this appears not to have fixed the problem and I don't know
> why it shouldn't ?


Is there *any* reason you can't fire up a debugger and single step
through this code? This is what I do when I simply cannot see my
mistake.


The debugger tells them that the error is on (*dst++ = *src++) ... dst
is as expected as is src. How does the debugger help in this case?


How? You're a kidding I assume. If not, let's investigate this. Either
writing to dst is at fault or reading from src is. A quick read of src
with the debugger shows that not to be a problem. Then we look at what dst
points to. Hmm. Funny, it seems to be in the same area as the code (on
this implementation) maybe I am not able to write to that memory region.
Okay, dst points to my list of string literals. I wonder if you can't
write to string literals. Let me make a tiny program with a string literal
and a normal array and try writing to each memory region. I'll use my
debugger again to see which write barfs. Wow, the write to the string
literal. Okay so I can't write to string literals. Now I will change my
program such that I don't do this anymore.

--
- Mark ->
--
Nov 13 '05 #9
On 18 Sep 2003 06:06:15 -0700, qe*@pobox.com (Paul Hsieh) wrote:
The debugger tells them that the error is on (*dst++ = *src++) ... dst
is as expected as is src. How does the debugger help in this case?


That statement performs several operations. So you replace that line
with separate statements for reading, writing and the increments:

{
char tmp;

tmp = *src;
*dst = tmp;
dst++;
src++;
}

Recompile and run again with the debugger. This tells you that
the problem is in the line *dst = tmp so you can conclude that
you are not allowed to write to that area of memory.

Nick.

Nov 13 '05 #10

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

Similar topics

14
5451
by: Patrick Coleman | last post by:
Hi, I have the following code: char request = "GET "; strcat(request, path); //Path is the path section of a url ie. "/path/test.htm" strcat(request, " HTTP/1.1"); cout<<request<<"\n"; //This outputs "GET /path/test.htm HTTP/1.1" happily //Send packet (I know there's no error checking, this is just an example)
10
5322
by: Nicholas | last post by:
How can I use strcat in the following scenario? char line has the following content "(360+c)" and not declared as string literal The purpose is that I want to prepend '0-' in front of line, for my formula to work. I tried to do the following, but prev still contains the original "(360+c)" and final gives me garbage characters char prev = '0'; strcpy(var, line);
5
5561
by: Kevin C. | last post by:
Never mind my last post about pointer subtraction, I traced the code and found the offender to be strcat. char call = "del "; system(strcat(strcat(call, del->table_name), ".tab")); After this line, trying to dereference del results in page faults or garbage. This makes me wonder how strcat is implemented, in terms of what it actually does with the arguments. Seems like it actually moves the pointer. Anyone know?
8
539
by: ctara_shafa | last post by:
Hi, I have a following problem: I'm creating a list and one of the fields should contain the date. Firstly I ask the user for the year, month and day and then I'd like to collect all this data in one field. To do this I tried to concatenate those 3 numbers into one using strcat. A piece of code is as follows /*function to enter the date*/ char * date (void) {
16
5158
by: Lilith | last post by:
I'm working on a simple piece of code under VC++ 6.0. I've got a char Buffer array into which I copy the contents of an MFC control. The string is properly nul terminated. I use strcat (Buffer, "\r\n") to append a carriage return\line feed to the string before outputting it to a display where I can monitor what's being exchanged with another service. But after the strcat the string in Buffer is terminated, not with \r\n but with a \n...
3
2948
by: aldonnelley | last post by:
Hi there. I'm just learning c++, and this is driving me nuts. I'm trying to save image files generated in a for loop with a filename built using strcat with: - a char base file name + a character to identify the individual files that is indexed from a char array by the int index of the for loop + a char file extension.
87
5118
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
9
3078
by: Neal Barney | last post by:
I have a C program which runs on a device using a Zilog Z180 microprocessor. While it can address 1MB of RAM, it can only address 64KB at any given time. And of that only 16KB can be used for "stack and heap space". So I'm running in a very memory constricted environment. The program "speaks" a proprietary protocol which sends ASCII strings back and forth from the device to the server. Within the past couple of months we've been...
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
8570
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
9043
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8758
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
8773
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
7584
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
5790
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2936
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.