-2

I'm trying to use an array of pointers to store the addresses of some double values, which are also stored in an array.

My problem is, that I'm able to print the addresses but not the actual values, which they point to. I've come up with the following. When I try to add a star (cout << *ptrArr[i] << endl;), I get a error message that says, that it's not a pointer.

const int size = 3;
double var[size] = { 0.0, 0.1, 0.2};
int *ptrArr = new int[size];

for (int i = 0; i < size; i++) {
  ptrArr[i] = (int)&var[i]; // assign the address of integer.
}

for (int i = 0; i < size; i++) {
  cout << "Value of var[" << i << "] = ";
  cout << ptrArr[i] << endl;
}

delete ptrArr;
Kai
  • 57
  • 1
  • 10
  • 7
    Do yourself a favor, and get a [decent book on C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – StoryTeller - Unslander Monica Oct 02 '17 at 12:45
  • 1
    That's because `ptrArr[i]` is not a pointer, innit. `ptrArr + i` is a pointer. And what the deuce is `(int)&var[i]`? – Bathsheba Oct 02 '17 at 12:46
  • `ptrArr[i] = (int)&var[i];` what should that code be good for?? – user0042 Oct 02 '17 at 12:46
  • 4
    "_I'm trying to use an array of pointers_" There's no array of pointers in the code you show. One (`var`) is array of `double`s, while other (`ptrArr`) is a pointer to the start of the array of `int`s. – Algirdas Preidžius Oct 02 '17 at 12:46
  • Use std::vector, not new[]. –  Oct 02 '17 at 13:10
  • I think I get what you mean. And I already changed the int casting. But maybe I don't get it right: I got myself a book about c++ (Der c++ Programmierer by Ulrich Breymann). As I learned a pointer stores a value which is a address and an array holds the start address, right? But is there a way to store addresses in an array? My main aim is to create an array which contains the adresses of some double values. And for this I have to use the new/method. – Kai Oct 02 '17 at 13:24

1 Answers1

1

Did you want to get this? Use a double* array, to store the addresses:

const int size = 3;
double var[size] = {0.0, 0.1, 0.2};
double **ptrArr = new double*[size];

for (int i = 0; i < size; i++) {
    ptrArr[i] = &var[i]; // assign the address of the double.
}

for (int i = 0; i < size; i++) {
    cout << "Value of var[" << i << "] = ";
    cout << *ptrArr[i] << endl;

    cout << "Address of var[" << i << "] = ";
    cout << ptrArr[i] << endl;
}
Danilo Carrabino
  • 387
  • 6
  • 11
  • Thank you very much, this is exactly what I was looking for :)! Would you be so mind an explain, why I have to use double pointers? – Kai Oct 02 '17 at 13:28
  • Because actually you're storing an address pointing to a double, so avoiding any particular cast, you can simply declare it double*. If you want you can also cast to int* or void*, but in this particular case, because you're working with double, it's perfectly correct to consider them double*. – Danilo Carrabino Oct 02 '17 at 13:39
  • Ahh, got it!;) Thanks again for your help and advice. – Kai Oct 02 '17 at 13:42