![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
Pointer Program
I'm trying to understand a few things in this program I'm working with out of one of my books. Here is the code:
int *pn = new int [3]; pn[0] = 2; pn[1] = 4; pn[2] = 6; cout << "pn[1] is: " << pn[1] << endl; // output is 4 pn += 1; cout << "pn[0] is: " << pn[0] << endl; // output is 4 cout << "pn[1] is: " << pn[1] << endl; // output is 6 First it outputs the value at pn[1] which is 4. I'm okay with that. Then the pointer is incremented by 1. So at this point the pointer is now pointing to the second element, since when we started the pointer was pointing at the first element. So the output again is 4. What's a little confusing is when he says: cout << "pn[0] is: " << pn[0] << endl; He still is using the pn[0] subscript even though the pointer is really pointing to the second element in the array. He could have done this instead: cout << "pn[1] is: " << *pn << endl; To me that would have been clearer. But I guess the compiler knows that because you told the pointer to point now to the second element in the array, if you write pn[0], it will still output pn[1]. Correct? What I don't get also is this: cout << "pn[1] is: " << pn[1] << endl; Somehow this outputs the value of the last array element. But why? Nowhere in the code did the pointer get told to point to the last element. Why does pn[1] output the value of the last element? Just need a little help here. Last edited by 357mag; Jul 9th, 2007 at 6:09 PM. Reason: spelling mistake |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
So like are the subscripts all being shifted over one place? Like sub[0] gets moved to sub[1] and sub[1] gets move to sub[2]?
|
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Pointer arithmetic. pn[n] merely adds the value of n to the pointer (taking into account the size of the element). Once pn has been incremented, then pn [0] is the element the pointer is now pointing to and pn [1] is the next element.
Note that by incrementing pn, the coder has screwed up his reference to the dynamic memory. It can't be deleted unless the pointer is restored to its original value. Bad thing to do. Make a copy before manipulating. I recommend that the read the pointer material linked to in my signature. If you've read it, read it again. One of the headings is pointer arithmetic.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jul 2005
Posts: 158
Rep Power: 0
![]() |
int *pn = new int [3]; // creates an array of 3 integers in the free store pn[0] = 2; // pn is the base address of the array you are writing 2 // there pn[1] = 4; // 4 is written to the address //element*sizeofint+baseaddress the second int pn[2] = 6; // same as before it's the third element cout << "pn[1] is: " << pn[1] << endl; // output is 4 prints the //value of the SECOND ELEMENT the element IS the OFFSET
__________________
Geeks may not be cool now but in the long run they prosper. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jul 2007
Location: Ohio
Posts: 19
Rep Power: 0
![]() |
pn[0] = pn (Always!)
pn[1] = pn+1 pn[2] = pn+2 This always holds. So if I do this: pn++; pn[0] = 4 pn[1] = 6 pn[2] = 8 then... pn[0] = 6 pn[1] = 8 pn[2] = ??? That's how you get those values. pn[2] would have some random number. |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You put your pn++ before the initialization; I think you meant after. Else, pn [0] still points to 4.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Language display in program | Prm753 | C++ | 3 | May 30th, 2006 5:45 PM |
| Creating a program to test a program | sixstringartist | C | 8 | Jan 21st, 2006 1:15 PM |
| HELP please!!! | hamacacolgante | C | 7 | Nov 21st, 2005 5:36 AM |
| Pointers in C (Part II) | Stack Overflow | C | 2 | Apr 29th, 2005 10:39 AM |
| Pointers in C (Part I) | Stack Overflow | C | 4 | Apr 28th, 2005 7:03 PM |