[SOLVED] C program basic.. but can't understand

Status
Not open for further replies.

saur

Member level 2
Joined
Feb 23, 2012
Messages
45
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Visit site
Activity points
1,621
In a code that I've come across,

First they 've defined NUM_RX_BUF and NUM_TX_BUF as 4 and ETH_BUF_SIZE as 1536.

Then I've defined "static unsigned int rx_buf[NUM_RX_BUF][ETH_BUF_SIZE>>2]"

But in the main function the use rx_buf as follows:

"Rx_Desc.Addr = (U32)&rx_buf;";

where Rx_Desc is an member of an array of structure type.

i is a variable of unsigned int datatype.

U32 has been defined as unsigned int using typedef.

My doubt is, can the address of a member of the array rx_buf be obtained by passing just one argument to rx_buf while its a 2-D array?

I thought 2-D arrays always require two arguments (rows) and (columns).

Hope someone could clarify my doubt.

Thanks.
 

there is no real array - it is an artificial construct - what you define as a n*m array is converted to a vector that is n*m long.

when you reference a row and column it is converted to the position in the vector
 

Just clear that an array is a constant pointer... if u define
int a[10];
int *p=a;

Now pointer p hold the address of a[0]...
when u use p+1, It indicates a[1], etc...
The same concept tey have used for 2D array...
Tey just stored the address of each row in a 1D array...
 

Just clear that an array is a constant pointer... if u define
int a[10];
i
Tey[sic] just stored the address of each row in a 1D array...

Are you sure about that? I believe the 'address' is derived from the pointer and the offset. the offset - for an array - being a function of the row and column

ie

if A[10, 5] then A[2,3] is at offset (2*10 + 3)

Think of the rows of an array being cut out and placed end to end............
 

"Rx_Desc.Addr = (U32)&rx_buf;"



This line indicates that, the address of rx_buf[0] is stored in Rx_Desc.Addr...
Here if u pass rx_buff[0] it means that rx_buff[0][0]'s address...

here if u need to access the value in rx_buf[5],
u jst need is

int a = *(Rx_Desc.Addr + 5);

I think u are not clear about arrays and pointer concept!!!
 
Reactions: saur

    saur

    Points: 2
    Helpful Answer Positive Rating
conclusion..
consider A[3][2]
and the array is a 6 elements allocated in some address (0x8000)
then

Code C - [expand]
1
2
3
4
5
6
A     = 0x8000 is an address
&A   = 0x8000 is an address
A[0] = 0x8000 is an address
A[1] = 0x8002 address of next row
A[2] = 0x8004 adress of third row
A[1][1] = a value of second row second column

 
Reactions: saur

    saur

    Points: 2
    Helpful Answer Positive Rating
I think I need too study arrays again.

I had studied that when using 2D arrays, we can write as:

a[][10]; i.e. its no error if we don't specify the no. of rows but we must always mention the no. of rows.

So, I believed that when using an array also, we must specify the column along with row.

So, I doubted that the absence of the column could lead to error.

Anyways, Thanks for the clarification.

Hope you all have a nice day.

And as an additional help can you guys suggest any book for C which would be very useful and is extremely good.

Once again, Thanks.
 
Last edited:
The Rx_Desc is a array of structs and addr is its element (which is here used to hold the starting address of a row)

which is loaded by the corresponding rows of the rx_buf.. Thats all this tiny program does.....

Read Let us 'C' very good to explore c..
 
Last edited:

Also as a continuation to my problem, there is a part of code where we define a pointer (named "frame") to a structure variable.

The structure contains a member of type "char" and is an array as follows: "char data[1]"

Now I believe "&frame" means the address at which the variable frame which contains the address of the structure variable is stored.

So, what does the following code mean:
"sp = (unsigned int *)&frame->data[0];"

sp is a pointer to unsigned int type data.

As per my knowledge, "frame->data[0]" will access the member data[0] in the structure pointed to by the pointer "frame".

So, why are they using &frame??

I believe, (unsigned int *), is used to typecast the data so that it represents an address which contains unsigned int data.

I hope this code snippet would be useful, for you guys to give me guidance:

"unsigned int *sp;

sp = (unsigned int *)&frame->data[0];
"
where
"typedef struct os_frame { /* << System frame buffer structure >> */
U16 length; /* Total Length of data in frame */
U16 index; /* Buffer Position Index */
U8 data[3]; /* Buffer data (protocol headers + data) */
} OS_FRAME;

OS_FRAME *frame, input;
frame = &input;
"

Thanks.
 

I think I need too study arrays again.



And as an additional help can you guys suggest any book for C which would be very useful and is extremely good.

Once again, Thanks.

U can refer TheC programming language by dennis ritchie...
**broken link removed**
 
Reactions: saur

    saur

    Points: 2
    Helpful Answer Positive Rating
&frame->data[0] is equal to frame->data ..


They are meaning that the address of first element.

for example &A[0][0] is equal to A or A[0]
 


frame is a poiter to a structure...
A stucture member by a structure poiter variable can be accessed onle by the symbol, "->" as the normal structure member by a normal structure variable can be accessed by"."

"frame = &input;" ==> Here the structure variable input's address is stored in pointer frame and to access the member of input we can use the pointer "frame"

They just typecast the U8(1Byte data) to an unsigned interger... sp is an unsigned integer pointer that holds the address of input.data[0] and it's typecasted.

could you understand this??
 
Reactions: saur

    saur

    Points: 2
    Helpful Answer Positive Rating
I could understand that part.

My only doubt is that if we used the following code:
Code:
sp = (unsigned int *)&frame->data[0];

then, does it first access the member data[0] and then typecast its address, or it first takes the address of the pointer frame? Because if it first takes address of pointer, then the statement won't make sense.

But as I could understand by what you are saying , it will first access the structure member and then take its address.

Thanks.
 

It just access the structure member as an integer by it's address...
In the structure it's treated as "U8 data[3];"
But as per "sp = (unsigned int *)&frame->data[0];" it's typecasted to an integer...
sp 'll combine the data[0],data[1],data[2] and data[3] if it's 32 bit compiler...
if it's 16 bit compiler, it combones data[0]&data[1]...

Are you clear now?
 
Reactions: saur

    saur

    Points: 2
    Helpful Answer Positive Rating
Thanks,

I now understand it.

Thank You for patiently helping me out
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…