0

Following scenario:

int** pToPField = new int* [8];

Now I have a pointer pointing to a field of pointers. Each pointing to an int, right?

Now I want to assign the first two int fields like:

*(*(pToPField)) = 1;
*(*(pToPField + 1)) = 2;

Or like:

*(pToPField[0]) = 1;
*(pToPfield[1]) = 2;

The error is always a core dump. Is my syntax that wrong? I tried to figure it out from the first answer to this question: How do I use arrays in C++? I had no luck.

Regards

Community
  • 1
  • 1
jeykey
  • 9
  • 3

1 Answers1

3

Your syntax is correct, but you are not allocating any space for the integers being pointed at by the pointers in the array, so you are accessing random memory, which you don't have access to, resulting in a segmentation fault.

Example:

pToPField[0] = new int;
pToPField[1] = new int;
// and so on...

Consider using std::vector instead.

std::vector<int> pToPField(8);

pToPField[0] = 1;
pToPField[1] = 2;
// and so on ...
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Rakete1111
  • 47,013
  • 16
  • 123
  • 162