R
Roman Mashak
Hello,
I encountered a piece of code, which I can't entirely understand. Here it
is:
#define TX_BUF_SIZE 1024
struct priv {
...
unsigned char *tx_buf[4];
unsigned char *tx_bufs;
};
static void init_ring()
{
struct priv *tp;
int i;
...
for (i = 0; i < 4; i++)
tp->tx_buf = &tp->tx_bufs[i * TX_BUF_SIZE];
}
static int open_dev()
{
...
/* here tp->tx_bufs gets initialised properly, i.e. it's not NULL. It
points to a big buffer */
init_ring();
...
}
Both structure members cause doubts to me: 'tx_buf' is array of pointers,
'tx_bufs' is just a pointer. So what they're doing in 'init_ring' is
initialising this array with addresses, but I can't figure out how. Setting
&tp->tx_bufs will yield address of object (i.e. address of pointer), so it
seems to me the wrong way, but ot works as it's supposed to do.
Please clarify my doubts. Thank you.
I encountered a piece of code, which I can't entirely understand. Here it
is:
#define TX_BUF_SIZE 1024
struct priv {
...
unsigned char *tx_buf[4];
unsigned char *tx_bufs;
};
static void init_ring()
{
struct priv *tp;
int i;
...
for (i = 0; i < 4; i++)
tp->tx_buf = &tp->tx_bufs[i * TX_BUF_SIZE];
}
static int open_dev()
{
...
/* here tp->tx_bufs gets initialised properly, i.e. it's not NULL. It
points to a big buffer */
init_ring();
...
}
Both structure members cause doubts to me: 'tx_buf' is array of pointers,
'tx_bufs' is just a pointer. So what they're doing in 'init_ring' is
initialising this array with addresses, but I can't figure out how. Setting
&tp->tx_bufs will yield address of object (i.e. address of pointer), so it
seems to me the wrong way, but ot works as it's supposed to do.
Please clarify my doubts. Thank you.