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

Greedy search - artificial intelligence, Greedy Search - artificial intelli...

Greedy Search - artificial intelligence: If we have a heuristic function for states, defined as above, so we can simply measure each state with respect to this measure and ord

Overcoming global issues in e-commerce, E-commerce advance tremendous chanc...

E-commerce advance tremendous chance by permitting industrialist to buy Materials at a low price internationally. Also they give companies the opportunity to sell to universal stor

Inventor, I am the inventor of the railway signaling device now operated by...

I am the inventor of the railway signaling device now operated by timer. I wish to move on to the next phase where the equipment will be operated by DTMF codes. The device is a si

2 way staircase switch-truth table logic equation & circuit, A staircase li...

A staircase light is controlled by two switches one at the top of the stairs and another at the bottom of stairs a. Make a truth table for this system. b. Write the lo

Double negation - artificial intelligence, Double Negation - Artificial int...

Double Negation - Artificial intelligence: Always parents are correcting their children for the use of double negatives, but we have to be very alert with them in natural langu

Show the classification of printers, Q. Show the Classification of Printers...

Q. Show the Classification of Printers? Printers can be classified on following bases: a) Impact: Impact printers print by impact of hammers on ribbon (for example Dot-Matri

Illustrate about fat structure, Q. Illustrate about FAT structure? DOS ...

Q. Illustrate about FAT structure? DOS file system maintains a table of pointers known as FAT (File allocation table) that comprises an array of 16-bit values. There is one ent

What is a decoder, What is a Decoder ? Ans. Decoder: - This dec...

What is a Decoder ? Ans. Decoder: - This decodes the information. The decoders contain n inputs 7 at the end maximum 2 n outputs since n bit no can decode max 2n infor

Sorting, Different sorting algorithm will be discussed in the lecutres. The...

Different sorting algorithm will be discussed in the lecutres. The task in this worksheet is to write a funtions based on the Quicksort algorithm. When sorting an array of objec

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