strcat问题了 [英] strcat problem again

查看:88
本文介绍了strcat问题了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



继续我的strcat分段错误发布 -

我在使用

strcat附加两个sting文字时出现问题。

我试图通过编写我自己的函数来修复它,它可以执行strcat

(mystract)。节目如下。

然而这似乎没有解决问题,我不知道为什么它不应该?b $ b不应该?

任何进一步的帮助,我将做错的其他事情将被赞赏

问候

伊恩。

该计划的想法是:

#enter一个整数转换为单词

#99 - 输入

#ninety nine --output

Ps尝试返回sprintf并使用宏而没有运气。


#include< stdio.h>

#include< stdlib.h>

#include< string.h>

char * convertLessThanOneThousand(int number);

char * convert(int number);

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

static int num;

/ * static char dest [1024]; * /

static char * numNames [] = {

"",

"一个,

两个,

三个,

四个,

五,

六个,

七,

8,

九,

十,

"十一,

十二,

十三,

十四,

十五,

十六,

十七,

十八,

十九

};

静态字符* tensNames [] = {

"",

" ;十,

"二十,

三十,

四十,

五十,

六十,

七十,

八十,

九十

};

char * majorNames [] = {

"",

"千,

百万,

亿美元,

"万亿美元,

" quadrillion,

quintillion"

};

int main(){

printf("输入一个转换为单词的整数);

scanf("%d",& num);

printf("转换为字词:%s \ n",convert(num));

返回0;

}


char * convertLessThanOneThousand(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(tensNames [number%10],soFar);

number / = 10;

}

if(number == 0)

返回soFar;

/ * return numNames [number] +"百" + soFar; * /

/ * sprintf(结果,'%s%s%s",numNames [数字],百,soFar); * /

返回mystrcat(numNames [number],mystrcat(百,soFar));

}


char * convert(int number){

char * zero =" zero";

if(number == 0){

返回零; }

char * prefix ="" ;;

char * soFar ="" ;;

int place = 0;

do {

int n = number%1000;

if(n!= 0){

char * s = convertLessThanOneThousand(n);

/ * soFar = s + majorNames [place] + soFar; * /

soFar = mystrcat(s,mystrcat(majorNames [place], soFar));

}

place ++;

number / = 1000;

} while(number> 0);

/ * return(前缀+ soFar).trim(); * /

返回(mystrcat(前缀,soFar));

}


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

while(* dest){}

dest--;

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

返回dest;

}

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* convertLessThanOneThousand(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("converted to words: %s\n", convert(num));
return 0;
}

