Pointers

This is a small segment of my code. I have created two vectors. I am successful in displaying both of them but when I try to subtract one from the other, it gives me an error. The error says EXC_BAD_ACCESS (code=1, address=0x0). I am positive it has something to do with pointers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  for (counter = Numbers.begin(); counter != Numbers.end(); ++counter)
    {
        cout << *counter;
        if (counter != Numbers.end() - 1)
            cout << ", ";
    }
    
    cout << endl << "Difference Vector " << endl;
    
    for (int a = 0; a < N; a++)
    {
        DiffVect[a] = Numbers[a] - counter[a];   // <------ This line.
        cout << DiffVect[a];
        if (a != N-1)
            cout << ", ";
    }
when the loop on lines 1--6 ends, `counter' would be `Numbers.end()' (and invalid iterator)
then in line 12 you are trying to dereference it.


> I have created two vectors.
I suppose that you refer to `Numbers' and `DiffVect'.
But you never show how you create them, and likely `DiffVect' is uninitialized.

> I am successful in displaying both of them
you only succeed on displaying `Numbers'

> but when I try to subtract one from the other
¿what do you think that `counter' is?
Topic archived. No new replies allowed.