Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - Pointers - *p++

 
Thread Tools Search this Thread
Old 11-01-2009, 10:24 PM   #1
Default Pointers - *p++


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.


crystal twix
  Reply With Quote
Old 11-01-2009, 11:27 PM   #2
red floyd
 
Posts: n/a
Default Re: Pointers - *p++
crystal twix wrote:
> 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.


red floyd
  Reply With Quote
Old 11-02-2009, 02:40 PM   #3
Michael Tsang
 
Posts: n/a
Default Re: Pointers - *p++
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

crystal twix wrote:

> 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+4kgCgkS2jciV057 fHKMZoMzVT6iNK
yjgAnRRhKLN/i+EOEZOX87I6V947BiR9
=V0pC
-----END PGP SIGNATURE-----



Michael Tsang
  Reply With Quote
Old 11-02-2009, 06:44 PM   #4
siddhant3s
 
Posts: n/a
Default Re: Pointers - *p++

> Better to write "int *p=NULL;" instead


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


siddhant3s
  Reply With Quote
Old 11-03-2009, 12:38 AM   #5
paperab
 
Posts: n/a
Default Re: Pointers - *p++
On Nov 2, 6:44*pm, siddhant3s <siddhan...@gmail.com> wrote:
> > Better to write "int *p=NULL;" instead

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


paperab
  Reply With Quote
Old 11-03-2009, 03:50 AM   #6
Ian Collins
 
Posts: n/a
Default Re: Pointers - *p++
crystal twix wrote:
> 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.

--
Ian Collins


Ian Collins
  Reply With Quote
Old 11-03-2009, 10:57 AM   #7
James Kanze
 
Posts: n/a
Default Re: Pointers - *p++
On Nov 3, 12:38 am, paperab <ab9380...@yahoo.co.uk> wrote:
> On Nov 2, 6:44 pm, siddhant3s <siddhan...@gmail.com> wrote:


> > > Better to write "int *p=NULL;" instead


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


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.

--
James Kanze


James Kanze
  Reply With Quote
Old 11-03-2009, 10:58 AM   #8
James Kanze
 
Posts: n/a
Default Re: Pointers - *p++
On Nov 3, 3:50 am, Ian Collins <ian-n...@hotmail.com> wrote:
> crystal twix wrote:
> > 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.


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

--
James Kanze


James Kanze
  Reply With Quote
Old 11-03-2009, 06:35 PM   #9
siddhant3s
 
Posts: n/a
Default Re: Pointers - *p++
On Nov 3, 5:38*am, paperab <ab9380...@yahoo.co.uk> wrote:
> On Nov 2, 6:44*pm, siddhant3s <siddhan...@gmail.com> wrote:
>
> > > Better to write "int *p=NULL;" instead

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


Read http://www.research.att.com/~bs/bs_faq2.html#null


siddhant3s
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Warning - pump and dumpers sending out trojan pointers Tester Computer Support 2 08-19-2007 07:32 PM
Mouse pointers? Gorbag Computer Support 1 06-29-2005 08:26 PM
Newbie looking for pointers Tom Wireless Networking 2 10-17-2004 02:43 AM
Toolbar 'pointers' are 5's and 6's and 8's??? fredcromer Computer Support 1 04-21-2004 08:48 PM
Pointers also changing into NUMBERS !!!?? fredcromer Computer Information 1 04-21-2004 08:27 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46