char* convertLessThanOneThousand(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(tensNames[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(numNames[number], mystrcat(hundred, 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 = convertLessThanOneThousand(n);
/*soFar = s + majorNames[place] + soFar;*/
soFar = mystrcat(s, mystrcat(majorNames[place], soFar));
}
place++;
number /= 1000;
} while (number > 0);
/*return (prefix + soFar).trim();*/
return (mystrcat(prefix, soFar));
}

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

推荐答案

" Ian Stanley" < IA ******** @ hotmail.com>写在

新闻:3f ******** @ news.chariot.net.au:
"Ian Stanley" <ia********@hotmail.com> wrote in
news:3f********@news.chariot.net.au:

继续我的strcat分段错误发布 -
我有一个问题,当使用
strcat附加两个sting文字时发生。
我试图通过编写我自己的函数来修复strcat
(mystract)。下面的程序。
然而,这似乎没有解决问题,我不知道为什么它不应该?
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 ->
--


srtcat要求已经提供它应该写入的内存

...听起来好像你正在写缓冲区和系统段故障

...

i没有读完全源..所以也许我只是在写废话..

Clemens


On Thu,2003年9月18日00:05:28 +0930

Ian Stanley < IA ******** @ hotmail.com>写道:
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********@hotmail.com> wrote:

继续我的strcat分段错误发布 -
我在使用
strcat追加两个sting文字时会出现问题。我已经尝试通过编写我自己的函数来修复它了strcat
(mystract)。以下计划。
然而,这似乎没有解决问题,我不知道为什么它不应该?
任何进一步的帮助,我还有什么其他错误将感谢
问候
Ian。
该计划的想法是:
#enter一个整数转换为单词
#99 - 输入
#ninety nine --output
Ps尝试返回sprintf并使用宏而没有运气。

#include< stdio.h>
#include< stdlib.h>
#include< string.h>
char * convertLessThanOneThousand(int number);
char * convert(int number);
char * mystrcat(char * dest,char * source );
static int num;
/ * static char dest [1024]; * /
static char * numNames [] = {
"",
"一个,
两个,
三个,
四,
五,
六,
七,
八,
九,
十,
十一,
十二,
十三,
十四,
十五,
十六,
十七,
十八,
十九
};
静态字符* tensNames [] = {
"",
"十,
二十,
三十,
四十,
五十,
六十,
七十,
八十,
九十
};
char * majorNames [] = {
"",
"千,
百万,
亿,
万亿,
千万亿,
quintillion"
};
int main(){
printf(输入一个转换为单词的整数);
scanf("%d",&) num);
printf("转换为单词:%s \ n",convert(num));
返回0;
}

char * convertLessThanOneThousand(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(tensNames [number%10],soFar);
number / = 10;
}
if( number == 0)
返回soFar;
/ * re转numNames [number] +"百" + soFar; * /
/ * sprintf(结果,'%s%s%s",numNames [number],百,soFar); * /
返回mystrcat(numNames [number],mystrcat (百,soFar));
}
char * convert(int number){
char * zero =" zero" ;;
if(number == 0){
返回零; }
char * prefix ="" ;;
char * soFar ="" ;;
int place = 0;
do {
int n = number %1000;
if(n!= 0){
char * s = convertLessThanOneThousand(n);
/ * soFar = s + majorNames [place] + soFar; * /
soFar = mystrcat(s,mystrcat(majorNames [place],soFar));
}
放置++;
number / = 1000;
} while(number> 0) ;
/ * return(前缀+ soFar).trim(); * /
返回(mystrcat(前缀,soFar));
}

char * mystrcat (char * dest,char * source){
while(* dest){}
dest--;
while(* dest ++ = * source ++){}
返回dest;
}
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* convertLessThanOneThousand(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("converted to words: %s\n", convert(num));
return 0;
}

char* convertLessThanOneThousand(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(tensNames[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(numNames[number], mystrcat(hundred, 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 = convertLessThanOneThousand(n);
/*soFar = s + majorNames[place] + soFar;*/
soFar = mystrcat(s, mystrcat(majorNames[place], soFar));
}
place++;
number /= 1000;
} while (number > 0);
/*return (prefix + soFar).trim();*/
return (mystrcat(prefix, soFar));
}

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



Ian Stanley写道:
Ian Stanley wrote:

继续我的strcat分段错误发布 -
我有一个附加时出现的问题两个sting文字使用
strcat。


你根本不能连接字符串文字*。要连接它们,

你必须更新第一个,但你不能更新

一个字符串文字。今天BOOM。


目击者:

静态字符* numNames [] = {
"",
"一个,
两个,
三个,
四,
五,
六,
七,
八,
九,
十,
十一,
十二,
十三,
十四,
十五,
十六,
十七,
十八,
19"
返回mystrcat(numNames [number],mystrcat(百,soFar));
char * mystrcat(char * dest,char * source){
while(* dest){}
dest--;
while(* dest ++ = * source ++){}
返回dest;
}
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(numNames[number], mystrcat(hundred, soFar)); char* mystrcat(char* dest, char* source){
while(*dest){}
dest--;
while(*dest++ = *source++){}
return dest;
}




如果mystrcat一直工作[它不会,因为在
第一个while循环],你将更新numNames中的一个字符串文字

。没有办法。无论如何,你需要将你的文字复制到一个

BIG ENOUGH缓冲区。


见证:


char * convert(int number){

char * zero =" zero" ;;

if(number == 0){

返回零; }

char * prefix ="" ;;


你要么使用C99或C ++,要么不是通常的C,这不是允许

声明后的声明。


-

Chris" electric hedgehog" Dollin

C常见问题解答: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html

C欢迎: http://www.angelfire.com/ms3/bchambl...me_to_clc.html


这篇关于strcat问题了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