this is a question for function parameters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// pointers as arguments:
#include <iostream>
using namespace std;

void increment_all (int* start, int* stop)
{
  int * current = start;
  while (current != stop) {
    ++(*current);  // increment value pointed
    ++current;     // increment pointer
  }
}

void print_all (const int* start, const int* stop)
{
  const int * current = start;
  while (current != stop) {
    cout << *current << '\n';
    ++current;     // increment pointer
  }
}

int main ()
{
  int numbers[] = {10,20,30};
  increment_all (numbers,numbers+3);
  print_all (numbers,numbers+3);
  return 0;
}

--------------------------------------------------------------------

I am trying to give it some change as experimental, and one of them is to put various form of pointers instead of "numbers" and "numbers+3"
What I did and didn't work was :

-----------------------------------------------------------------
1
2
3
int numbers[]={10,20,30};
int *stop; stop = numbers[2] // which is 30
increment all( numbers, stop)

-----------------------------------------------------------------
I also tried "numbers[2]" instead of "stop" and it didn't work too.
What are the rules for things to be in parameters?
Last edited on
numbers[2] is an int, not a pointer to an int.

If you want to stop at 30, then take the address of numbers[2]
 
int *stop = &numbers[2];


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Thank you for your kind lesson, AbastractionAnon !

for code and posting, both helps me a lot : )
Topic archived. No new replies allowed.