answersLogoWhite

0


Best Answer

Yes. More specifically, they can be used to represent a dynamic multi-dimensional array.

As most people know, for a one-dimensional dynamic array, you simply need a pointer to the first element in the array, where each element contains an object of the same type as the pointer. The pointer can be passed around functions just as if it were a static array, the only difference being the requirement to pass the upper bound of the array as well as the array itself.

For a two-dimensional array, you need a pointer-to-pointer which points to the first element of a one-dimensional pointer array, where each pointer in that array points to a one-dimensional array of objects. The pointer-to-pointer must be the same type as the pointers and the objects.

For a three-dimensional array you need a pointer-to-pointer-to-pointer. And so on. Each additional dimension simply adds a new level of indirection, and a new level of one-dimensional pointer arrays.

Of course multi-dimensional arrays are only useful if every dimension is fully utilised and doesn't require too much in the way of resizing. If that is not the case, then you may get more efficient memory consumption from a vector of vectors (of vectors), which allows dynamic resizing at every level without having to copy existing elements. The downside is you lose the random access provided by the array.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

8y ago

Suppose we want a 3x4x5 array of integers. First we allocate three arrays, one for each dimension:

int* p = malloc (3*4*5*sizeof(int));

int** pp = malloc (3*4*sizeof(int*));

int*** ppp = malloc (3*sizeof(int**));

Note that each array has an extra level of indirection. This is important!

The array pointed to by p is the actual array containing all the data, however it is one-dimensional. It has a length of 3x4x5=60 integers. The second array is also one dimensional and it will be used to hold 3x4=12 pointers. Each of these pointers will hold the start address of a group of 5 integers from the first array. The final array is also one dimensional and it will be used to hold 3 pointers. Each of those pointers will hold the start address of a group of 4 elements from the second array.

Before we can access the data array multi-dimensionally, we need to initialise the pointer arrays:

for (int index=0; index<3; ++index) {

ppp[index]=&pp[index*4];

}

for (int index=0; index<12; ++index) {

pp[index]=&p[index*5];

}

Now we can use the data array. Let's initialise the elements with the values 1 through 60:

int val=1;

for (int x=0; x<3; ++x) {

for (int y=0; y<4; ++y) {

for (int z=0; z<5; ++z) {

ppp[x][y][z] = val++;

}

}

}

Note that we access the array through ppp. We can also access the array through pp and p, however ppp is the only one that allows us to access the array using three-dimensional suffix notation.

Now let's print the elements. The simplest way to visualise a three-dimensional array is to imagine that it's really a one-dimensional array containing independent two-dimensional arrays. We can then print each of those two-dimensional arrays as a separate table:

for (int x=0; x<3; ++x) {

printf ("Table: %d\n", x);

for (int y=0; y<4; ++y) {

for (int z=0; z<5; ++z) {

printf ("%d\t", ppp[x][y][z]);

}

printf ("\n");

}

printf ("\n");

}

When we're finished with our arrays, we must clean up. It's always best to release resources in the reverse order they were allocated:

free (ppp);

free (pp);

free (p);

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How is a multi-dimensional array defined in terms of a pointer?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the difference between multidimensional and dimensional scaling?

The difference between multidimensional and dimensional scaling is in terms of relationship between physical characteristic and dimension. In the case of multidimensional scaling, each dimension can be connected to 2 or more physical characteristics, unlike dimensional scaling..


What is the use of pointer in c?

A double pointer in C or C++ ... int ** ppi; ... simply means that ppi is a pointer that points to a pointer that points to an int. When defining function-parameters, another way of declaring this is ... int * ppi[]; ... which means that ppi is a pointer to an array of pointers that each point to an int, which happens to have the exact same meaning, but it is more telling in terms of what the usefulness of such a double pointer might have. Think of main() ... int main (int agrc, char ** argv); int main (int argc, char * argv[]); ... the two forms have exactly the same meaning, but the second form more clearly says what the design paradigm is, that argv is a pointer to an array of pointers that each point to an array of char, i.e. the arguments of the program's invocation.


What shoul you do when there is a leakage of space in terms of array?

If you have a memory leak, you should find it and fix it. It is a bug. It does not matter if it is an array or not. It is still a bug, and it needs to be fixed.


What is a recursive rule?

It is a term for sequences in which a finite number of terms are defined explicitly and then all subsequent terms are defined by the preceding terms. The best known example is probably the Fibonacci sequence in which the first two terms are defined explicitly and after that the definition is recursive: x1 = 1 x2 = 1 xn = xn-1 + xn-2 for n = 3, 4, ...


How do you create a two dimentional array?

