By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Proper Use of NULL

P: n/a
Hi,

A quick question regarding the use of NULL.

I have a Reader class, which reads from a Display. Reader has two
relevant members for this question:

CharsetCollection
-- a collection of possible Charsets being used by Display
activeCharset
-- the actual Charset being used by Display

The first time a Reader object invokes its findNextChar() method, it
will search every character in each of its Charsets and try to find a
match for the character at the current cursor position in Display. It
will then set activeCharset to the appropriate Charset based on the
search results, so that on future searches only that Charset will be
searched.

Right now, I have the constructor of Reader setting
activeCharset(NULL) in its initialization list.

Then my findNextChar() method looks like this:

char Reader::findNextChar() {
if (activeCharset == NULL) { //compiler errors here
search all Charsets
set activeCharset based on search results
} else {
search only activeCharset
}
}

My question is: What is the right way to do what I am trying to do.
Am I simply using NULL incorrectly? Is my design on the wrong track
altogether?

thanks for any ideas,
cpp
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"cppaddict" <he***@hello.com> wrote in message
news:67********************************@4ax.com...
Hi,

char Reader::findNextChar() {
if (activeCharset == NULL) { //compiler errors here
search all Charsets
set activeCharset based on search results
} else {
search only activeCharset
}
}

My question is: What is the right way to do what I am trying to do.
Am I simply using NULL incorrectly? Is my design on the wrong track
altogether?


You don't say what the specific error is, or give us the declaration of
activeCharset, so we can't say for sure. But, if activeCharset is a
pointer, you can use nil. If it's an integer, you can use 0. (The term
NULL, I think, is not part of the standard, and is often defined as 0.)
-Howard

Jul 22 '05 #2

P: n/a
cppaddict <he***@hello.com> wrote in
news:67********************************@4ax.com:
My question is: What is the right way to do what I am trying to do.
Am I simply using NULL incorrectly? Is my design on the wrong track
altogether?


NULL is zero (0) in C++. Should be used with pointers only.
Is activeCharset a pointer?

All in all, it's better to use a plain zero (0) instead of NULL. Less
confusion, etc.

Besides, NULL is a preprocessord definition, and as we all know,
preprocessor is evil. period.

On the other hand... Are the guys behind Boost.Preprocessor from hell? huh?
If the preprocessor alone is evil, then that library is the purest, most
condensed, archetypal evil to walk the earth, soon reaching for critical
mass! :)

But I digress...

Cheers.
Jul 22 '05 #3

P: n/a
Howard wrote:
"cppaddict" <he***@hello.com> wrote in message
news:67********************************@4ax.com...
Hi,

char Reader::findNextChar() {
if (activeCharset == NULL) { //compiler errors here
search all Charsets
set activeCharset based on search results
} else {
search only activeCharset
}
}

My question is: What is the right way to do what I am trying to do.
Am I simply using NULL incorrectly? Is my design on the wrong track
altogether?

You don't say what the specific error is, or give us the declaration of
activeCharset, so we can't say for sure. But, if activeCharset is a
pointer, you can use nil. If it's an integer, you can use 0. (The term
NULL, I think, is not part of the standard, and is often defined as 0.)
-Howard


The standard says that NULL exists, but is implementation-defined.

Section 18.1, line 4 :

"The macro NULL is an implementation-defined C + + null pointer constant
in this International Standard (4.10)."

It also mentions that it is defined in <cstddef>.

Section 4.10 starts by defining a "null pointer constant" :

"A null pointer constant is an integral constant expression (5.19)
rvalue of integer type that evaluates to zero."
Alan
Alan
Jul 22 '05 #4

P: n/a
>
You don't say what the specific error is, or give us the declaration of
activeCharset, so we can't say for sure. But, if activeCharset is a
pointer, you can use nil. If it's an integer, you can use 0. (The term
NULL, I think, is not part of the standard, and is often defined as 0.)
-Howard


Wrong way round, NULL is standard, nil is not.

john
Jul 22 '05 #5

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2h************@uni-berlin.de...

You don't say what the specific error is, or give us the declaration of
activeCharset, so we can't say for sure. But, if activeCharset is a
pointer, you can use nil. If it's an integer, you can use 0. (The term
NULL, I think, is not part of the standard, and is often defined as 0.)
-Howard


Wrong way round, NULL is standard, nil is not.

john


D'OH! :-)

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.