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?

3
  • There is such a thing as a ** , but it's not the same as a [][] at all. Commented Feb 5, 2015 at 18:15
  • You would have to declare as array[rows * columns] (or allocate memory for) and index by [row * columns + col] Commented Feb 5, 2015 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. Commented Feb 5, 2015 at 18:17

4 Answers 4

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)];
}
Sign up to request clarification or add additional context in comments.

1 Comment

@user3746892 See this question stackoverflow.com/questions/14015556/…, although it is of c ++ the answer can help you better understand this answer
3

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]

Comments

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;

Comments

0

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!)

4 Comments

@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.
No. It's a multidimensional array. They really, really are different.
@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: c-faq.com/aryptr/index.html
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.