# what is significance of two-diemnsional arrays in c?

Status
Not open for further replies.

#### embeddedlover

##### Full Member level 5
I always use one-dimensional array for all my applications. There are two-dimensional arrays and more.

What is the reason for having dimensional arrays?

One thing i am clear is whatever dimensional array you use the allocation is contiguous..

where does the need arise for multi dimensional arrays?

#### andre_luis

##### Super Moderator
Staff member
Depens on target application.
In image processing can represents the grayscale mapping of a picture.

+++

embeddedlover

### embeddedlover

Points: 2

#### srizbf

to store the image data(r,g,b)

embeddedlover

### embeddedlover

Points: 2

#### FvM

##### Super Moderator
Staff member
What is the reason for having dimensional arrays?
Strictly spoken, arrays in high level language are just a method to calculate memory addresses of data objects. If your problem involves multi-dimensional data objects, you'll want the compiler to do the work for you. Otherwise, you may use assembly code as well...

Points: 2

### syeda amna

Points: 2

#### embeddedlover

##### Full Member level 5
Strictly spoken, arrays in high level language are just a method to calculate memory addresses of data objects. If your problem involves multi-dimensional data objects, you'll want the compiler to do the work for you. Otherwise, you may use assembly code as well...

As you said if we want to reference an object of single dimensional array we will be doing it by taking starting address of that array....the same we will be doing for multi-dimensional array...how does compiler act differently for single and multi dimensional array?

#### andre_luis

##### Super Moderator
Staff member
Compiler doesn´t treat differently multidimensional arrays than unidimensional.
To the point of view of compiler, the only change is how the access to values stored into memory at those arrays are indexed.

+++

embeddedlover

### embeddedlover

Points: 2

#### embeddedlover

##### Full Member level 5
For suppose if i declare my array as a[2][2] instead of a[4]........
how can i access nth element?
how come i know which is nth element?
where as in the case of single dimensional array as a[n-1]

#### andre_luis

##### Super Moderator
Staff member
Maybe you didn´t realize that memory mamagement is performed automactily by compiler. There is not your task worry about that.
Multidimensional arrays are just a simpler way to you represent variables wich represents real-world models.

For instance, if I want to plot a grafic point in midle of a VGA ( 480x640 ) screen, I must set this color.

Example :
Code:
Value[240,320] = 0xFF ;

+++

Last edited:
embeddedlover

### embeddedlover

Points: 2

#### embeddedlover

##### Full Member level 5
Ya know that compiler takes care of everything...
but my doubt was how we can accesses a particular element in multidimensional array...

#### FvM

##### Super Moderator
Staff member
For suppose if i declare my array as a[2][2] instead of a[4]........
how can i access nth element?
The first point is, that you can access the array elements by simply writing indexed expressions, e.g. a[1][0] = 1; In most cases this will be enough and you don't have to care, how the objects are arranged in memory. There is however an implicite order set by the C standard, mapping the array elements to linear addresses:
a[0][0]
a[0][1]
a[1][0]
a[1][1]

embeddedlover

### embeddedlover

Points: 2

#### Sink0

##### Full Member level 6
Yea.. and if you need to get that data on how they are on memory, just create a pointer to it...

Something like...

Code:
 int *A_ptr = &A[0][0];

Now you can get the data in memory like A_ptr where it is the ith element in memory after A[0][0];

Cya

embeddedlover

### embeddedlover

Points: 2

#### embeddedlover

##### Full Member level 5
Now you can get the data in memory like A_ptr where it is the ith element in memory after A[0][0];

Is it the only way to get the required data by using index...

#### alexan_e

Code C - [expand]1
int *A_ptr = &A[0][0];

Or to assign the address of A[0] or A[0][0] or A[0][0][0] you can just write

Code C - [expand]1
int *A_ptr = &A;

the result is exactly the same

Alex

---------- Post added at 17:12 ---------- Previous post was at 16:03 ----------

Is it the only way to get the required data by using index...

Another way using pointer would be with a pointer to an array like this

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char t1,t2,t3,t4,t5,t6;  // six char variables
char my_array[2][3]={{'1','2','3'},{'4','5','6'}};   // a two dimensional array
char (*my_array_pointer)[3];  // pointer to an array of three char

void main(void)
{
my_array_pointer = my_array;  // assign the address to the pointer
t1= (*my_array_pointer)[0];         // this is the same as my_array[0][0]
t2 = (*my_array_pointer)[1];        // this is the same as my_array[0][1]
t3= (*my_array_pointer)[2];         // this is the same as my_array[0][2]
my_array_pointer++;                 // increment pointer
t4 = (*my_array_pointer)[0];        // this is the same as my_array[1][0]
t5 = (*my_array_pointer)[1];        // this is the same as my_array[1][1]
t6 = (*my_array_pointer)[2];        // this is the same as my_array[1][2]

}

Alex

embeddedlover

### embeddedlover

Points: 2

#### Sink0

##### Full Member level 6
Or to assign the address of A[0] or A[0][0] or A[0][0][0] you can just write

Code C - [expand]1
int *A_ptr = &A;

the result is exactly the same

Yea i know ... but to show that the pointer points to the fist element of the array i think is more intuitive to learn... but the result is the same....

#### alexan_e

I wrote it as an alternative way, not as a correction to what you said.
Yes your way is more straightforward and easier to the eye.

Alex

#### embeddedlover

##### Full Member level 5
Thank you Alex and Sink, that looks very convincing...

---------- Post added at 05:49 ---------- Previous post was at 05:48 ----------

i read somewhere that for two-dimensional need not to be allocated contiguougsly, but i firmly believe it is allocated contiguously, am i right?

#### navenmou

##### Full Member level 4
hiiii

Mainly multi--dimensional arrays are used in image processing for 2D,3D images...because the images are having various parameters like height,length,width.....

another thing for perform matrix applications in calculator........

#### Sink0

##### Full Member level 6
I might be wong but as far as i know, if it is not dinamically allocated it must be contiguously, but if you alloc an array of pointers they dont need to be contiguously.

Cya

#### FvM

##### Super Moderator
Staff member
I might be wong but as far as i know, if it is not dinamically allocated it must be contiguously, but if you alloc an array of pointers they dont need to be contiguously.
As previously explained, an array is mainly defining a scheme to calculate addresses of memory objects.
This implies, that array memory objects must be always contiguous.

Non-contiguous memory objects have to use other addressing schemes, e.g. liked lists.

#### embeddedlover

##### Full Member level 5
I might be wong but as far as i know, if it is not dinamically allocated it must be contiguously, but if you alloc an array of pointers they dont need to be contiguously.

Non-contiguous memory objects have to use other addressing schemes, e.g. liked lists.

In linked list each node is linked to next node by address of next node, so, allocation need not be contiguous. We alloc in linked list...
We use structures to represent and link each node.

But for an array it is contiguous. Even though we use alloc here, allocation is still contiguous as we are dynamically allocating for an array.

Status
Not open for further replies.