Code Review: strncpy

  • Thread starter Vijay Kumar R Zanvar
  • Start date
D

Dan Pop

In said:
So, what do you think of people who use NULL?

It depends on whether they are using it correctly or not ;-)
These symbolic constants are equally useful and/or useless (depending on
your point of view), in the context of a language where an integer constant
zero serves a number of roles!

The purpose of NULL is to avoid using 0 in a context where an integer
doesn't *naturally* belong.

END_OF_STRING serves no such purpose: 0 or '\0' are perfectly natural
representations of the null character and there is exactly one reason
for having a null character inside a string.

Dan
 
R

Richard Heathfield

Christian said:
That's what is called obfuscation. I sure sign of a wannabe-programmer.

How do you feel about

#define EOS '\0'

? Is that, too, a sure sign of a wannabe-programmer?

Before you answer, I should point out that that line is quoted from source
code written by Doug Gwyn for the book "Software Solutions in C". For my
part, I consider Mr Gwyn to be a real programmer, not a wannabe. (Minor
nit: EOS obviously invades implementation namespace, and I'm pretty sure Mr
Gwyn knows that.)
 
M

Mark McIntyre

How do you feel about

#define EOS '\0'

? Is that, too, a sure sign of a wannabe-programmer?

Very very close to a sure sign. In fact, its a million-to-one chance.
Before you answer, I should point out that that line is quoted from source
code written by Doug Gwyn for the book "Software Solutions in C".
For my part, I consider Mr Gwyn to be a real programmer, not a wannabe.

Me too. But he's not immune from adding wannabe-programmer style
obfuscatory gratuity to his code merely because he's a real
programmer.
 
P

pete

Mark said:
Very very close to a sure sign. In fact, its a million-to-one chance.


Me too. But he's not immune from adding wannabe-programmer style
obfuscatory gratuity to his code merely because he's a real
programmer.

I'm with Mark on this one.
I don't know if he had a good reason for writing bad style,
but that's bad style.
 
C

CBFalconer

Richard said:
.... snip ...

How do you feel about

#define EOS '\0'

? Is that, too, a sure sign of a wannabe-programmer?

Before you answer, I should point out that that line is quoted
from source code written by Doug Gwyn for the book "Software
Solutions in C". For my part, I consider Mr Gwyn to be a real
programmer, not a wannabe. (Minor nit: EOS obviously invades
implementation namespace, and I'm pretty sure Mr Gwyn knows that.)

That has been around for a loooong time. At least 25 years. I
don't remember where I first saw it, or even in what language.
 
R

Richard Heathfield

pete said:
I'm with Mark on this one.
I don't know if he had a good reason for writing bad style,
but that's bad style.

I agree, but there's a big difference between saying "that's bad style" and
"that's a SURE SIGN of a wannabe-programmer" (my caps).
 
M

Mark McIntyre

I agree, but there's a big difference between saying "that's bad style" and
"that's a SURE SIGN of a wannabe-programmer" (my caps).

Well, you know what they say about all generalisations.... :)
 
P

Peter Nilsson

Dan Pop said:
It depends on whether they are using it correctly or not ;-)


The purpose of NULL is to avoid using 0 in a context where an integer
doesn't *naturally* belong.

The standards formally differentiate integer and pointer types. The choice
of null pointer constants (for compatability with pre-existing C) _makes_ 0
belong *naturally*.

Macros like EOS and END_OF_STRING are as unnecessary as NULL is an
anachronism.
 
P

Peter Nilsson

Alan Balmer said:
The same as I think of people who use "while" or "switch". They're
using standard C. "END_OF_STRING" does not appear in the standard.

Your argument (so far) could also be used by advocates wanting the macro (or
one like it) put _into_ the standard.
Which is why I prefer the (standard) '\0' form to emphasize that I
intend one of those roles, that of the end of string marker.

Why do you feel you need to emphasise it? Do you feel C programmers should
use NULL? If so, why?
In fact,
if I saw code using END_OF_STRING my assumption would be that the
programmer was using some other character to mean end of string, and
I'd have to go find the definition. That's (mild) obfuscation.

I agree. But I think Christian Bau's labelling of Old Wolf detracted from
his comment.
 
D

Dan Pop

In said:
The standards formally differentiate integer and pointer types. The choice
of null pointer constants (for compatability with pre-existing C) _makes_ 0
belong *naturally*.

Nope, it belongs there by *pure* convention. In the absence of this
convention, a plain 0, having type int, would have no place in a pointer
context.
Macros like EOS and END_OF_STRING are as unnecessary as NULL is an
anachronism.

The anachronism is making 0 a null pointer constant. The purpose of NULL
is to hide this anachronism from C code. If the only null pointer
constant were (void *)0, one could argue that the *only* purpose of NULL
was to save a few keystrokes, since (void *)0 naturally belongs to a
pointer context.

Dan
 
P

pete

CBFalconer said:
That has been around for a loooong time. At least 25 years. I
don't remember where I first saw it, or even in what language.

Every once in while I see macros which make me think
"Pascal writers, forced to write C at gunpoint"

Is EOS like one of those ?
 

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

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,341
Latest member
hanifree

Latest Threads

Top