How to initialize a pointer in c++,

Discussion in 'C++' started by =?utf-8?B?5rW36aOO?=, Jun 7, 2006.

  1. How to initialize a pointer in c++,
    Mostly, I use null, for example,
    char * szName = null;
    However, if i compile it without including afxdisp.h
    , .net compiler tell me that the identifier is not declared.

    but if i base on lunix operate system, is it correct also.

    I think i shoud use 0, for example,
    char * szName =0;
    is it more general?

    thank in advanced
    =?utf-8?B?5rW36aOO?=, Jun 7, 2006
    #1
    1. Advertising

  2. =?utf-8?B?5rW36aOO?=

    Ian Collins Guest

    海风 wrote:
    > How to initialize a pointer in c++,
    > Mostly, I use null, for example,
    > char * szName = null;
    > However, if i compile it without including afxdisp.h
    > , .net compiler tell me that the identifier is not declared.
    >

    Not a standard header.

    Use NULL.

    --
    Ian Collins.
    Ian Collins, Jun 7, 2006
    #2
    1. Advertising

  3. Ian Collins wrote:
    > 海风 wrote:
    > > How to initialize a pointer in c++,
    > > Mostly, I use null, for example,
    > > char * szName = null;
    > > However, if i compile it without including afxdisp.h
    > > , .net compiler tell me that the identifier is not declared.
    > >

    > Not a standard header.
    >
    > Use NULL.


    Or just 0.


    Jonathan
    Jonathan Mcdougall, Jun 7, 2006
    #3
  4. =?utf-8?B?5rW36aOO?=

    benben Guest

    Ian Collins wrote:
    > 海风 wrote:
    >> How to initialize a pointer in c++,
    >> Mostly, I use null, for example,
    >> char * szName = null;
    >> However, if i compile it without including afxdisp.h
    >> , .net compiler tell me that the identifier is not declared.
    >>

    > Not a standard header.
    >
    > Use NULL.
    >


    NULL is not defined by default.

    Ben
    benben, Jun 7, 2006
    #4
  5. =?utf-8?B?5rW36aOO?=

    Ian Collins Guest

    benben wrote:
    > Ian Collins wrote:
    >
    >> 海风 wrote:
    >>
    >>> How to initialize a pointer in c++,
    >>> Mostly, I use null, for example,
    >>> char * szName = null;
    >>> However, if i compile it without including afxdisp.h
    >>> , .net compiler tell me that the identifier is not declared.
    >>>

    >> Not a standard header.
    >>
    >> Use NULL.
    >>

    >
    > NULL is not defined by default.
    >

    What is? NULL is part of standard C++, null isn't.

    There's nothing wrong with using 0, it's all a matter of style.

    --
    Ian Collins.
    Ian Collins, Jun 7, 2006
    #5
  6. "Ian Collins" <> schrieb im Newsbeitrag
    news:...
    > benben wrote:
    >> Ian Collins wrote:
    >>
    >>> 海风 wrote:
    >>>
    >>>> How to initialize a pointer in c++,
    >>>> Mostly, I use null, for example,
    >>>> char * szName = null;
    >>>> However, if i compile it without including afxdisp.h
    >>>> , .net compiler tell me that the identifier is not declared.
    >>>>
    >>> Not a standard header.
    >>>
    >>> Use NULL.
    >>>

    >>
    >> NULL is not defined by default.
    >>

    > What is? NULL is part of standard C++, null isn't.
    >
    > There's nothing wrong with using 0, it's all a matter of style.


    #define NULL 0L

    NULL indicates that the variable is a pointer. An NULL is a pointer to
    invalid memory.
    Gernot Frisch, Jun 7, 2006
    #6
  7. thanks all
    =?utf-8?B?5rW36aOO?=, Jun 7, 2006
    #7
  8. In message <>, Gernot Frisch
    <> writes
    >
    >"Ian Collins" <> schrieb im Newsbeitrag
    >news:...
    >> benben wrote:
    >>> Ian Collins wrote:
    >>>
    >>>> æμ·é£Ž wrote:
    >>>>
    >>>>> How to initialize a pointer in c++,
    >>>>> Mostly, I use null, for example,
    >>>>> char * szName = null;
    >>>>> However, if i compile it without including afxdisp.h
    >>>>> , .net compiler tell me that the identifier is not declared.
    >>>>>
    >>>> Not a standard header.
    >>>>
    >>>> Use NULL.
    >>>>
    >>>
    >>> NULL is not defined by default.
    >>>

    >> What is? NULL is part of standard C++, null isn't.
    >>
    >> There's nothing wrong with using 0, it's all a matter of style.

    >
    >#define NULL 0L
    >
    >NULL indicates that the variable is a pointer.


    No, NULL is not a variable. And it only indicates that you _intend_ to
    use it as a pointer.

    #include <iostream>
    #include <ostream>
    #include <cstddef>
    using std::cout;

    void f(int x)
    {
    cout << "int\n";
    }

    void f(char * x)
    {
    cout << "pointer\n";
    }

    int main()
    {
    f(NULL);
    }


    >An NULL is a pointer to
    >invalid memory.



    --
    Richard Herring
    Richard Herring, Jun 7, 2006
    #8
  9. Richard Herring wrote:
    -snip-
    > No, NULL is not a variable. And it only indicates that you _intend_ to
    > use it as a pointer.
    >
    > #include <iostream>
    > #include <ostream>
    > #include <cstddef>
    > using std::cout;
    >
    > void f(int x)
    > {
    > cout << "int\n";
    > }
    >
    > void f(char * x)
    > {
    > cout << "pointer\n";
    > }
    >
    > int main()
    > {
    > f(NULL);
    > }
    >
    >
    >> An NULL is a pointer to
    >> invalid memory.


    It gives "int" on my macintosh. Didn't you write it should be a pointer?


    Best regards
    Martin Jørgensen

    --
    ---------------------------------------------------------------------------
    Home of Martin Jørgensen - http://www.martinjoergensen.dk
    =?UTF-8?B?TWFydGluIErDuHJnZW5zZW4=?=, Jun 7, 2006
    #9
  10. In message <>, Martin Jørgensen
    <> writes
    >Richard Herring wrote:
    >-snip-
    >> No, NULL is not a variable. And it only indicates that you _intend_
    >>to use it as a pointer.
    >> #include <iostream>
    >> #include <ostream>
    >> #include <cstddef>
    >> using std::cout;
    >> void f(int x)
    >> {
    >> cout << "int\n";
    >> }
    >> void f(char * x)
    >> {
    >> cout << "pointer\n";
    >> }
    >> int main()
    >> {
    >> f(NULL);
    >> }
    >>
    >>> An NULL is a pointer to
    >>> invalid memory.

    >
    >It gives "int" on my macintosh.


    That's correct.

    > Didn't you write it should be a pointer?


    No, that was Gernot Frisch.

    My point is precisely that even if you *intend* it to be a pointer, the
    compiler actually treats it as an integer, so to say that it "is" a
    pointer is dangerously misleading.

    --
    Richard Herring
    Richard Herring, Jun 7, 2006
    #10
  11. >> Didn't you write it should be a pointer?
    >
    > No, that was Gernot Frisch.
    >
    > My point is precisely that even if you *intend* it to be a pointer,
    > the compiler actually treats it as an integer, so to say that it
    > "is" a pointer is dangerously misleading.


    Yes, you're right. I just wanted to say "NULL" indicates that the
    variable you assign NULL to is a poniter. Not NULL itself.
    Gernot Frisch, Jun 7, 2006
    #11
  12. =?utf-8?B?5rW36aOO?=

    Bo Persson Guest

    "Gernot Frisch" <> skrev i meddelandet
    news:...
    >>> Didn't you write it should be a pointer?

    >>
    >> No, that was Gernot Frisch.
    >>
    >> My point is precisely that even if you *intend* it to be a pointer,
    >> the compiler actually treats it as an integer, so to say that it
    >> "is" a pointer is dangerously misleading.

    >
    > Yes, you're right. I just wanted to say "NULL" indicates that the
    > variable you assign NULL to is a poniter. Not NULL itself.


    But in the code here

    >> I think i shoud use 0, for example,
    >> char * szName =0;
    >> is it more general?


    it is pretty obvious that we are initializing a pointer. :)

    In that case, using 0 is very general, and absolutely portable. No
    headers required.


    Bo Persson
    Bo Persson, Jun 7, 2006
    #12
  13. =?utf-8?B?5rW36aOO?=

    marius lazer Guest

    Bo Persson wrote:

    >
    > In that case, using 0 is very general, and absolutely portable. No
    > headers required.


    NULL is NOT standard C++. The standard specifically mentions the use of
    an unadorned zero (0) instead of NULL. Be careful that some older
    header files define NULL as (void*) 0. That's against the standard.

    Also, standard C++ guarrantees that delete 0 or NULL pointer is a
    no-op.

    Marius
    marius lazer, Jun 8, 2006
    #13
  14. =?utf-8?B?5rW36aOO?=

    Noah Roberts Guest

    marius lazer wrote:
    > Bo Persson wrote:
    >
    > >
    > > In that case, using 0 is very general, and absolutely portable. No
    > > headers required.

    >
    > NULL is NOT standard C++. The standard specifically mentions the use of
    > an unadorned zero (0) instead of NULL. Be careful that some older
    > header files define NULL as (void*) 0. That's against the standard.


    It can't be both, which is it? Does the standard not define NULL or is
    it against the standard to define NULL as (void*)0?
    Noah Roberts, Jun 8, 2006
    #14
  15. =?utf-8?B?5rW36aOO?=

    Noah Roberts Guest

    Noah Roberts wrote:
    > marius lazer wrote:
    > > Bo Persson wrote:
    > >
    > > >
    > > > In that case, using 0 is very general, and absolutely portable. No
    > > > headers required.

    > >
    > > NULL is NOT standard C++. The standard specifically mentions the use of
    > > an unadorned zero (0) instead of NULL. Be careful that some older
    > > header files define NULL as (void*) 0. That's against the standard.

    >
    > It can't be both, which is it? Does the standard not define NULL or is
    > it against the standard to define NULL as (void*)0?


    BTW, 18.1/4
    Noah Roberts, Jun 8, 2006
    #15
  16. =?utf-8?B?5rW36aOO?=

    red floyd Guest

    Noah Roberts wrote:
    > Noah Roberts wrote:
    >> marius lazer wrote:
    >>> Bo Persson wrote:
    >>>
    >>>> In that case, using 0 is very general, and absolutely portable. No
    >>>> headers required.
    >>> NULL is NOT standard C++. The standard specifically mentions the use of
    >>> an unadorned zero (0) instead of NULL. Be careful that some older
    >>> header files define NULL as (void*) 0. That's against the standard.

    >> It can't be both, which is it? Does the standard not define NULL or is
    >> it against the standard to define NULL as (void*)0?

    >
    > BTW, 18.1/4
    >


    Specifically footnote 180: "Possible definitions include 0 and 0L, but
    not (void*)0."
    red floyd, Jun 8, 2006
    #16
  17. =?utf-8?B?5rW36aOO?=

    marius lazer Guest

    Noah Roberts wrote:

    > It can't be both, which is it? Does the standard not define NULL or is
    > it against the standard to define NULL as (void*)0?


    The C++ standard does not define NULL, but most compiler or system
    header files do (leftover from K&R C). If your NULL is defined as
    (void*)0 or 0L or whatever other than plain 0 do not use it! The
    standard states to use unadorned 0 so that's what I've been doing since
    '93.

    Marius
    marius lazer, Jun 8, 2006
    #17
  18. =?utf-8?B?5rW36aOO?=

    Old Wolf Guest

    marius lazer wrote:
    > The C++ standard does not define NULL, but most compiler or system
    > header files do (leftover from K&R C).


    The C++ standard specifies that certain headers must define NULL.
    Those headers are <clocale>, <cstddef>, <cstdio>, <cstdlib>,
    <cstring>, <ctime>, and <cwchar>.
    Old Wolf, Jun 9, 2006
    #18
  19. =?utf-8?B?5rW36aOO?=

    red floyd Guest

    marius lazer wrote:
    > Noah Roberts wrote:
    >
    >> It can't be both, which is it? Does the standard not define NULL or is
    >> it against the standard to define NULL as (void*)0?

    >
    > The C++ standard does not define NULL, but most compiler or system
    > header files do (leftover from K&R C). If your NULL is defined as
    > (void*)0 or 0L or whatever other than plain 0 do not use it! The
    > standard states to use unadorned 0 so that's what I've been doing since
    > '93.
    >


    Actually, footnote 180 to 18.1/4 says that 0L is legit. However
    (void*)0 is specifically banned.
    red floyd, Jun 9, 2006
    #19
  20. =?utf-8?B?5rW36aOO?=

    marius lazer Guest

    Old Wolf wrote:

    > The C++ standard specifies that certain headers must define NULL.
    > Those headers are <clocale>, <cstddef>, <cstdio>, <cstdlib>,
    > <cstring>, <ctime>, and <cwchar>.


    Yes, but these are the "old" C header files wrapped in the std
    namespace. C++ has no need for NULL.

    Marius
    marius lazer, Jun 9, 2006
    #20
    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. Replies:
    10
    Views:
    663
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    813
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,218
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    620
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    437
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page