473,842 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
17 67775
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,"AB C");
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*****@earthl ink.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***@YOURBRAn oos.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,"AB C");


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,"AB C"); ...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************ ***********@YOU RBRAnoos.fr>,
Emmanuel Delahaye <em***@YOURBRAn oos.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)cybers pace.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.c yberspace.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
3856
by: mail.zhf | last post by:
if have two char use equality, out first. example: char str = "gbdfssdffss"; out is: f.
5
3994
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
1
1377
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 ignores an empty data field below is the output for the sentence above "GPZDA" "162913.48"
3
11039
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 rid of leading zeroes when I got the surprising result that my result set was empty. The only change in the select was to change CHAR(t4.dosage)||' '||t5.dosage_units AS dose,
26
2339
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 returnString. The problem is that returnString points to the correct value in the function, but after the function has finished, the string that it points to is empty. ..... char* radioList = NULL; ini->getParsedKey("Radio",radioList);
0
2619
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 *price, char *priceUnit, char *tempName, char *user1, char *user2, char *user3); allwaysOpean is 0 for every call. itemNumber is article code and other
3
1837
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 negative number". and i can't calculate double integers. And my last problem is to change the boolean function to flag and while operation to for. here is my program #include<iostream>
5
2642
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 (function() == '/0'). But then I see that the those if statements are flagging true when the function returns back a char * or no length
3
1735
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 a member of char' on the line that I have *** in front of below. Can someone help me fix this error? I don't know what that error means. Thanks!! Public Class Form1 Private Const paymentfile As String = "C:\checkfile.txt" Private...
0
9715
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
10940
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
10610
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10308
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7854
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
7030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5695
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...
1
4499
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
3
3141
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.