本文介绍了strcat问题了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 继续我的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 usingstrcat.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 itshouldn''t ?Any further help as to what else I am doing wrong will be appreciatedregardsIan.The idea of the program is to:#enter a integer to convert to words#99 -- input#ninety nine --outputPs 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;}推荐答案 是否* *任何*原因你不能启动调试器并单步执行 此代码?这就是我无法看到自己的错误。 - - 马克 - > -Is there *any* reason you can''t fire up a debugger and single step throughthis code? This is what I do when I simply cannot see my mistake.--- Mark ->-- 如果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问题了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!