reset char array | | |
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 | | | | re: reset char array
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 | | | | re: reset char array
In article <3f992c1d$0$58703$e4fe514c@news.xs4all.nl>, Rick wrote:
[cut][color=blue]
> 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[/color]
Why not strcpy()? That feels more sensible.
[color=blue]
> array is still filled with some old stuff. How to get rid of that? I tried
> free( test );[/color]
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 | | | | re: reset char array
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 | | | | re: reset char array
In article <3f99432b$0$58715$e4fe514c@news.xs4all.nl>, Rick wrote:
[cut][color=blue]
> 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[/color]
See the FAQ here: http://www.eskimo.com/~scs/C-faq/q7.5.html
[cut][color=blue]
> It probably has to do with all the allocate stuff doesn't it? Or can't
> string functions work without this parameter?[/color]
I'm not quite sure what you mean with "work"...
[color=blue]
> 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." );[/color]
[cut][color=blue]
> if ( strcmp( "A", "A" ) == 1) printf( "That just has to be right." );[/color]
The strcmp() library function returns the difference between two
strings. A return value of zero means that the strings were
equal.
--
Andreas Kähäri | | | | re: reset char array
On 10/24/2003 10:21 AM, Rick wrote:[color=blue]
> 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[/color]
The above should be fine. Are you sure you didn't instead do:
char s[7] = "blabla";
as that would be wrong.
<snip>[color=blue]
> if ( strcmp( "A", "A" ) == 1) printf( "That just has to be right." );[/color]
Make that "== 0". See http://www.acm.uiuc.edu/webmonkeys/b...14.html#strcmp
Ed. | | | | re: reset char array
Rick wrote:
[color=blue]
> 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...[/color]
As others have pointed out, only test[0] needs to be set - however, if you
really want all bytes 0, use:
memset(test, 0, sizeof test); | | | | re: reset char array
Rick wrote:[color=blue]
>
> 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.[/color]
Not true! In fact, most of the string functions do NOT need a "pointer",
since they rarely modify the inputs. For example,
int strcmp(const char *s1, const char *s2);
Note that the args are const - thye do not require pointers; string
literals are just fine here (Note your example below trying to compare
"A" to "A").
[color=blue]
>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
>[/color]
Make test a static variable so it stays in scope:
char *test(){
static char *s = "blabla";
return s;
}
[color=blue]
> 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[/color]
strcmp returns zero if the two input strings are equal.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225 | | | | re: reset char array
"Rick" <aso3rick@hotmail.com> writes:
[color=blue]
> 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.[/color]
free() is only meant to be used with pointers to blocks of memory
that were allocated with malloc() or calloc(). You can't use them
on statically or automatically allocated memory.
If your subStr() is intended to replace the current contents of
the string, it should set the first char of test to '\0' before
using strcat; or use strcpy/strncpy instead.
--
Micah J. Cowan micah@cowan.name | | | | re: reset char array
"Fred L. Kleinschmidt" <fred.l.kleinschmidt@nospam_boeing.com> writes:
[color=blue]
> Rick wrote:[color=green]
> >
> > 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.[/color]
>
> Not true! In fact, most of the string functions do NOT need a "pointer",
> since they rarely modify the inputs. For example,
> int strcmp(const char *s1, const char *s2);[/color]
See the *s? s1 and s2 above are called "pointers". I think I
understand what you meant, but please get the terminology
straight, so readers who may not know better don't propagate
the errors.
--
Micah Cowan micah@cowan.name | | | | re: reset char array
On Fri, 24 Oct 2003 19:25:00 GMT, "Fred L. Kleinschmidt"
<fred.l.kleinschmidt@nospam_boeing.com> wrote:
[color=blue][color=green]
>> char *test(){
>> char *s = "blabla";
>> return s;
>> } // result is, well, not readable
>>[/color]
>
>Make test a static variable so it stays in scope:
> char *test(){
> static char *s = "blabla";
> return s;
> }
>[/color]
What's wrong with the first version? Mind you, "static" means that s is static,
the string of chars being pointed at is already static. So the first version if
perfectly fine, or just
return "blabla";
they're all the same. | | | | re: reset char array
On Fri, 24 Oct 2003 11:26:49 +0000, Ed Morton wrote:
[color=blue]
>
>
> On 10/24/2003 10:21 AM, Rick wrote:[color=green]
>> 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[/color]
>
> The above should be fine. Are you sure you didn't instead do:[/color]
Hmm. I'd have thought that "blabla" wouldn't be guaranteed to exist
outside of test. I gather from your reply, though, that it is? Is that
because "blabla" is a constant, and not an initializer (as it is below)?
[color=blue]
>
> char s[7] = "blabla";
>
> as that would be wrong.
>
> <snip>[color=green]
>> if ( strcmp( "A", "A" ) == 1) printf( "That just has to be right." );[/color]
>
> Make that "== 0". See
> http://www.acm.uiuc.edu/webmonkeys/b...14.html#strcmp
>
> Ed.[/color]
Mac
-- | | | | re: reset char array
On Sat, 25 Oct 2003 10:10:47 -0700, "Mac" <foo@bar.net> wrote:
[color=blue]
>On Fri, 24 Oct 2003 11:26:49 +0000, Ed Morton wrote:
>[color=green]
>>
>>
>> On 10/24/2003 10:21 AM, Rick wrote:[color=darkred]
>>> 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[/color]
>>
>> The above should be fine. Are you sure you didn't instead do:[/color]
>
>Hmm. I'd have thought that "blabla" wouldn't be guaranteed to exist
>outside of test. I gather from your reply, though, that it is? Is that
>because "blabla" is a constant, and not an initializer (as it is below)?[/color]
Yes. In this context, the quoted material is a string literal which
is defined as a static array of char. The static qualifier insures
that it exists for the entire life of the program, not just the life
of the function in which it appears.
[color=blue]
>[color=green]
>>
>> char s[7] = "blabla";
>>
>> as that would be wrong.
>>
>> <snip>[color=darkred]
>>> if ( strcmp( "A", "A" ) == 1) printf( "That just has to be right." );[/color]
>>
>> Make that "== 0". See
>> http://www.acm.uiuc.edu/webmonkeys/b...14.html#strcmp
>>
>> Ed.[/color]
>
>Mac[/color]
<<Remove the del for email>> | | | | re: reset char array
Mac wrote:
[color=blue]
> On Fri, 24 Oct 2003 11:26:49 +0000, Ed Morton wrote:[/color]
<snip>[color=blue][color=green][color=darkred]
>>> char *test(){
>>> char *s = "blabla";
>>> return s;
>>> } // result is, well, not readable[/color]
>>
>>The above should be fine. Are you sure you didn't instead do:[/color]
>
>
> Hmm. I'd have thought that "blabla" wouldn't be guaranteed to exist
> outside of test. I gather from your reply, though, that it is? Is that
> because "blabla" is a constant, and not an initializer (as it is below)?[/color]
Pretty much. In the first version, s points to the area of memory where
"blabla" lives so when the function returns the value of "s" that value
still points to "blabla", while in the second the function stack
starting at location "s" gets a copy of "blabla" so when the function
returns the value of "s", it no longer points to a valid location since
the functions stack was already released. "blabla" cheerfully lives on
in memory in either case, but in the second you no longer have a pointer
to it, but instead a pointer to where a copy of it used to live. See http://www.eskimo.com/~scs/C-faq/q6.2.html for a better general description.
Ed.
[color=blue]
>[color=green]
>> char s[7] = "blabla";[/color][/color]
<snip> | | | | re: reset char array
Million of answers, can't go wrong anymore! Thank you all!
Greetings,
Rick |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|