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

Over fitting considerations - artificial intelligence, Over fitting Conside...

Over fitting Considerations - artificial intelligence Left  unexamined ,  back  propagation  in  multi-layer  networks  may  be very susceptible  to over fitting itself to the

State about sixth generation electronic computers, Sixth Generation (1990 -...

Sixth Generation (1990 - ) This  generation  begun  with  many  gains  in  parallel  computing,  both  in  hardware area and in improved understanding of how to build up algori

What are the types of smart cards used in e-commerce, What are the types of...

What are the types of Smart cards used in e-commerce? Generally there are 2 types of smart cards. Memory smart cards, which can be sighted as minuscule removable read/ write

Describe the architecture of electronic data exchange, Describe the Electro...

Describe the Electronic Data Exchange (EDI) architecture in brief. Electronic Data Interchange (EDI) may be generally easy to understand as the replacement of paper-based purch

What is the semiconductor memory chip, What is the Semiconductor memory chi...

What is the Semiconductor memory chip A semiconductor memory chip comprises a large number of cells organized into an array and the logic necessary to access any array in the c

What is control panel, A system utility that comes with Windows that permit...

A system utility that comes with Windows that permits the use to change a variety of dissimilar Windows and system settings.

Search mechanisms in prolog, Search mechanisms in Prolog: Here we can ...

Search mechanisms in Prolog: Here we can needs this simple Prolog program to describe how Prolog searches as:president(X) :- first_name(X, georgedubya), second_name(X, bush).

Execute the command in linux, Now that the user's command has been parsed i...

Now that the user's command has been parsed into an array of char*, we can pass this to the OS to execute the command. To execute the command, use the execvp() function from unis

Describe loading disk operating system, Q. Describe Loading Disk Operating ...

Q. Describe Loading Disk Operating System? Different version of DOS, such as 2.0, 3.0, 3.2, 4.0, 5.0, 6.0, 6.2, 6.22, 7.0, 7.2 are present. The newer versions have additional

DSP, Assignment of Analog to digital Ssignal Processing

Assignment of Analog to digital Ssignal Processing

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