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.

Help with C tables and pointers

Status
Not open for further replies.

m_t_blind

Newbie level 5
Joined
Jul 31, 2005
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,376
When we have a 1-dimension table, for example
char m[2],
the name of the table m is a char type pointer to the first element of the table.ok.
When we have a multi-dimension table, example
char m2[10][2],
i found that the name is't a valid pointer, and i use &m[0][0] for this.
(This on the IAR compiler for msp)

1.My first question is that this is right for general in C, or simply apears on the specific compiler ?

2.And my next question is (for the table m2):
The pointer *(&m2[0][0] + 10) points the m2[0][2]?
If no why?
 

sinu_gowde

Full Member level 2
Joined
Nov 10, 2005
Messages
137
Helped
32
Reputation
64
Reaction score
12
Trophy points
1,298
Activity points
3,490
Hi m_t_blind,
checkout my explanation:

char m2[10][2]
Let's take the underlined part to be the "name" of an array. Then prepending the char and appending the [2] we have an array of 10 characters. But, the name m2[10] is itself an array indicating that there are 10 elements each being an array of 2 characters. Hence we have an array of 10 arrays of 2 characters each.

*(&m2[0][0] + 10) points the m2[0][2] is wrong.
it points to m2[5][0].
this is becuase, when you say &m2[0][0] actually it takes the address of the first element. & when you ADD 10 to this, it will add 10*sizeof(char) to that address. So finally it pionts to m2[5][0].

(sorry for that mistake.. thanks silvio)

cheers...
 

    m_t_blind

    Points: 2
    Helpful Answer Positive Rating

silvio

Advanced Member level 3
Joined
Dec 31, 2001
Messages
716
Helped
105
Reputation
210
Reaction score
16
Trophy points
1,298
Location
Romania
Activity points
11,471
m_t_blind said:
1.My first question is that this is right for general in C, or simply apears on the specific compiler ?

It's ANSI C.

sinu_gowde said:
it will add 10*sizeof(int) to that address

it will add 10*sizeof(char) to that address

sinu_gowde said:
So finally it points to m2[5][0].

Correct.

Two_dimmensional arrays are stored in a row-column matrix, where the left index indicates the row and the right indicates the column. This means that the rightmost index changes faster than the leftmost when accessing the elements in the array in the order in which are actually stored in memory.
 

    m_t_blind

    Points: 2
    Helpful Answer Positive Rating

comp_freak

Member level 1
Joined
Jan 19, 2006
Messages
40
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
1,687
I think you need to understand the basic of array and pointers... when u say m[10].. not matter if its declared char or int or double.. you are just allocationg a chunk of memory and which u have name.. and m[0] will be the first element of array.. and whenever u need to declare and pointer they are explicitly declare such as int * point, etc..

as far i knw c... just study the basic of C array and pointers.. u will be fine.. pointer is very powerful u can crash system with it !:idea:


if u need online c book i arleady upload it on rapidshare.. here is the link


h**p://rapidshare.de/files/12360722/C-K_R-Material.pdf.html
 

arunragavan

Advanced Member level 1
Joined
Jul 1, 2004
Messages
417
Helped
30
Reputation
60
Reaction score
9
Trophy points
1,298
Location
India
Activity points
5,030
yeap.. that ur sayin is rite.. there is no concept of 2D array in C.. what is it is you can have multi dimensional array and more over they way in which elements are arranged in the memory is in the form of a single slot.. as you guys know.. (under turbo C) integer takes up 2 bytes and character takes 1 byte..each memory slot is allocated with 2 bytes of memory / 1 byte for character and *(&a[0][0]+10) is equal to *( first memory allocation + 10 places which is 1 byte each) so finally de-references it and prints it..

with regards,
arun
 

m_t_blind

Newbie level 5
Joined
Jul 31, 2005
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,376
ok thank you all, ( and sorry for the mistake of m2[0][2] whitch doesn't exist, i mean m2[0][1] )
 

coshkun

Full Member level 2
Joined
Aug 27, 2005
Messages
126
Helped
18
Reputation
36
Reaction score
10
Trophy points
1,298
Activity points
2,214
I want to create a char array and want to call messages like this:
message[0]---> for first message
messsage[1] ---> second message and so on...
How can i do that?
 

comp_freak

Member level 1
Joined
Jan 19, 2006
Messages
40
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
1,687
You want to creat array of pointers which point at Array of char... u can see by follwoing diagrams...
 

coshkun

Full Member level 2
Joined
Aug 27, 2005
Messages
126
Helped
18
Reputation
36
Reaction score
10
Trophy points
1,298
Activity points
2,214
Can you write an example code please?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top