set char to empty

M

Magix

Hi,

How do I set the char [ ] to empty ?

let say I have,
char test[20];

can I do like this :
*test = NULL;

Thanks.
 
M

Martin Ambuhl

Magix said:
Hi,

How do I set the char [ ] to empty ?

let say I have,
char test[20];

char test[20] = "";
if you want to initialize it to an empty string.
can I do like this :
*test = NULL;

*test (or test[0]) is a char.
NULL is a pointer.
Thou shalt not assign pointer values to chars.

*test = 0;
test[0] = 0;

or any of several synonymous statements will do.
 
B

Barry Schwarz

Hi,

How do I set the char [ ] to empty ?

let say I have,
char test[20];

You cannot set an array to empty. test is an array of 20 char. Each
of those 20 has a value (even if you don't know what the value is).

You can set the array to an empty (strlen = 0) string by setting
test[0] to '\0'. There are several ways to do this:

As part of the definition
char test[20] = "";
char test[20] = {'\0'};

Via an assignment
test[0] = '\0';

Via a function
strcpy(test,"");
memset(test,'\0',sizeof test);
can I do like this :
*test = NULL;

No. You cannot place an array on the left of the assignment operator.


<<Remove the del for email>>
 
E

Emmanuel Delahaye

Magix wrote on 02/08/04 :
How do I set the char [ ] to empty ?

let say I have,
char test[20];

can I do like this :
*test = NULL;

Where did you get that? Certainely not from a good C-book. 'test' being
an array, it's a constant pointer. It can't be modified.

What do you mean by 'empty'? An object has always a value. It is never
'empty'.

That said, a peculiar value could have the semantics of 'empty'. Its a
matter of convention. If you are talking about a string, it's
considered 'empty' or "" when you have a 0 at index 0.

char test[20];

test[0] = 0;

or
*test = 0;

or

strcpy (test, "");

because "" is hard coded

char anonymous[] = {0};
 
M

Magix

Martin Ambuhl said:
Magix said:
Hi,

How do I set the char [ ] to empty ?

let say I have,
char test[20];

char test[20] = "";
if you want to initialize it to an empty string.
can I do like this :
*test = NULL;

*test (or test[0]) is a char.
NULL is a pointer.
Thou shalt not assign pointer values to chars.

*test = 0;
test[0] = 0;

or any of several synonymous statements will do.

I used test as string buffer. After strcat to test buffer in this process, i
will need to clear the buffer, and strcat another info to test buffer in
another process.
 
R

Richard Bos

Martin Ambuhl said:
Magix said:
can I do like this :
*test = NULL;

*test (or test[0]) is a char.
NULL is a pointer.
Thou shalt not assign pointer values to chars.

*test = 0;
test[0] = 0;

or any of several synonymous statements will do.

In particular, if you want to make it obvious that you're assigning a
char, not an int,

*test='\0';
or
test[0]='\0';

is completely equivalent, but looks more char-ish.

Richard
 
J

Joona I Palaste

Emmanuel Delahaye said:
Magix wrote on 02/08/04 :
How do I set the char [ ] to empty ?

let say I have,
char test[20];

can I do like this :
*test = NULL;
Where did you get that? Certainely not from a good C-book. 'test' being
an array, it's a constant pointer. It can't be modified.

He's modifying *test, not test. And arrays are not pointers.
What do you mean by 'empty'? An object has always a value. It is never
'empty'.
That said, a peculiar value could have the semantics of 'empty'. Its a
matter of convention. If you are talking about a string, it's
considered 'empty' or "" when you have a 0 at index 0.

This is true.

(Sample code snipped)
 
M

Magix

Emmanuel Delahaye said:
Magix wrote on 02/08/04 :
How do I set the char [ ] to empty ?

let say I have,
char test[20];

can I do like this :
*test = NULL;

Where did you get that? Certainely not from a good C-book. 'test' being
an array, it's a constant pointer. It can't be modified.

What do you mean by 'empty'? An object has always a value. It is never
'empty'.
That said, a peculiar value could have the semantics of 'empty'. Its a
matter of convention. If you are talking about a string, it's
considered 'empty' or "" when you have a 0 at index 0.

char test[20];

test[0] = 0;

or
*test = 0;

or

strcpy (test, "");

because "" is hard coded

char anonymous[] = {0};

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"


Basical, I used it as string buffer. I want to clear the buffer for use of
another process.

let say:
char test[20];

strcat(test,"ABC");
...some routine
strcat(test, "DEF");
.... some routine
.... output test
// clear test buffer for another process
*test=0;
strcat(test, "GHI")
... some routine
strcat(test, "XYZ")
... output test
 
E

Emmanuel Delahaye

Joona I Palaste wrote on 02/08/04 :
Emmanuel Delahaye said:
Magix wrote on 02/08/04 :
How do I set the char [ ] to empty ?

let say I have,
char test[20];

can I do like this :
*test = NULL;
Where did you get that? Certainely not from a good C-book. 'test' being
an array, it's a constant pointer. It can't be modified.

He's modifying *test, not test. And arrays are not pointers.

Dammned. I missed the '*'...
 
E

Emmanuel Delahaye

Emmanuel Delahaye wrote on 02/08/04 :
Magix wrote on 02/08/04 :
How do I set the char [ ] to empty ?

let say I have,
char test[20];

can I do like this :
*test = NULL;

Where did you get that? Certainely not from a good C-book. 'test' being an
array, it's a constant pointer. It can't be modified.

FOrget that. I missed the '*'. That said, you should use 0 or '\0'
instead of NULL that has only a meaning in a pointer context.
 
E

Emmanuel Delahaye

Magix wrote on 02/08/04 :
Basical, I used it as string buffer. I want to clear the buffer for use of
another process.

let say:
char test[20];

strcat(test,"ABC");

Stop! You can't use strcat() on an uninitialized string.

add

*test = 0;

before the first strcat() call, or use strcpy() the first time...
...some routine
strcat(test, "DEF");
... some routine
... output test
// clear test buffer for another process
*test=0;
Ok.

strcat(test, "GHI")
... some routine
strcat(test, "XYZ")
... output test

Ok. Just be sure that you don't overflow the string. strncat() can
help.
 
M

Martin Ambuhl

Magix said:
Magix wrote:

Hi,

How do I set the char [ ] to empty ?

let say I have,
char test[20];

char test[20] = "";
if you want to initialize it to an empty string.

can I do like this :
*test = NULL;

*test (or test[0]) is a char.
NULL is a pointer.
Thou shalt not assign pointer values to chars.

*test = 0;
test[0] = 0;

or any of several synonymous statements will do.


I used test as string buffer. After strcat to test buffer in this process, i
will need to clear the buffer,

No, you don't. You just need to mark it with an immediate end-of-string
'\0'.
and strcat another info to test buffer in
another process.

For which my above suggestions will work just fine.
 
M

Magix

Emmanuel Delahaye said:
Magix wrote on 02/08/04 :
Basical, I used it as string buffer. I want to clear the buffer for use of
another process.

let say:
char test[20];

strcat(test,"ABC");

Stop! You can't use strcat() on an uninitialized string.

add

*test = 0;

before the first strcat() call, or use strcpy() the first time...
...some routine
strcat(test, "DEF");
... some routine
... output test
// clear test buffer for another process
*test=0;
Ok.

strcat(test, "GHI")
... some routine
strcat(test, "XYZ")
... output test

Ok. Just be sure that you don't overflow the string. strncat() can
help.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

I initialise:
char test[20]="";

The first print out, I got ABCDEF
The second print out, I got GHIXYZABCDEF, which I expect to get GHIXYZ
Why ?
 
E

Emmanuel Delahaye

Magix wrote on 02/08/04 :
I initialise:
char test[20]="";

Good practice. Ok.
The first print out, I got ABCDEF
Ok.

The second print out, I got GHIXYZABCDEF, which I expect to get GHIXYZ
Why ?

Because something wrong probably occured in 'some routine'.
 
R

Richard Tobin

The second print out, I got GHIXYZABCDEF, which I expect to get GHIXYZ
Why ?

Because something wrong probably occured in 'some routine'.[/QUOTE]

Although in theory undefined behaviour could do anything, it's hard to
see how in practice a string could end "DEF" immediately after
strcat(test, "XYZ").

What exactly is the output test? Just a printf?

-- Richard
 
D

Darrell Grainger

Hi,

How do I set the char [ ] to empty ?

You need to define what 'empty' means to you.
let say I have,
char test[20];

can I do like this :
*test = NULL;

No. *test is actually the same as using test[0]. The NULL macro is not a
char. You could use:

test[0] = '\0';
or
*test = '\0';

Essentially, test is an array of twenty char. If you set the first char to
'\0' (null character), all the string functions will see this array as an
empty string. If you want to remove EVERYTHING from the string then you
need to fill all twenty elements with the null character. Something like:

int i;

for(i = 0; i < sizeof(test); i++)
test = '\0';
 
C

Christopher Benson-Manica

Richard Bos said:
In particular, if you want to make it obvious that you're assigning a
char, not an int,
*test='\0';
or
test[0]='\0';

is completely equivalent, but looks more char-ish.

It's a question of religion, I suppose? :)
 
R

Richard Bos

Christopher Benson-Manica said:
Richard Bos said:
In particular, if you want to make it obvious that you're assigning a
char, not an int,
*test='\0';
or
test[0]='\0';

is completely equivalent, but looks more char-ish.

It's a question of religion, I suppose? :)

More a question of taste. I can't imagine anyone throwing quite such a
tantrum about it as some seem to do about brace style.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top