Thread: Array on C++ VTM .. CONFUSE!! plz help

1. Pointers/Array on C++ VTM .. CONFUSE!! plz help

Trying to understand those Pointers, I do this:
================================
PHP Code:
```  int postOffice[3] = {3, 2, 4};      int *ptrInt = postOffice;  for (int i=0; i<3;i++) {    cout << "*ptrInt = " << *ptrInt << endl;   cout << "(int)ptrInt = " << (int)ptrInt << endl;            *ptrInt++; // Q1. (questio nbelow)What is happening   cout << "*postOffice = " << *postOffice << endl;      //postOffice++; //Q2. (Question below) Why can't we do this }  ```
=====OutPut =========
*ptrInt = 3
(int)ptrInt = 1244876
*postOffice = 3
*ptrInt = 2
(int)ptrInt = 1244880
*postOffice = 3
*ptrInt = 4
(int)ptrInt = 1244884
*postOffice = 3

======= My Question is ===========
Q1. As above, I don't understand the *ptr++; thing
Isn't *ptr refer to the actual VALUE?, "3"
If you use ptr++; i can understand that it jump to the next byte in the address. but *ptr++ should means it add 1 to the actual value... 3+1

Q2. So, since you can do int *ptr = postOffice;
and you can do *ptr++,
Why can't you do postOffice++ ????? Isn't postOffice contain a memory address?, why can't we move it 1 byte forward by incrementing it...

Just like the example in the vtm where u have
int *ptr = new int[5];
ptr += 2; // move 2 byte forward...

Sorry if it too long, but I've been watching this video for 2 days, still confuse..

THANKS
Last edited by alexqwerty; 01-13-2005 at 11:38 AM.

2. Registered User
Join Date
Jun 2003
Posts
36
Q1. As above, I don't understand the *ptr++; thing
Isn't *ptr refer to the actual VALUE?, "3"
If you use ptr++; i can understand that it jump to the next byte in the address. but *ptr++ should means it add 1 to the actual value... 3+1
++ can be a pre or post operator depending upon where it is placed, in this case after the "ptr" means your doing your operation, then increasing ptr by 1. The operation your doing is reading the value of ptr (due to the *).

If you did:

Code:
`cout << *ptr++ << endl;`
Then you would after 1 execution see 3 output, and ptr would be now pointing at 2.

If your just wanting to increment ptr to point to the next memory location you need to do "ptr++" by itself, without the *. You only need the star to get hold of the value held in the memory location that ptr points to.

If instead you wanted to increment what Ptr pointed to, eg one of the Values, then you would need to use

Code:
`(*ptr)++`
Q2. So, since you can do int *ptr = postOffice;
and you can do *ptr++,
Why can't you do postOffice++ ????? Isn't postOffice contain a memory address?, why can't we move it 1 byte forward by incrementing it...

Just like the example in the vtm where u have
int *ptr = new int[5];
ptr += 2; // move 2 byte forward...
in the example PTR is a pointer, that is its a location in memory that stores the address of something else. You cannot change the location in memory that ptr exists at, but you can change what it holds in that location in memory, and thus change what it points to.

In the postOffice example, post office is not declared to be a pointer and for this reason (I think, although not 100% certain) pointer arithmetic cannot be used.

I'm not all that good at explaining things, so I hope the above is understandable.. If not I'm sure someone will clarify

BTW in the case of wanting to increase postOffice to move through the array, you would be best served (assuming you don't want to just use postOffice[1] etc) to declare a pointer to the array eg

Code:
```int postOffice[3] = {67, 34, 82};
int *myPtr;

myPtr = postOfifice;```
Now you can do myPtr++ etc and you don't have to worry about doing myPtr-- or myPtr-=2 or whatever to move back to the start of the array, you just do myPtr = postOffice; again
Last edited by muphicks; 01-13-2005 at 03:55 PM.

3. Oh, that is extremely helpful... Thank you so much.

FROM Q1. so basically, *ptr++ :

- - means *(ptr+1) which point to the next memory location then use that value.

-- not (*ptr)+1 .. which is the actual value + 1 .. i.e 3+1

THANK You, I didn't know that the pre ++ and post ++ is applying to this situaton... I'm enlighted !!! ...

From Q2.
You cannot change the location in memory that ptr exists at, but you can change what it holds in that location in memory, and thus change what it points to.
this 1 sentence helps me so much. Thanks for clarification.

Still not sure about the array postOffice though, 'cause in the VTM, Buzz said that array postOffice is actually a pointer pointing to postOffice[0].

e.g postOffice same as &postOffice[0]
That why i'm confuse why can't we do *postOffice++ .

You save me months of reading book.. Thankyou so much.
Last edited by alexqwerty; 01-13-2005 at 11:52 PM.

4. Registered User
Join Date
Jun 2003
Posts
36
FROM Q1. so basically, *ptr++ :

- - means *(ptr+1) which point to the next memory location then use that value.

-- not (*ptr)+1 .. which is the actual value + 1 .. i.e 3+1
Not quite. Its returning the value first, then incrementing the pointer to point to the next memory location. Remember that ++ after something is a post opperation, so it will occur after the expression has been evaluated (and usually assigned to something). EG:

Code:
```  int postOffice[3] = {72, 45, 87};
int *ptr = postOffice;

cout << *ptr << endl;
cout << (int)ptr << endl;

int myInt = *ptr++;

cout << *ptr << endl;
cout << (int)ptr << endl;
cout << myInt << endl;```
In the above example, if you try it, you should see the value of pointer been 72. But after the call to *ptr++ the value of pointer should be 45. However the important bit, is what the value of "myInt" is.

Is ptr been incremented to point to 45, then the value returned eg 45. Or is the value of pointer 72 been returned, then pointer incremented... Try it and find out

Also you might want to comment out the myInt line and replace it with the following

Code:
`int myInt = (*ptr)++;`
and see what effect that has on your code

5. OHHHHHHHHHHHH!!!!!!!!!... I thought i already understand everything :

int myInt = *ptr++;
Let me guess, the answer is 45.
.. debug ... wait a sec.

So like you said, it actually assign the value 72 to myInt first, then it does the increment. After this line, *ptr will become 45.

Those Pre++ and post++ is so tricky...
-------------

int myInt = (*ptr)++;

Let me guess, the answer would be 72 + 1 = 73.
No, actually it does the assignment before increment. so it should still be 72.
.. Debug.. Wait
HEY I'm right, the answer is 72.

---------------------
Does that mean i'm fully understood the concept of ++ and pointer?

Thank you so much. Just one more thing. If i use &ptr, it will return the address of the pointer right?. I've just try it. i think so.

Thanks again.. (you don't know how happy i am now)

6. Registered User
Join Date
Jun 2003
Posts
36
Glad you understand now. It means my explinations maybe arn't as bad as I originally thought

7. Your explanation was very details and spot on. Better than books .

Thank muphicks, i can move on the next vtm now.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•