How to initialize a pointer in c++,

G

Guest

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
 
I

Ian Collins

海风 said:
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.
 
I

Ian Collins

benben said:
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.
 
G

Gernot Frisch

Ian Collins said:
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.
 
R

Richard Herring

Gernot Frisch said:
#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);
}
 
G

Guest

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);
}

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


Best regards
Martin Jørgensen
 
R

Richard Herring

Martin Jørgensen said:
Richard Herring wrote:
-snip-

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.
 
G

Gernot Frisch

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.
 
B

Bo Persson

Gernot Frisch said:
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

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
 
M

marius lazer

Bo said:
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
 
N

Noah Roberts

marius said:
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?
 
M

marius lazer

Noah said:
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
 
O

Old Wolf

marius said:
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>.
 
R

red floyd

marius said:
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.
 
M

marius lazer

Old said:
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
 

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
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top