answersLogoWhite

0

Working with two-dimensional arrays is really no different to working with a one-dimensional array. A two-dimensional array is really just a one-dimensional array where every element is a one-dimensional array.

int x[4];

Here, x is a one-dimensional array of 4 elements of type int.

int y[3][4];

Here, y is two-dimensional, however it's actually a one-dimensional array of 3 elements where each element is exactly the same type as x; an array of 4 elements of type int.

The biggest problem with multi-dimensional arrays is when we pass arrays into functions. An array implicitly decays to a pointer so we lose all information regarding the dimensions when we pass them to functions. Thus we have to pass the dimensions as separate arguments. However, a two-dimensional array does just decay to a point it decays to a pointer-to-pointer. That is, each dimension adds another level of indirection.

The following example demonstrates how to pass a multi-dimensional array to a function:

#include<stdio.h>

void print2d (int* a, unsigned rows, unsigned cols) {

for (unsigned r=0; r<rows; ++r) {

for (unsigned c=0; c<cols; ++c)

printf ("%d\t", a[r * cols + c]);

printf ("\n");

}

}

int main()

{

int x[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

print2d (&x[0][0], 3, 4);

return 0;

}

Note that we don't have this problem in C++ because we can use std::array and std::vector objects, which are much easier to work with and just as efficient as any built-in array.

User Avatar

Wiki User

8y ago

What else can I help you with?

Related Questions

What are the data type in c language?

Some of them are: 1. char, short, int, long, float, double 2. pointers to these 3. arrays of these 4. arrays of pointers 5. pointers to arrays ...


How will you initialize a 2-d array?

All arrays are one-dimensional. A two-dimensional array is simply a one-dimensional array of one-dimensional arrays: int a[2][3]; This is an array of 2 elements where each element is itself an array of 3 integers. In other words it is an array of 6 integers. The two dimensions simply allow us to split the array into two sub-arrays of 3 elements each.


What would happen to the perimeter of your circle if you double the dimension?

If you double the diameter, or the radius, then the perimeter will also double.


What is the final three dimension shape of a protein called?

Double Helix


How does the volume of a prism change if each dimension of the prism is doubled what does it double into?

If each dimension of a prism is doubled then the volume increases by a multiple of 8.


How does the volume of a prism change if each dimension of th prism is doubled what does it double in to?

If each dimension is doubled, the prism then haseight times the volume that it had before.


What happens to the perimeter of a rectangle when you double one of its dimensions?

The perimeter of a rectangle is given by (2L plus 2W). If you double either the width or length dimension, then it is four times the original dimension, such as (4L plus 2W) or (2L plus 4W).


How can I access my Adobe Photoshop Elements 5.0.2 software on my hard drive?

go to C:\Program Files\Adobe there you will find your elements folder , double click on it to open,if you need then right click on .exe file and choose send to Desktop(create shortcut)


Which of the following is the area of a square with side of 8 cm when the dimension is double?

If the side of 8 is double to 16 then its area is 16*16 = 256 square cm


Differentiate single dimensional array to double dimensional array?

A single dimensional array is an array of items. A two-dimensional array is an array of arrays of items.


What is the significance of the double colon symbol in programming languages?

In programming languages, the double colon symbol (::) is often used to denote scope resolution or to access elements within a namespace or class. It helps to organize and structure code by specifying the context in which a particular function or variable is defined.


How do you pass a 2D array in a functions?

if you were to call a function you would write it as: function(array[][], int pretend, double pretend2); arrays will always be passed by reference, not by value.