Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

what is significance of two-diemnsional arrays in c?

Status
Not open for further replies.

embeddedlover

Full Member level 5
Full Member level 5
Joined
Aug 10, 2007
Messages
277
Helped
46
Reputation
92
Reaction score
38
Trophy points
1,308
Activity points
3,155
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
Advanced Member level 7
Joined
Nov 7, 2006
Messages
9,502
Helped
1,186
Reputation
2,391
Reaction score
1,186
Trophy points
1,403
Location
Brazil
Activity points
55,124
Depens on target application.
In image processing can represents the grayscale mapping of a picture.

+++
 

FvM

Super Moderator
Staff member
Advanced Member level 7
Joined
Jan 22, 2008
Messages
50,988
Helped
14,630
Reputation
29,536
Reaction score
13,739
Trophy points
1,393
Location
Bochum, Germany
Activity points
291,730
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...
 

embeddedlover

Full Member level 5
Full Member level 5
Joined
Aug 10, 2007
Messages
277
Helped
46
Reputation
92
Reaction score
38
Trophy points
1,308
Activity points
3,155
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
Advanced Member level 7
Joined
Nov 7, 2006
Messages
9,502
Helped
1,186
Reputation
2,391
Reaction score
1,186
Trophy points
1,403
Location
Brazil
Activity points
55,124
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

Full Member level 5
Full Member level 5
Joined
Aug 10, 2007
Messages
277
Helped
46
Reputation
92
Reaction score
38
Trophy points
1,308
Activity points
3,155
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
Advanced Member level 7
Joined
Nov 7, 2006
Messages
9,502
Helped
1,186
Reputation
2,391
Reaction score
1,186
Trophy points
1,403
Location
Brazil
Activity points
55,124
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

Full Member level 5
Full Member level 5
Joined
Aug 10, 2007
Messages
277
Helped
46
Reputation
92
Reaction score
38
Trophy points
1,308
Activity points
3,155
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
Advanced Member level 7
Joined
Jan 22, 2008
Messages
50,988
Helped
14,630
Reputation
29,536
Reaction score
13,739
Trophy points
1,393
Location
Bochum, Germany
Activity points
291,730
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]
 

Sink0

Full Member level 6
Full Member level 6
Joined
Nov 25, 2009
Messages
390
Helped
37
Reputation
74
Reaction score
30
Trophy points
1,308
Location
Sao Paulo, Brazil
Activity points
4,186
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

Full Member level 5
Full Member level 5
Joined
Aug 10, 2007
Messages
277
Helped
46
Reputation
92
Reaction score
38
Trophy points
1,308
Activity points
3,155
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

Administrator
Advanced Member level 7
Joined
Mar 16, 2008
Messages
11,888
Helped
2,021
Reputation
4,158
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,371

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
 

Sink0

Full Member level 6
Full Member level 6
Joined
Nov 25, 2009
Messages
390
Helped
37
Reputation
74
Reaction score
30
Trophy points
1,308
Location
Sao Paulo, Brazil
Activity points
4,186
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

Administrator
Advanced Member level 7
Joined
Mar 16, 2008
Messages
11,888
Helped
2,021
Reputation
4,158
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,371
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
Full Member level 5
Joined
Aug 10, 2007
Messages
277
Helped
46
Reputation
92
Reaction score
38
Trophy points
1,308
Activity points
3,155
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
Full Member level 4
Joined
Sep 25, 2010
Messages
228
Helped
49
Reputation
98
Reaction score
46
Trophy points
1,318
Location
Bangalore, India
Activity points
2,588
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
Full Member level 6
Joined
Nov 25, 2009
Messages
390
Helped
37
Reputation
74
Reaction score
30
Trophy points
1,308
Location
Sao Paulo, Brazil
Activity points
4,186
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
Advanced Member level 7
Joined
Jan 22, 2008
Messages
50,988
Helped
14,630
Reputation
29,536
Reaction score
13,739
Trophy points
1,393
Location
Bochum, Germany
Activity points
291,730
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.
e.g.: address_n_m = address_0_0 + n*n_incr + m*m_incr
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
Full Member level 5
Joined
Aug 10, 2007
Messages
277
Helped
46
Reputation
92
Reaction score
38
Trophy points
1,308
Activity points
3,155
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.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top