A Philisophical Question

A

August Karlstrom

Tim said:
The variable to_len is clearly supposed to be a length. It
shouldn't be compared to NULL. If we think the 'if' is
necessary, a test that makes more sense would be:

if( to_len > 0 ){
memmove( ptr, l_to, to_len );
ptr += to_len;
}

Yes, of course. I mixed it up with Pete's suggestion to use

if (l_to != NULL) {
memmove(ptr, l_to, to_len);
ptr += to_len;
}


August
 
S

Skarmander

August said:
Same here. I see it as a light form of language abuse.

C abuses itself by not having a `nil' keyword. You can pretend NULL is
that keyword for code you write yourself, of course, but since you'll
have to know the full story anyway to read existing code, it doesn't
help much. In any case, this is like brace style: you'll have to decide
for yourself, and as long as you're consistent with the environment
you're working in, it really doesn't matter.

S.
 
A

August Karlstrom

Skarmander said:
....
C abuses itself by not having a `nil' keyword. You can pretend NULL is
that keyword for code you write yourself, of course, but since you'll
have to know the full story anyway to read existing code, it doesn't
help much. In any case, this is like brace style: you'll have to decide
for yourself, and as long as you're consistent with the environment
you're working in, it really doesn't matter.

The symbol for the nil pointer is not the issue here. If p is a pointer
then I think `if (p != 0) ...' is perfectly OK although I prefer `if (p
!= NULL) ...".


August
 
S

Skarmander

August said:
The symbol for the nil pointer is not the issue here. If p is a pointer
then I think `if (p != 0) ...' is perfectly OK although I prefer `if (p
!= NULL) ...".
Ah, but *that* particular wart is the result of C not having a proper
boolean type. It all conspires. :)

S.
 
P

pete

Skarmander said:
August Karlstrom wrote:
Ah, but *that* particular wart is the result of C not having a proper
boolean type. It all conspires. :)

My convention for using

if (x)

instead of

if (x != 0)

is when the meaning of x is boolean in it's nature.
In the below example, it's "while" instead of "if".

int a_toi(const char *nptr)
{
int n;

n = 0;
while (isspace(*nptr)) {
++nptr;
}
if (*nptr != '-') {
if (*nptr == '+') {
++nptr;
}
while (isdigit(*nptr)) {
n = 10 * n - '0' + *nptr++;
}
} else {
++nptr;
while (isdigit(*nptr)) {
n = 10 * n + '0' - *nptr++;
}
}
return n;
}
 
A

August Karlstrom

pete said:
My convention for using

if (x)

instead of

if (x != 0)

is when the meaning of x is boolean in it's nature.

Yes, that's certainly the correct way. It's a classical beginners
mistake is to compare a boolean (or in C an int with boolean
interpretation) with a boolean literal.


August
 

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,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top