重置char数组 [英] reset char array

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

问题描述




这对你来说可能是小菜一碟C大师,但它让我感到头疼。我还没有真正使用指针和字符这么多东西

出错了。练习可以创造奇迹,但问题是我没有b $ b有很多时间练习。好的,这是问题,如何重置一个char

数组?

我得到了这个代码:


char test [8] ="" ;;


printf(subStr(" result =%s \ n"," blablabla",3,5,test));

printf(subStr(" result =%s \ n"," another string" 3,5,test));


subStr是一个自制函数,从输入

参数中得到几个字符并将其放入输出参数(在本例中为测试)。我使用它的第一个

时间确实可行,但当然,第二次测试char

数组将进一步增长而不是替换。因此,结果

应该是:

" bla"
"

我得到

" bla"

" thebla"

这是因为subStr使用strcat而第二次使用它测试

数组仍然充满了一些旧东西。如何摆脱它?我尝试了

免费(测试);

介于这两者之间但是没有用。


问候,

Rick

Hi,

This is probably piece of cake for you C gurus but it is giving me a
headache. I''m still not really used with pointers and chars so many things
are going wrong. Practicing makes wonders but the problem is that I don''t
have much time to practice. Ok, here''s the question, how to reset a char
array?
I got this code :

char test[8] = "";

printf( subStr( "result = %s \n" , "blablabla" , 3,5, test ) );
printf( subStr( "result = %s \n" , "another string" , 3,5, test ) );

subStr is a selfmade function which get''s a few chars out of the input
parameter and puts it in the output parameter (test in this case). The first
time I use it it works ok, but of course, the second time the test char
array will grow further instead of being replaces. So while the result
should be :
"bla"
"the"
I get
"bla"
"thebla"
It''s because of subStr uses strcat and the second time I use it the test
array is still filled with some old stuff. How to get rid of that? I tried
free( test );
between those 2 but that didn''t work.

Greetings,
Rick

推荐答案

好的,这是一种方法这样做

test [2] = 0;

test [3] = 0;

//或者循环一次


但是我感觉到了我是以愚蠢的方式做的...


问候,

Rick
Ok, this is one way of doing it
test[2] = 0;
test[3] = 0;
// or do it in a loop

But I got the feeling I''m doing it on a stupid way...

Greetings,
Rick


在文章< 3f *********************** @ news.xs4all.nl> ;,里克写道:

[cut ]
In article <3f***********************@news.xs4all.nl>, Rick wrote:
[cut]
subStr是一个自制函数,它从输入
参数中获取几个字符并将其放入输出参数(在本例中为测试)。我使用它的第一个时间它可以正常工作,但当然,第二次测试char
数组将进一步增长而不是替换。因此,结果
应该是:
bla
the
我得到
bla
thebla<这是因为subStr使用strcat而第二次使用它测试


为什么不strcpy()?这感觉更明智。

数组仍然充满了一些旧东西。如何摆脱它?我试过
免费(测试);
subStr is a selfmade function which get''s a few chars out of the input
parameter and puts it in the output parameter (test in this case). The first
time I use it it works ok, but of course, the second time the test char
array will grow further instead of being replaces. So while the result
should be :
"bla"
"the"
I get
"bla"
"thebla"
It''s because of subStr uses strcat and the second time I use it the test
Why not strcpy()? That feels more sensible.
array is still filled with some old stuff. How to get rid of that? I tried
free( test );




你可能只有free()你有malloc()编辑的东西。

你也可以尝试在两次调用之间测试[0] =''\''',这是不能解决有问题的subStr()

实施,只需解决它。


-

Andreas K?h?ri



You may only free() stuff that you have malloc()''ed.
You could also try test[0]=''\0'' inbetween the two invocations,
but that wouldn''t really solve the problem of a faulty subStr()
implementation, just work around it.

--
Andreas K?h?ri


你是对的,我有更好的功能。我修复它现在它

就像strncpy函数一样,更好。但字符串drame ain''t
结束了,我还有另外一个问题。首先,我注意到我看到的所有字符串

函数(我看了一下string.h)需要一个指针参数,所以他们可以直接写入
。我尝试在没有它的情况下制作功能但是他们再次使用它们不再工作了,例如:


char * test(){

char * s =" blabla";

返回s;

} //结果是,嗯,不可读


但是如果我这样做

char * test(char * s){

返回s;

} //结果还可以


它可能与所有分配的东西有关,不是吗?或者不能没有这个参数
字符串函数吗?

另一个问题,它是关于strcmp的。现在subStr函数终于工作了,我希望将结果比作这样比较

char s [8];

subStr(" abcdef,1,2,8); //结果是BC

if(strcmp(s," BC")== 1)printf(多么令人惊讶。);


之前我使用strcmp工作但它工作但现在我突然无法得到

正确的结果。即使我这样做

if(strcmp(" A"," A")== 1)printf(只是必须是正确的。);

它不会工作。我输入的内容有什么问题?


问候,

Rick
You''re right, I''m better of with a good function. I fixed it and now it
works like the strncpy function, much better. But the string drame ain''t
over, I got another questions. First of all, I noticed that all the string
functions I saw (I looked at string.h) need a pointer parameter so they can
write directly to it. I tried to make functions without that but then they
won''t work anymore, an example :

char *test(){
char *s = "blabla";
return s;
} // result is, well, not readable

But if I do
char *test( char *s ){
return s;
} // result is ok

It probably has to do with all the allocate stuff doesn''t it? Or can''t
string functions work without this parameter?
Another question, it''s about strcmp. Now that subStr function is finally
working I want the result to be compared like this
char s[8];
subStr( "abcdef", 1,2,8 ); // result is "BC"
if ( strcmp( s, "BC" ) == 1) printf( "How amazing." );

I worked before with strcmp and it worked but now suddenly I can''t get the
right result. Even if I do this
if ( strcmp( "A", "A" ) == 1) printf( "That just has to be right." );
It won''t work. What''s wrong with my input?

Greetings,
Rick


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

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