0

I'm trying to understand an example of a graph represented by an adjacency matrix. I'm having trouble with the following code:

private:
      bool** adjacencyMatrix;
      int vertexCount;
public:
      Graph(int vertexCount) {
            this->vertexCount = vertexCount;
            adjacencyMatrix = new bool*[vertexCount];

            for (int i = 0; i < vertexCount; i++) {
               adjacencyMatrix[i] = new bool[vertexCount];

                  for (int j = 0; j < vertexCount; j++)
                        adjacencyMatrix[i][j] = false;
            }
      } 

I understand that adjacencyMatrix now points to an array of pointers to bool, But I can't understand why there are two ** in the declaration of adjacencyMatrix.

I know I'm missing something very rudimentary but I just can't understand it. I've searched the Internet and couldn't find anything.

I just came here because there are so many knowledgeable and helpful people!

Marcus Kim
  • 283
  • 2
  • 12
  • Look at this like It is an array of arrays. – 273K Mar 31 '18 at 04:39
  • Possible duplicate of [Why should I use a pointer rather than the object itself?](https://stackoverflow.com/questions/22146094/why-should-i-use-a-pointer-rather-than-the-object-itself) –  Mar 31 '18 at 04:39
  • Just like you said, it points to pointers. Therefore, it is a double pointer. – Ben Voigt Mar 31 '18 at 04:44
  • 1
    I don't know why anyone would do it like that. It would be much better to use `vector>`. (`char` rather than `bool` because `vector` is optimized for space at the expense of speed). – Sid S Mar 31 '18 at 05:02

0 Answers0