473,805 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 13 '05 #1
14 14405
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
Nov 13 '05 #2
In article <3f************ ***********@new s.xs4all.nl>, Rick wrote:
[cut]
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 );


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
Nov 13 '05 #3
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
Nov 13 '05 #4
In article <3f************ ***********@new s.xs4all.nl>, Rick wrote:
[cut]
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

See the FAQ here:

http://www.eskimo.com/~scs/C-faq/q7.5.html
[cut] It probably has to do with all the allocate stuff doesn't it? Or can't
string functions work without this parameter?

I'm not quite sure what you mean with "work"...

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." ); [cut] if ( strcmp( "A", "A" ) == 1) printf( "That just has to be right." );

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
Nov 13 '05 #5


On 10/24/2003 10:21 AM, Rick wrote:
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
The above should be fine. Are you sure you didn't instead do:

char s[7] = "blabla";

as that would be wrong.

<snip> if ( strcmp( "A", "A" ) == 1) printf( "That just has to be right." );


Make that "== 0". See
http://www.acm.uiuc.edu/webmonkeys/b...14.html#strcmp

Ed.

Nov 13 '05 #6
bd
Rick wrote:
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...


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);
Nov 13 '05 #7


Rick wrote:

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.
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").
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

Make test a static variable so it stays in scope:
char *test(){
static char *s = "blabla";
return s;
}
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


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
Nov 13 '05 #8
"Rick" <as******@hotma il.com> writes:
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.


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
mi***@cowan.nam e
Nov 13 '05 #9
"Fred L. Kleinschmidt" <fred.l.kleinsc hmidt@nospam_bo eing.com> writes:
Rick wrote:

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.


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);


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
mi***@cowan.nam e
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
30139
by: Ken | last post by:
How can I reset the initial form variables that are set with session statements when clicking on a button? I tried this but the function was not called: <?PHP function reset_form($none) { $_SESSION = array(); } ?> <form enctype="multipart/form-data" name="company_info" method="post" action="add_pic.php">
6
2188
by: Shabam | last post by:
I have a text field that's pre-filled with data. Suppose the user edits it, but decides he wants to reset the data back to the original pre-filled data, how can I do that?
22
17210
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
8
2180
by: spike | last post by:
My prygram goes through a string to find names between '\'-characters The problem is, parts of the name in sTemp remains if the new name is shorter than the old name. code --------------------------------------------------- char sTemp; char sText = "THELONGESTNAME\\samantha\\gregor\\spike\\..."; // and so on...
5
3991
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
12
5537
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the following program: #include<stdio.h> #define SIZE 1 int main()
3
6245
by: Crirus | last post by:
I have a big array 512*512 elements Wich is the best way to reset the array to 0 on each element? Redim without preserve, I have for loop and clear shared function Wich one is best optimised for speed? -- Cheers,
5
7225
by: bdbeames | last post by:
I have and array that I fill with elements in a function. I call it later and want to reset it so that it is empty so I can fill it with different elements and a different number of elements. How do I reset it so that there is nothing in it? I would do it before the If statements, but I'm not sure how to reset it. Could someone help? var imageArray; var image_dir; var imageNum =0;
4
1452
by: JuAn2226 | last post by:
can anyone help me to to do coding for reset and start again. i got 10 array of random number, 10 array for start time how do i reset the both array after the 10 array is filled and start filling up again for new random number again.
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10613
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.