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.