# Pinter aurithmetic

• 01-08-2009
swgh
Pinter aurithmetic
Hi Guys.

I am doing the final question for my homework sheet and I am having alot of trouble with it.

I have to print the following:

Code:

`int brainTeaser[ ARRAY_SIZE ] = { 1, 7, 11, 27, 77, 107, 111, 127, 177 }`
Using pointer aurithmetic and I am not allowed to use indexing notation.
This is proving harder than I thought. I have come up with this:

Code:

```#include <iostream> // main function - driver ///////////////////////////////////////////////////// // int main ( void ) {         const int ARRAY_SIZE = 9;         int brainTeaser[ ARRAY_SIZE ] = { 1, 7, 11, 27, 77, 107, 111, 127, 177 };         int *ptr = brainTeaser;         for ( ; *ptr < ARRAY_SIZE; ( ptr )++ ) {                 std::cout << *ptr << " ";         }         std::cin.get();         return 0; }```
Which appears to work ok, but then prints out only 1 and 7. If I change the
< to != it prints the array ok but also it prints two more uknown addreses which means I have gone beyond the array bounds.

Could anyone give me any help on where I could be going wrong? I am sure my (ptr)++ is correct. But then again could it be the size I have got wrong in the for loop?

Any hints apprciated.
• 01-08-2009
matsp
Code:

`*ptr < ARRAY_SIZE;`
That is not the right way to determine that you have reached the end [unless of course your array looks like this
Code:

`int brainTeaser[ ARRAY_SIZE ] = { 1, 7, 11, 27, 77, 107, 111, 127, 177, ARRAY_SIZE };`
(Of course, the ARRAY_SIZE should then be 1 larger than now).

Perhaps you want to take the difference between ptr and brainTeaser?

--
Mats
• 01-08-2009
C_ntua
The *ptr < ARRAY_SIZE is wrong.
Code:

```for (int i =0 ; i < ARRAY_SIZE; ++i)   cout << *(ptr + i) << ' ';```
your code wold make sense like this:
Code:

```int last_element = 177 for (; *ptr != last_element; ++ptr)     cout << *ptr << ' '; cout << *ptr << ' ';  //print also last element```
• 01-08-2009
Elysia
Furthermore, your code is not much better than swgh's.
Still, I am curious...
What do you think *ptr < ARRAY_SIZE does, swgh? Why are you using it in the first place?
• 01-08-2009
laserlight
Quote:

Originally Posted by swgh
Using pointer aurithmetic and I am not allowed to use indexing notation.

Cheat: first write it out using index notation, then convert every a[n] to *(a+n).

On the other hand, you are probably not supposed to use a counter at all, so this cheat might be forbidden... but it can still help since then you just change the counter to a pointer, and instead of using some upper bound, use a pointer that is one past the end (cosmetic changes, as it were, since an optimising compiler will do something similiar anyway).
• 01-08-2009
swgh
Elysia I assumed at first *ptr began at zero like an array does. so i could use it as i woud in a normal array

Code:

`for ( int i = 0; // this is the pointer starting value i < ARRAY_SIZE; i++ )`
I had learned that to use pointer aurethmetic you had to do this: (ptr)++ so that is why I had that as the end
of the for loop instead of using the i++ index as i could with a normal array.

I can see now how wrong that is :p but it is good to make mistakes as you learn from them, and pointers are never simple.
• 01-08-2009
KIBO
Code:

`p < brainTeaser + ARRAY_SIZE`
brainTeaser + ARRAY_SIZE computes the address one past the in the array

• 01-08-2009
Elysia
Quote:

Originally Posted by swgh
Elysia I assumed at first *ptr began at zero like an array does. so i could use it as i woud in a normal array

Your mindset is correct, I think.
You must stop "assuming" and "stealing" common ways to do things like this.
First, you must learn the basics. Then you can look at those ways and see how they really work, and then implement your solution.
The "*ptr" simply dereferences the pointer, and what does that do, do you know?

Quote:

I had learned that to use pointer aurethmetic you had to do this: (ptr)++ so that is why I had that as the end
of the for loop instead of using the i++ index as i could with a normal array.
ptr++ will advance the pointer by sizeof(T) bytes. And in an array, each element is sizeof(T) bytes, so it will advance to the next element.

Quote:

I can see now how wrong that is :p but it is good to make mistakes as you learn from them, and pointers are never simple.
Tsk, tsk. Pointers are so simple. People really never learn the proper basics.

So let us begin with this:
How would you iterate over a normal array, not using pointers?

Quote:

Originally Posted by KIBO
...Solution...

KIBO, are we handing out solutions again?
• 01-08-2009
Elkvis
it's interesting that I never knew until now that if you increment a pointer to a type T, it actually adds sizeof(T) to the pointer address. I always thought it would just add 1 to the address. I'm sure that this is something very elementary in C++, but the books I have don't really cover pointers very well, so I never learned that bit of info. definitely good to know.
• 01-08-2009
m37h0d
Quote:

Originally Posted by laserlight
Cheat: first write it out using index notation, then convert every a[n] to *(a+n).

On the other hand, you are probably not supposed to use a counter at all, so this cheat might be forbidden... but it can still help since then you just change the counter to a pointer, and instead of using some upper bound, use a pointer that is one past the end (cosmetic changes, as it were, since an optimising compiler will do something similiar anyway).

wouldn't it be

a[n] = *(a+n*sizeof(type)) ?