473,394 Members | 1,640 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,394 software developers and data experts.

0 insted of '\0' in char ptr


I have come across the following code which treys to implement the
strcat function. It may
be wrong or correct, but it is using integer zero instead of
appending the C string terminator '\0'.

#include <stdarg.h>
char *xstrcat(char *des, char *src, ...)
{

char *destination = des;
va_list v;
va_start(v, src);
while (src != 0)
{

while (*src != 0)

*des++ = *src++;

src = va_arg(v, char *);

}

*des = 0;
va_end(v);
return destination;
}

Is the above code is UB? Please explain.

--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan AT gmail DOT com
(AT = @ and DOT = .)
Nov 14 '05 #1
4 1550
sathya <sa****@nomail.com> wrote:
I have come across the following code which treys to implement the
strcat function. It may
be wrong or correct, but it is using integer zero instead of
appending the C string terminator '\0'.


That's quite correct. '\0' _is_ an int zero; character constants have
type int, for historical reasons. The necessary conversion to char type
will be done automatically; the process should be identical regardless
of whether you spell the int zero 0 or '\0'.

Richard
Nov 14 '05 #2
sathya wrote:

I have come across the following code which treys to implement the
strcat function. It may be wrong or correct, but it is using
integer zero instead of appending the C string terminator '\0'.

#include <stdarg.h>
char *xstrcat(char *des, char *src, ...)
{
char *destination = des;
va_list v;
va_start(v, src);
while (src != 0)
{
while (*src != 0)
*des++ = *src++;
src = va_arg(v, char *);
}
*des = 0;
va_end(v);
return destination;
}

Is the above code is UB? Please explain.


No it isn't for that reason. However it is a horror and an error
prone trap, as is almost any variadic function in C. You would be
better off using the FreeBSD originated strlcat and strlcpy
routines, for which you can find an implementation at:

<http://cbfalconer.home.att.net/download/>

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #3
On Tue, 14 Dec 2004 12:14:26 UTC, sathya <sa****@nomail.com> wrote:

I have come across the following code which treys to implement the
strcat function. It may
be wrong or correct, but it is using integer zero instead of
appending the C string terminator '\0'.


'\0' is commonly used to document inline that you means a (char)0.
0, char(0), '\0' have always the same meaning but

0 requires from the reader to see that you assigns/compares a char.
The reader has to think an extra step to see that you means a char.

char(0): casting is always an dangerous option, requiring rethinking
it if it does really what you needs and means, so avoiding casts
whenever possible makes the code more readable.

'\0' is documenting that you speaks about chars, not ints. It is more
for the reader than for the compiler.

So both, 0 and '\0' are commonly used to address a null value as char,
whereas '\0' is more documentative but costs 3 extra typings. C
programmers are often sluggish, so they avoid typing as they can.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Nov 14 '05 #4
"Herbert Rosenau" <os****@pc-rosenau.de> wrote:
On Tue, 14 Dec 2004 12:14:26 UTC, sathya <sa****@nomail.com> wrote:
I have come across the following code which treys to implement the
strcat function. It may
be wrong or correct, but it is using integer zero instead of
appending the C string terminator '\0'.


'\0' is commonly used to document inline that you means a (char)0.
0, char(0), '\0' have always the same meaning but


No, they don't; 0 has not just exactly the same meaning, but also the
same type, as '\0', but char(0) is a syntax error (and (char)0, which
you might have meant, has the same value, but a different type).

Richard
Nov 14 '05 #5

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

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
1
by: Schultz | last post by:
Hi, I would like to be able to edit data in a datagrid by column insted of by row. Is this possible? And, how can one make an entire Datagrid editable by clicking on "Edit All" > "Update" or...
2
by: hdf | last post by:
hi could you hlp me with this each time i draw reports from my sql database it display the following charaters don’t insted of the don't and you hlpme with this. it hpens every time some one...
4
seshu
by: seshu | last post by:
Hi everybody Presently Iam write three select statements with diff where and limt conditions but the select stmt in them is same insted of that can i write only one direct stmt...
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
2
by: yaniv111 | last post by:
hello: i got a wierd case: i am trying to add an item to the list but i am facing a problem: public partial class test: System.Web.UI.Page { protected void Page_Load(object sender,...
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.