Pointers - *p++

C

crystal twix

Hi. I'm trying to understand what my professor says when he refers to
declaring a pointer, and seeing the operator precedence and seeing
what happens when we do things like

p++
*p++
(*p)++ etc,

but he starts out with this declaration
int *p = (int *)0;

Although my compiler does not complain, when I try to do something
like
cout << *p;

I get a Bus Error on the console (I'm using Xcode). What am I doing
wrong here? Thanks.
 
R

red floyd

crystal said:
Hi. I'm trying to understand what my professor says when he refers to
declaring a pointer, and seeing the operator precedence and seeing
what happens when we do things like

p++
*p++
(*p)++ etc,

but he starts out with this declaration
int *p = (int *)0;

Although my compiler does not complain, when I try to do something
like
cout << *p;

I get a Bus Error on the console (I'm using Xcode). What am I doing
wrong here? Thanks.

Dereferencing a NULL pointer.
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

crystal said:
Hi. I'm trying to understand what my professor says when he refers to
declaring a pointer, and seeing the operator precedence and seeing
what happens when we do things like

p++
*p++
(*p)++ etc,

but he starts out with this declaration
int *p = (int *)0;
Better to write "int *p=NULL;" instead
Although my compiler does not complain, when I try to do something
like
cout << *p;
p is a pointer object. As the value of p is NULL, dereferencing it becomes
an object that does not exist. The output operation tries to take the value
in the object, however, as the object does not exist, the behaviour is
undefined.
I get a Bus Error on the console (I'm using Xcode). What am I doing
wrong here? Thanks.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkru724ACgkQG6NzcAXitM+4kgCgkS2jciV057fHKMZoMzVT6iNK
yjgAnRRhKLN/i+EOEZOX87I6V947BiR9
=V0pC
-----END PGP SIGNATURE-----
 
P

paperab

I am *not* saying anything against but, I write int* p=0; as it remind
me that in C++ 'NULL' is 'zer0'.

This is not always true. It depends of the implementation of your C++.
 
I

Ian Collins

crystal said:
Hi. I'm trying to understand what my professor says when he refers to
declaring a pointer, and seeing the operator precedence and seeing
what happens when we do things like

p++
*p++
(*p)++ etc,

but he starts out with this declaration
int *p = (int *)0;

If you get into the habit of declaring and initialising pointers at the
point of first use, the discussion is moot.
 
J

James Kanze

Yes. It's a style issue. I too prefer NULL, but I understand
people who prefer 0. Either is "idiomatic". The original:
int *p = (int*)0;
isn't.
This is not always true. It depends of the implementation of
your C++.

It's not necessarily true that NULL expands to the exact string
0, but it must expand to a null pointer constant, i.e. an
integral constant expression evaluating to 0. (It's interesting
that a null pointer constant is not allowed to be a pointer.) 0
is by far the most frequent solution, and 0L was also prevelant
at one time. At least one compiler uses something like
__builtin_null, so that it can warn if NULL is used as an
integer; this is by far the best solution.
 
J

James Kanze

If you get into the habit of declaring and initialising
pointers at the point of first use, the discussion is moot.

He did. He initialized it with a null pointer, then proceeded
with a number of operations which aren't legal on null pointers.
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top