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

Define the register length, Q. Define the Register Length? Register Le...

Q. Define the Register Length? Register Length: Asignificant characteristic related to registers is length of a register. Generally the length of a register is dependent on it

Why isdn handles data pertaining, ISDN handles data pertaining to? ISDN...

ISDN handles data pertaining to? ISDN handles data pertaining to all digital services.

Three firm capabilities that are performance in e-commerce, Three firm capa...

Three firm capabilities that are critical for superior firm performance in e-commerce are:- 1.  Information technology capability,  2.  Strategic flexibility, and  3.  Tr

Design a 3-bit counter using sequential logic, Q. Design a 3-bit counter us...

Q. Design a 3-bit counter using sequential logic with following counting sequence using JK- flip-flops which counts the sequence 0, 3, 2, 7, 5 and repeat.

Explain the instruction decode, Q. Explain the Instruction Decode? Ins...

Q. Explain the Instruction Decode? Instruction Decode: This phase is performed under control of Control Unit of computer. The Control Unit determines the operation which is t

Define step by step procedure in computer programming, Define Step by Step ...

Define Step by Step Procedure in Computer Programming? The Computer programming is the skill and art of creating a computer program a defined set of instructions in source code

What are the advantages of CMOS logic, What are the advantages of CMOS logi...

What are the advantages of CMOS logic Ans: Advantages of CMOS Logic: (i) The power dissipation is mini-mum of all the logic families (ii) LSI and VLSI are possible

Explain about the term business-to-customer, Explain about the term busines...

Explain about the term business-to-customer. B2C (business-to-customer): "Electronic commerce" is usually understood mostly as selling goods or services to people ("last

Manipulating logical expressions, Digital circuits also manipulate logicale...

Digital circuits also manipulate logicalexpressione.g. IF account is in credit THEN allow phoneto make calls.So a digital circuit must determine if somethingis TRUE or FALSE. Norma

Components of information super highway infrastructure, Explain the compone...

Explain the components of Information Super Highway Infrastructure. The Information Superhighway is more than the Internet. It is a sequence of components, having the collectio

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