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

Hashing collision resolution techniques, Hashing collision resolution techn...

Hashing collision resolution techniques are a) Chaining, b) Bucket addressing

Explain about the network level in detail, Explain about the network level ...

Explain about the network level in detail. Network Level Firewall/Packet Filters: At the Network level firewalls operate upon the mechanism of filtering individual IP pa

Keyboard status word generator, Q.  Write short note on Interfacing Keyboar...

Q.  Write short note on Interfacing Keyboard, giving block diagram. Why do we need to introduce circuitry called Keyboard Status Word Generator?

Isoquants, what are the types of isoquants

what are the types of isoquants

Data communication, how CSMA protocol is improved through persistence metho...

how CSMA protocol is improved through persistence methods & collition detection

Transportation model, advantages and disadvantages of northwest corner meth...

advantages and disadvantages of northwest corner method and least cost method

Hypertext vs hypermedia, Hypertext vs Hypermedia     Hypertext is basic...

Hypertext vs Hypermedia     Hypertext is basically the similar as regular text - it can be stored, read, searched, or edited - with a significant exception: hypertext having co

What are assets in adobe premiere pro, Question: a) What is the meanin...

Question: a) What is the meaning of the term CTI in Adobe Premiere Pro? b) What are assets in Adobe Premiere Pro? c) In Adobe Premiere Pro, what is the principal diffe

Describe the various signalling techniques, Describe the various signalling...

Describe the various signalling techniques. Signaling systems link the variety of transmission systems, switching systems and subscriber elements in telecommunication network

Define flowchart, Define Flowchart. A  process  of  expressing  an  alg...

Define Flowchart. A  process  of  expressing  an  algorithm  by  a  collection  of  linked  geometric  shapes   containing explanations of the algorithm's steps.

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