A one-dimensional array is an array where each element in the array points to a specific value of the type specified by the array (all values must be of the same type). For example, we can store integer values in an integer array, character values in a character array and strings in a string array. Multi-dimensional arrays are implemented as one-dimensional arrays where every element is itself a one-dimensional array, for as many dimensions as required. The overall size of any array (in elements) is the product of all its dimensions, thus a two-dimensional array of 4x5 elements has 20 elements in total, divided into 4 arrays of 5 elements each. However, because all the elements are allocate contiguously, any multi-dimensional array can be treated as if it were one-dimensional. Note that every element of an array must be exactly the same length, even when that element is another array. The most common type of array we use is a pointer array (an array of pointer elements). Given that a non-null pointer does not store any size information (the number of elements being referred to), we typically use null-terminated pointer arrays, where a null pointer denotes the end of the array being referred to. This makes it possible to implement "jagged" or "irregular" multi-dimensional arrays, where each dimension can be a different length. An array of variable-length strings is an example of a jagged array, such that each element points to a null-terminated character array.

Related questions

What does mode mean in a line plot?

The mode is defined as the term which is repeated for the highest number of times in an array or a sequence. If many terms are repeated for a similar number of times and highest, that array has more than one value for mode.


How are undefined terms and defined terms relate to each other?

"Defined items" are defined in terms of "undefined terms".


What is the difference between multidimensional and dimensional scaling?

The difference between multidimensional and dimensional scaling is in terms of relationship between physical characteristic and dimension. In the case of multidimensional scaling, each dimension can be connected to 2 or more physical characteristics, unlike dimensional scaling..


How are defined terms and undefined terms relate to each other?

"Defined items" are defined in terms of "undefined terms".


What basis are heats of formation and combustion defined?

Combustion in defined in terms of 1 mole of reactant, formation is defined in terms of 1 mole of product


What is the sum of the first 10 terms?

The answer depends on how the terms and defined.


What are all the defined terms?

All capitalized terms not otherwise defined herein shall have the meaning ascribed to same in the Agreement.


Are most mathematical terms defined?

No. For example, division by 0 is not defined.


What is the use of pointer in c?

A double pointer in C or C++ ... int ** ppi; ... simply means that ppi is a pointer that points to a pointer that points to an int. When defining function-parameters, another way of declaring this is ... int * ppi[]; ... which means that ppi is a pointer to an array of pointers that each point to an int, which happens to have the exact same meaning, but it is more telling in terms of what the usefulness of such a double pointer might have. Think of main() ... int main (int agrc, char ** argv); int main (int argc, char * argv[]); ... the two forms have exactly the same meaning, but the second form more clearly says what the design paradigm is, that argv is a pointer to an array of pointers that each point to an array of char, i.e. the arguments of the program's invocation.


What word is defined by the description of something unfamiliar using familiar terms?

"Analogy" is defined as the description of something unfamiliar using familiar terms.


What shoul you do when there is a leakage of space in terms of array?

If you have a memory leak, you should find it and fix it. It is a bug. It does not matter if it is an array or not. It is still a bug, and it needs to be fixed.


How is an index use when coding arrays?

An array is a single block of contiguous memory addresses divided into one or more units known as elements. The elements are implicitly indexed such that an array of nelements has a valid range of indices in the range 0 to n-1, inclusive. This is what we call a zero-based index and is the default for all built-in arrays in C.All arrays allocated locally or statically are named arrays while all arrays allocated on the heap are anonymous arrays (you cannot name something that only exists at runtime) thus we must use a pointer variable (which may be named or anonymous depending on where it is allocated) to keep track of the start address. However, unlike a named variable where the name implicitly refers to the variable's value, an array name implicitly refers to the start address; it is a reference (an alias for an address) not a variable. The upshot is that we don't need to differentiate between a named array and an anonymous array; we can implicitly refer to both by their start address and they both work in exactly the same way. As a result, pointers and arrays are closely related.In particular, the built-in array subscript operator []works exactly the same way regardless of whether we are referring to a named array or to an anonymous array via a pointer variable.Subscripting a C-style built-in array is defined in terms of the pointer operations + (increment) and * (dereference) such that for every built-in array a and integer j within the range of a, we find the following equivalences hold true:a[j] j[a]Note that the + (increment) operator when applied to a memory address (whether represented by a pointer or a reference) increments the address in terms of the address type. That is, for an array of type T we would refer to it using a pointer or reference of type T* and the + operator will increment the address in units of sizeof (T) bytes. This is all done behind the scenes and is entirely automatic; we need only think in terms of how many elements we wish to increment by.Given the above equivalences, we find the following equivalences also hold true:a == a+0 = &a[0]That is, a is a reference to the first (and only the first) element in the array. Note that the & operator is necessary because a reference is implicitly an address but a[0] is implicitly a value, therefore we need to explicitly take the address of that value.It therefore follows that following equivalences must also hold true as well:*a = *(a+0) == a[0]In other words, a[0] implicitly dereferences the value that is offset 0 elements from the address referred to by a.[Note that these equivalences are fairly low-level and do not hold for C++ standard-library containers such as std::vectorand std::array].The reason these equivalences work is simply because the array subscript operator is nothing more than eye-candy for the underlying pointer arithmetic. When we write a[j] the compiler will translate this just as if we'd actually written *(a+j). Given that a+j == j+a (built-in addition is always a transitive operation) it should now be less of a surprise to find that a[j] == j[a].Here's a technical example to prove it really works:char c[] = {"abcde"};assert (c[3]==3[c]); // e.g., ('d' == 'd')