473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

set char to empty

Hi,

How do I set the char [ ] to empty ?

let say I have,
char test[20];

can I do like this :
*test = NULL;

Thanks.
Nov 14 '05 #1
17 67454
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.

Nov 14 '05 #2
On Mon, 2 Aug 2004 12:50:39 +0800, "Magix" <ma***@asia.com> wrote:
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>>
Nov 14 '05 #3
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"

Nov 14 '05 #4

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:2n************@uni-berlin.de...
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, and strcat another info to test buffer in
another process.
Nov 14 '05 #5
Martin Ambuhl <ma*****@earthlink.net> wrote:
Magix wrote:
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
Nov 14 '05 #6
Emmanuel Delahaye <em***@yourbranoos.fr> scribbled the following:
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)

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"We sorcerers don't like to eat our words, so to say."
- Sparrowhawk
Nov 14 '05 #7
Joona I Palaste wrote on 02/08/04 :
Emmanuel Delahaye <em***@yourbranoos.fr> scribbled the following:
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 '*'...

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

"C is a sharp tool"

Nov 14 '05 #8

"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> wrote in message
news:mn***********************@YOURBRAnoos.fr...
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
Nov 14 '05 #9
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.

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

"C is a sharp tool"

Nov 14 '05 #10
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"

Nov 14 '05 #11
Magix wrote:
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:2n************@uni-berlin.de...
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.
Nov 14 '05 #12

"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> wrote in message
news:mn***********************@YOURBRAnoos.fr...
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 ?
Nov 14 '05 #13
Magix wrote on 02/08/04 :
I initialise:
char test[20]="";
Good practice. Ok.
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

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'.

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

"C is a sharp tool"

Nov 14 '05 #14
In article <mn***********************@YOURBRAnoos.fr>,
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote:
// clear test buffer for another process
*test=0;

strcat(test, "GHI")
... some routine
strcat(test, "XYZ")
... output test
The second print out, I got GHIXYZABCDEF, which I expect to get GHIXYZ
Why ?


Because something wrong probably occured in 'some routine'.


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
Nov 14 '05 #15
Richard Bos <rl*@hoekstra-uitgeverij.nl> spoke thus:
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? :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #16
On Mon, 2 Aug 2004, Magix wrote:
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[i] = '\0';

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #17
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Richard Bos <rl*@hoekstra-uitgeverij.nl> spoke thus:
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
Nov 14 '05 #18

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

Similar topics

22
by: mail.zhf | last post by:
if have two char use equality, out first. example: char str = "gbdfssdffss"; out is: f.
5
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...
1
by: arssa2020 | last post by:
hello, i am writing a GPS app in evc++ 4.0, i am using c-style char* instead of CStrings, i want to parse NMEA sentence such as "GPZDA,162913.48,19,08,2005,,*63" problem is when i use strtok it...
3
by: Bob Stearns | last post by:
Vol. 1 of SQL REFERENCE contains the following statement in the description of CHAR: Note: The CAST expression can also be used to return a string expression. I was trying it in order to get...
26
by: the.tarquin | last post by:
Okay, this one has me totally baffled. I have a function, getParsedKey(char* key, char* returnString). I pass in the key I want, it retrieves it from a data structure and puts the value in...
0
by: usunto_bryjamus | last post by:
Hi, I have software which have to read data using dll writen in C. This library has method which has header like this: int fnGetDescription(int allwaysOpen, char *itemNumber, char *desc, char...
3
by: begum | last post by:
hi everybody; I wirte a program but i have some problem. When I enter character or negative number, the program is crashed.I want it to give me a output like that " you will enter character or...
5
by: Travis | last post by:
I am using a function that returns a const char * that is usually a word, etc. How can I check to see if what it returns is empty? I tried if (function() == "") and (function() == NULL) and...
3
by: Michelle Monty | last post by:
Hello, I'm creating a program that will allow the user to enter payment information on the form and it will save that information to a sequential file. I'm getting an error that says 'padleft is not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.