Using pointers with an array

Im making a pointer that works with an array and im trying to get the output but it just gives me a big number and not the array element. when i ran it i got: 2130567168

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int names()
{
    int array[] = {10,15,23,12};
    int *pArray = &array[5];

    return *pArray;
}

int main()
{
    cout << names();
}
I'm surprised that it didn't crash on you. array[ 5 ] is not a valid element in an array with four elements. The proper elements are 0, 1, 2, and 3.
oh oops i forgot that, i changed that, i still get a weird number its just not as long.

1
2
3
4
5
6
7
8
9
10
11
12
13
int names()
{
    int array[4] = {10,15,23,12};
    int *pArray = &array[3];

    return *pArray;
}

int main()
{
    cout << names();
}


never mind i got it, thanks.
Last edited on
Topic archived. No new replies allowed.