Pointers

I am trying to write a program and I have the following code:

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
30
31
32
33
34
35
36
37
38
39
40
41

#include <iostream>
using namespace std;


void readSizeAndAllocateArray(int ** ptr, int *size, int **maxIdx, int **minIdx)   
{

	*ptr = new int[*size];  //fill the memory location for the array subscripts
    *maxIdx = *(ptr + *size);
    *minIdx = *ptr -1;
    cout << *maxIdx << endl;
    cout << &maxIdx << endl;
    cout << maxIdx << endl;

}

int main()
{
	int size = 0;

	int * sPtr = NULL;
	int * minIdx = NULL;
	int * maxIdx = NULL;

	cout << "Enter the size of the array: ";
	cin >> size;  //fills the memory location for the size

	readSizeAndAllocateArray(&sPtr, &size, &minIdx, &maxIdx); //since you have a pointer to a pointer you need the &
	for (int i=0; i<size; i++)
	{
			sPtr[i] = i;
			cout << sPtr[i] << endl;
			maxIdx[i] = i;
			cout << maxIdx[i] << endl;
	}

	return 0;
}




the problem is that when I cout the max index all I see is the address of the operator. Can someone show me I am doing wrong? Or "point" me in the write direction
This looks wrong:
1
2
*maxIdx = *(ptr + *size);
*minIdx = *ptr -1;

After this the *maxIdx will point to the element after the last element and *minIdx will point to the element before the first element.
When I take your suggestion of removing the -1 and the *size I still get the address locations. I am doing research to see if I happened to miss anything, and trying to read up on pointers again.
Topic archived. No new replies allowed.