将字符添加到字符串中 [英] adding chars to a string
问题描述
这可能很简单,当你从未做过指针并习惯于使用Delphi或Visual Basic中的
luxery字符串时,C可以尽管如此。我想要做的就是将字符串添加到字符串中。我看了一下string.h文件,但是
我没有找到那种功能(那个字符串库超过7年b
岁)所以我决定把这个功能写在自己身上。它似乎工作
虽然当我试图在这些动作之间做printf时我很奇怪
消息。无论如何,现在我正在尝试将字符添加到2个字符串。第一个字符串
是可以的,但第二个字符串似乎已经被淹没了。这是我的代码:
/ *将1个字符添加到字符串中的函数* /
char * strAddChar(char * s1,char c){
char * s;
s = s1;
s = s + strlen(s1); //写的位置
* s = c; //这应该在null char
的地方首先
//是。
s ++;
* s = 0; //添加一个新的空字符串
return(s1);
} // strAddChar
/ *这就是我使用函数* /
main(){
char * res1 ="" ;; //空字符串1
char * res2 ="" ;; //空字符串2
strAddChar(res1,'a'');
strAddChar(res1,''b'');
strAddChar(res1,''c'');
strAddChar(res2,''1'');
strAddChar(res2,''''');
strAddChar(res2,''3'');
printf("%s \ n",res1); // test
printf("%s \ n",res2);
好的,输出应为res1 =" abc"和res2 =123但这是我的结果:
res1 =" abc" res2 =" bc123"
" bc"进入res2 ??如何修复这个功能?
问候,
瑞克
Hi,
This is probably simple byt when you never did pointers and being used to
luxery strings like in Delphi or Visual Basic, C can get though. What I''m
trying to do is to add chars to a string. I looked in the string.h file but
I didn''t find that kind of function (that string library is more than 7
years old) so I decided to write that function on myself. It seems to work
although when I''m trying to do printf''s between those actions I get strange
messages. Anyway, now I''m trying to add chars to 2 strings. The first string
is ok but the second one seems to get overwrited. Here''s my code :
/* the function to add 1 char to a string */
char *strAddChar(char *s1, char c){
char *s;
s = s1;
s = s + strlen(s1); // the position to write
*s = c; // this should be on the place where the null char
first
// was.
s++;
*s = 0; // add a new null string
return(s1);
} // strAddChar
/* this is how I use the function */
main(){
char *res1 = ""; // empty string 1
char *res2 = ""; // empty string 2
strAddChar( res1, ''a'' );
strAddChar( res1, ''b'' );
strAddChar( res1, ''c'' );
strAddChar( res2, ''1'' );
strAddChar( res2, ''2'' );
strAddChar( res2, ''3'' );
printf( "%s\n",res1 ); // test
printf( "%s\n",res2 );
Ok, the output should be res1="abc" and res2="123" but this is my result :
res1="abc" res2="bc123"
How does "bc" come in res2?? How to fix the function?
Greetings,
Rick
推荐答案
在文章< 3f *********************** @ news.xs4all.nl>中,Rick写道:
In article <3f***********************@news.xs4all.nl>, Rick wrote:
<当你从未做过指针并习惯于像Delphi或Visual Basic这样的luxery字符串时,这可能很简单,C可以得到。我试图做的是将字符串添加到字符串中。
Hi,
This is probably simple byt when you never did pointers and being used to
luxery strings like in Delphi or Visual Basic, C can get though. What I''m
trying to do is to add chars to a string.
您是否调查了strcat()和strncat()?
-
Andreas K?h?ri
Did you investigate strcat() and strncat()?
--
Andreas K?h?ri
要添加,我认为result2会被result1以某种方式覆盖。当那些2
被初始化时,它们会自动获得一个内存地址,而且当我将结果添加到result1时,似乎
,结果2不会进一步移动。结果1
可能只获得1个字节的内存,因此在添加第二个字符时,它的数量为b
。结果2上的和contineus。
如果这是真的,那将是非常危险的,因为我可以覆盖程序中的大部分内容。通常你可以使用数组和malloc它们,但在我的情况下,我真的不知道结果有多大。在构建
结果字符串时,我需要至少1个子字符串用于其他东西,该字符串有
同样的问题,我无法计算它有多大,它必须动态增长
。怎么处理这个?
问候,
Rick
To add, I think result2 get''s overwritten by result1 somehow. When those 2
got initialized they get a memory adress automatically and it seems that
when I''m adding chars to result1, result2 won''t move further. Result1
probably only get 1 byte of memory so when adding the second char it''s "out
of bounds" and contineus on result2.
If this is true, that would be quiete dangerous because I could overwrite a
big part of the program. Normally you can use arrays and malloc them but in
my case, I really don''t know how big the result becomes. While building the
result string, I need at least 1 sub string for other stuff, that string has
the same problem, I can''t calculate how big it becomes, it has to grow
dynamically. How to handle this?
Greetings,
Rick
你的答案比光快!我只是试了但是同样的问题
(看到其他帖子,我解释了问题一点点)。或者
也许我使用的代码错了,这就是我用strncat做的:
char * res1 ="" ;;
char * res2 ="" ;;
strncat(res1," a",1);
strncat(res1," b", 1);
strncat(res1," c",1);
strncat(res2," 1",1);
strncat(res2," 2",1);
strncat(res2," 3",1);
printf(" result1%s \ n",res1);
printf(" result2%s \ n",res2);
结果:res1 =" abc" RES2 = QUOT; bc123" ;.当使用strcat时一样。
问候,
Rick
You answered faster than the light! I just tried it but the same problem
(see that other post where I explained the problem a little bit more). Or
maybe I am using the code wrong, this is what I did with strncat:
char *res1 = "";
char *res2 = "";
strncat( res1, "a",1 );
strncat( res1, "b",1 );
strncat( res1, "c",1 );
strncat( res2, "1",1 );
strncat( res2, "2",1 );
strncat( res2, "3",1 );
printf( "result1 %s\n",res1 );
printf( "result2 %s\n",res2 );
result : res1 = "abc" res2="bc123". When using strcat the same.
Greetings,
Rick
这篇关于将字符添加到字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!