int number[5] = {1, 2, 3, 4, 5};
int odd[3]; //array of 3 odd numbers;
int *xPtr = odd; //xPtr now points to the first element of odd
for(int i = 0; i <= 5; i+=2) //Start i at 1, increment by 2 for odd numbers
*xPtr++ = numbers[i]; //Dereference xPtr to access the value pointed to by it.
//set it equal to numbers[i]. (i will point to the first, third, and fifth element.
//post-increment offsets xPtr by one, so it points to the next location in odd.
//output odd to see if we did it right:
for(int i = 0; i < 3; ++i)
std::cout << odd[i] << std::endl;
However, when i run the program, it outputs the even integers. If i change the initial value of i to 0, it outputs odd integers... Do you have any idea why?
Oh, woops. I made a silly mistake. You're right, the initial value should be zero.
Arrays start at zero and count up. so number[0] would be 1, numbers[2] would be 3.
I edited my post to reflect that error.