How is a multidimensional array defined pointer, Computer Engineering

Assignment Help:

How is a multidimensional array defined in terms of a pointer to a collection of contiguous arrays of lower dimensionality ?

C does not have true multidimensional arrays. However, because of the generality of C's type system, you can have arrays of arrays.

Here is a two-dimensional array, although the techniques can be extended to three or more dimensions.

int a2[5][7];

Formally, a2 is an array of 5 elements, each of which is an array of 7 ints. Thus a two dimensional array, for example, is actually a collection of one dimensional arrays. Therefore, we can define a two-dimensional array as a pointer to a group of contiguous one dimensional arrays. A two dimensional array declaration can be written as

Int (*a2)[7];

We need parenthesis (*a2) since [] have a higher precedence than * So:

int (*a2)[7]; declares a pointer to an array of 7 ints.

int *a[7]; declares an array of 7 pointers to ints.

This concept can be generalized to higher dimensional arrays; that is

data - type (*variable name) [expression1][expression2].....[expression n];

If we don't know how many columns the array will have, we'll clearly allocate memory for each row (as many columns wide as we like) by calling malloc, and each row will therefore be represented by a pointer. To keep track of those pointers, we want to simulate an array of pointers, but we don't know how many rows there will be, either, so we'll have to simulate that array (of pointers) with another pointer, and this will be a pointer to a pointer.

This is best illustrated with an example:

#include

int **array;

array = malloc(nrows * sizeof(int *));

if(array == NULL)

{

fprintf(stderr, "out of memory\n");

exit or return

}

For (i = 0; i < nrows; i++)

{

array[i] = malloc(ncolumns * sizeof(int));

if(array[i] == NULL)

{

fprintf(stderr, "out of memory\n");

exit or return

}

 


Related Discussions:- How is a multidimensional array defined pointer

Fail-first - artificial intelligence, Fail-first - artificial intelligence:...

Fail-first - artificial intelligence: Alternatively one such dynamic ordering procedure is known like "fail-first forward checking". In fact the idea is to take advantage of i

First order predicate logic - artificial intelligence, First Order Predicat...

First Order Predicate Logic : This is a more expressive logic because it is mostly builds on propositional logic by allowing us to needs as constants, variables, predicates,

What is link destruction, What is link destruction? Link destruction is...

What is link destruction? Link destruction is inverse of link creation. When a link is destroyed makes sure the associated objects accessible by other handles or intentionally

Function modules are also external subroutines, Function Modules are also e...

Function Modules are also external Subroutines. True.

Classify scheduler, Classify Scheduler. Scheduler is a kernel function ...

Classify Scheduler. Scheduler is a kernel function decide which method  be  thought to be implemented by the processor: the scheduler scans the list of processes in the ready s

Explain clearly the difference between j-cuts and l-cuts, Question: (a)...

Question: (a) Explain clearly the difference between J-cuts and L-Cuts. (b) What is the function of the rate stretch tool? (c) What's the difference between an Image Matt

Interconnection network in-array processing, Interconnection Network (IN): ...

Interconnection Network (IN):  IN performs data exchange between the PEs, manipulation functions and data routing. This IN is under the control of CU.

How do you turn off cookies for one page in your site, How do you turn off ...

How do you turn off cookies for one page in your site?  Use the Cookie. Discard Property which Gets or sets the discard flag set by the server. When true, this property initiat

How can we use ordered lists, Q. How can we use Ordered Lists? Lists ha...

Q. How can we use Ordered Lists? Lists having numbered items are termed as ordered lists. They are used when items in the list have a natural order. They can also be used when

What is input - output commands, Q. What is Input - Output commands? Th...

Q. What is Input - Output commands? There are four kinds of I/O commands which an I/O interface can receive when it's addressed by a processor: Control : This type of c

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd