943,734 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 446
  • C++ RSS
Jul 29th, 2008
0

Pointer Values

Expand Post »
I was reviewing some material over pointers in a book called C++ Primer 4th Edition and it said there were four possible values that you can set to a pointer.
  1. A constant expression with a value of 0
  2. An address of an object of an appropriate type
  3. The address one past the end of another object
  4. Another valid pointer of the same type

I am not sure exactly what is meant by the third type. Is past a typo that should be passed?

From what I understand this is what is meant by the other three types
1)
const int c_ival = 0;
int *pNum1 = c_ival;

2)
int ival = 1;
int *pNum2 = &ival;

3)
????

4)
int *pNum3 = pNum2;

Any help would be appreciated thanks.
Similar Threads
Reputation Points: 11
Solved Threads: 2
Light Poster
kinger29 is offline Offline
35 posts
since Mar 2008
Jul 29th, 2008
0

Re: Pointer Values

I think what he's referring to in #1 is a NULL pointer:
C++ Syntax (Toggle Plain Text)
  1. int* x = NULL;
  2. //or
  3. int* x = 0;

I'm guessing this is what is meant by #3:
C++ Syntax (Toggle Plain Text)
  1. int x[10] = {0};
  2. x[3] = 5;
  3. x[4] = 13;
  4. int* pInt = (&x[3])+1;

*pInt would be 13
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Jul 29th, 2008
0

Re: Pointer Values

Case 3 means you can do this.
C++ Syntax (Toggle Plain Text)
  1. int arr[10];
  2. int *pStart = arr;
  3. int *pEnd = &arr[10]; // one past the end
  4. while ( pStart < pEnd ) {
  5. pStart++;
  6. }
Note that whilst you can POINT to the element just off the end of the array, any attempt to dereference pEnd would be undefined behaviour.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reading bytes from a particular address
Next Thread in C++ Forum Timeline: Bool problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC