2

Can a pointer to a multidimensional array in C be written simply as:

double *array;

Where arrayis an n by n matrix?

And then can I access the element in row i, column j, by array[i][j]?

Or is there such thing as a double pointer?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
user3746892
  • 67
  • 2
  • 8
  • There *is* such a thing as a `**` , but it's not the same as a `[][]` at all. – EOF Feb 05 '15 at 18:15
  • You would have to declare as `array[rows * columns]` (or allocate memory for) and index by `[row * columns + col]` – Weather Vane Feb 05 '15 at 18:17
  • Pointer is a pointer. Pointer to anything in C is represented in the same way, so if you want, you can cast them. But it is strongly not recommended. Multidimensional arrays are usually represented as pointers to other pointers. – Eugene Sh. Feb 05 '15 at 18:17

4 Answers4

5

Can a pointer to a multidimensional array in C be written simply as:

double *array;

Yes.

Say you have M x N array. You can use:

double* array = malloc(M*N*sizeof(*array));

Then, you can access the elements by using:

size_t getArrayIndex(size_t m, size_t n, size_t M)
{
    return (m*M+n);
}

double getArrayElement(double* array, size_t m, size_t n, size_t M)
{
    return array[getArrayIndex(m, n, M)];
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    @user3746892 See this question http://stackoverflow.com/questions/14015556/how-to-map-the-indexes-of-a-matrix-to-a-1-dimensional-array-c, although it is of c ++ the answer can help you better understand this answer – Cyberguille Feb 05 '15 at 18:50
2

double * is a pointer to a double. It's also pointer to an element of an array of double.

To do what you want:

double bla[23][42];          // array of arrays, type is double [23][42]
double (*p)[23][42] = &bla;  // pointer to an object of type double [23][42]
ouah
  • 142,963
  • 15
  • 272
  • 331
0

to make a pointer to a multidimensional array array[i][j] where i is number of rows and j is number columns you can do double (*ptr)[j] which says that ptr is a pointer to double array of j elements. then you can assign ptr=array;

lychee
  • 1,771
  • 3
  • 21
  • 31
-1

Can a pointer to a multidimensional array in C be written simply as:

double *array;

That could be something that is used to represent an array, when handing it over to a function! A C array is then only represented by a pointer to its first element. How these elements are organized internally is up to you; making sure you don't use indices that are actually outside the array, too.

Or is there such thing as a double pointer?

Of course; double ***ndim_array is perfectly valid. However, you could also use multidimensional arrays like double [][][].

(Thanks to all the contributors in comments!)

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • @EOF: yep, you're right of course with the "a pointer is not an array"; I think I'm pretty correct on `double[][]` actually having to be read as "an array of arrays", ie. a pointer to a pointer. – Marcus Müller Feb 05 '15 at 18:25
  • No. It's a multidimensional array. They really, really are different. – EOF Feb 05 '15 at 18:26
  • 1
    @MarcusMüller: No, you've got it wrong. An array is not a pointer, and an array of arrays is not a pointer to a pointer. Check out the FAQ: http://c-faq.com/aryptr/index.html – Thomas Padron-McCarthy Feb 05 '15 at 18:28
  • @ThomasPadron-McCarthy: that FAQ has a nice explanation! Note, though, that I've used the verb *represent* deliberately. Ok, you've got me on the multidimensional array thing, indeed, though, so I'm removing this from my answer. Thanks, guys! BTW; if you can, you're absolutely welcome to correct my answer here. – Marcus Müller Feb 05 '15 at 18:45