Search code examples
cpointersmemoryreferencedereference

What does ** mean in C?


I have a sample C program I am trying to understand. Below is a function excerpt from the source code:

double** Make2DDoubleArray(int arraySizeX, int arraySizeY)
{
  double** theArray;
  theArray = (double**) malloc(arraySizeX*sizeof(double*));
  int i = 0;

  for (i = 0; i < arraySizeX; i++)
    theArray[i] = (double*) malloc(arraySizeY*sizeof(double));

  return theArray;
}

My question is what is the significance of the ** in the return type. I know that the * is generally used as a pointer. I know that it may also be used to dereference the pointer.

This makes me think that double** is a double value because it is essentially the dereferencing of a reference. Is my thinking correct? If not, can someone please explain the use of ** in this example?


Solution

  • In this case, double means a variable of type double.

    double* means a pointer to a double variable.

    double** means a pointer to a pointer to a double variable.

    In the case of the function you posted, it is used to create a sort of two-dimensional array of doubles. That is, a pointer to an array of double pointers, and each of those pointers points to an array of pointers.