Proper Use of NULL

Discussion in 'C++' started by cppaddict, May 20, 2004.

  1. cppaddict

    cppaddict Guest

    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
    cppaddict, May 20, 2004
    #1
    1. Advertising

  2. cppaddict

    Howard Guest

    "cppaddict" <> wrote in message
    news:...
    > 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
    Howard, May 20, 2004
    #2
    1. Advertising

  3. cppaddict

    bartek Guest

    cppaddict <> wrote in
    news::

    > 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.
    bartek, May 20, 2004
    #3
  4. cppaddict

    Alan Johnson Guest

    Howard wrote:

    > "cppaddict" <> wrote in message
    > news:...
    >
    >>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
    Alan Johnson, May 20, 2004
    #4
  5. >
    > 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
    John Harrison, May 20, 2004
    #5
  6. cppaddict

    Howard Guest

    "John Harrison" <> wrote in message
    news:...
    > >
    > > 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! :)
    Howard, May 20, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harald Kirsch

    Proper use of CharsetDecoder?

    Harald Kirsch, Aug 4, 2003, in forum: Java
    Replies:
    1
    Views:
    3,906
    Roedy Green
    Aug 5, 2003
  2. Replies:
    5
    Views:
    26,588
    Mike Schilling
    Mar 29, 2006
  3. Carl
    Replies:
    21
    Views:
    986
    Patricia Shanahan
    Aug 24, 2006
  4. Replies:
    19
    Views:
    1,087
    Default User
    Apr 4, 2006
  5. H. Wade Minter
    Replies:
    8
    Views:
    286
    Robin
    Apr 25, 2004
Loading...

Share This Page