3

Some time ago I asked a question in which I was told (in the commits) that using the term "multi-dimensional array" for a construct like this:

int **a; 

Initialized like this:

a = malloc(n * sizeof(*a)); for (i = 0; i < n; ++ i) a[i] = malloc(m * sizeof(*a[i])); 

is misleading and that this is "only an emulation of " a multi-dimensional array. Not being a native English speaker, I am having a hard time figuring out what is the proper terminology. Unfortunately, the guy who complained about the terminology was of no help himself.

Sure enough, the term "emulation of multi-dimensional array" is too long to be actually used in text/conversation.

To sum up:

  • What is the proper terminology for the construct above (specifically in C, if that makes a difference)?

Side question:

  • Is this terminology language agnostic? If not, how is it called in C++ for example?

Note: I'd be happy if your answer is linked with a reference.
Edit: I understand the difference between this construct and int a[n][m];. That's not the question.

Update

The memory allocated is not necessarily regular. This construction is more precise:

a = malloc(n * sizeof(*a)); for (i = 0; i < n; ++ i) if (needed[i]) a[i] = malloc(m[i] * sizeof(*a[i])); else a[i] = NULL; 
2
  • Side note (and besides the terminology): You do understand what the difference between that and a bidimensional array of size nxm is, right? Commented Jul 20, 2012 at 4:20
  • @DavidRodríguez-dribeas, yes of course. Commented Jul 20, 2012 at 4:23

4 Answers 4

7

I've always heard these referred to as "jagged arrays" -- even if each sub-array is the same length, they can be of different lengths, hence the term "jagged." (In a true multi-dimensional array, each dimension has a fixed size, and in this case only the first dimension is truly fixed.)

Sign up to request clarification or add additional context in comments.

6 Comments

I've always heard them called "ragged arrays" - I suspect one is a corruption of the other.
I might be wrong, but I feel "jagged array" is a term made popular by the C# language as they had to distinguish between jagged arrays and true multi-dimensional arrays.
@Jesse: Same distinction here, and in Java, and in most other languages that have a "true" multi-dimensional array type.
@BenVoigt: I don't want to get too pedantic, but AFAIK, C, C++ and Java do not have "true" multi-dimensional arrays, while C# does. Read the first line here and this SO question.
@Jesse: int a[6][7]; is a "true" multidimensional array in C and C++ (there's only one array, one block, all elements stored contiguously). Which is very different from the thing in this question. You cannot pass int a[6][7]; to a function f(int** a), they are not compatible.
|
1

It's an "array of (pointers to) arrays", the term is understood across a variety of languages, and applies whether the lengths of the element arrays are equal (square AoAs) or not (jagged AoAs).

3 Comments

So in my original question (the one I linked), I'm supposed to say "I have an array of array of array of array of array of array"? Or "AoAoAoAoAoA"?
I'll probably accept your answer, but what do you think of this term: "A dynamic 6-dimensional array"? That is, adding dynamic to make people understand it's malloced.
@Shahbaz: You can certainly qualify your example with dynamic, as in "a dynamic array of arrays". But dynamic by itself is neither necessary nor sufficient to indicate this kind of array.
1

"Nested arrays", or "arrays of arrays [of ...]".

While I don't think it supports differing dimensions, Boost has an offering in this vague space - http://www.boost.org/libs/multi_array/doc/user.html - the design decisions and interface may be worth consideration even if you're implementing something new.

Comments

0

I would call the construct a "simulated multi-dimensional array" as they do in the c-faq. If you think that term is too long, you could say "simulated 2d array", etc. However, referring to int **a; as a multi-dimensional array should never be done, as it's type is a pointer to pointer to int. I think the distinction is that pointers can be used to construct arrays, but they will always remain as pointers. Also, it should be known that an array of pointers (int *a[10];) can also be used to construct jagged arrays, so the terminology would not be specifically referring to what you have.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.