Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 9th, 2007, 6:06 PM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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
357mag is offline   Reply With Quote
Old Jul 9th, 2007, 6:16 PM   #2
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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]?
357mag is offline   Reply With Quote
Old Jul 9th, 2007, 6:18 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 11th, 2007, 3:18 AM   #4
teencoder
Hobbyist Programmer
 
teencoder's Avatar
 
Join Date: Jul 2005
Posts: 158
Rep Power: 0 teencoder is an unknown quantity at this point
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
Pointers are a tough subject. Also it is important to delete an object when you are done or you will create a memory leak.
__________________
Geeks may not be cool now but in the long run they prosper.
teencoder is offline   Reply With Quote
Old Jul 11th, 2007, 11:14 AM   #5
francoissoft
Newbie
 
Join Date: Jul 2007
Location: Ohio
Posts: 19
Rep Power: 0 francoissoft is on a distinguished road
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.
francoissoft is offline   Reply With Quote
Old Jul 11th, 2007, 11:48 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:54 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC