473,770 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

referencing with &

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[i] = &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.

---
Best regards, Roman
Feb 13 '07 #1
6 1679
On 2ÔÂ13ÈÕ, ÉÏÎç11ʱ21·Ö, "Roman Mashak" <m...@tusur.ruw rote:
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[i] = &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.

---
Best regards, Roman
I think there may be some codes like these which do not be shown here
tx_bufs = (unsigned char *)malloc(TX_BUF _SIZE * 4);
the four elements of the tx_buf array just point to the 4 segmengts of
tx_bufs,
each of which sized TX_BUF_SIZE.
I don't know if I made myself understood with my poor Enlish :)

Feb 13 '07 #2
On Tue, 13 Feb 2007 12:21:08 +0900, "Roman Mashak" <mr*@tusur.ru >
wrote in comp.lang.c:
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[i] = &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.
Assuming that the tx_bufs pointer member of the structure is
initialized with the address of a block containing at least 4 *
TX_BUF_SIZE characters, then, as 'i' is incremented in four passes
through the loop:

tx_bufs [0 * TX_BUF_SIZE] is the first character that it points to and
&tx_bufs [0 * TX_BUF_SIZE] is the address of the first character.

So at the end of the loop, tx_buf[0] points to the first character of
the block, tx_buf[1] points to the 1024th character of the block, and
so on. This is because the & operator and the [] operator, when
applied to the same pointer in a single expression cancel each other
out (the standard actually states that "is evaluated and the result is
as if the & operator were removed and the [] operator were changed to
a + operator."

So &array[subscript] is equivalent to array + subscript.

Many C programmers, myself included, feel that the equivalent source
is much easier to understand:

for (i = 0; i < 4; i++)
tp->tx_buf[i] = tp->tx_bufs + (i * TX_BUF_SIZE);

It makes it clearer that you are adding an integer value to a pointer,
which results in a pointer.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 13 '07 #3
On Feb 13, 3:21 am, "Roman Mashak" <m...@tusur.ruw rote:
Hello,

I encountered a piece of code, which I can't entirely understand. Here it
is:
<snip>
tp->tx_buf[i] = &tp->tx_bufs[i * TX_BUF_SIZE];
<snip>

This is probably more clear if you read it as
tp->tx_buf[i] = tp->tx_bufs + i * TX_BUF_SIZE

The array is simply being initialized to point
into the transfer buffer at equally spaced
intervals.

--
Bill Pursell

Feb 13 '07 #4
On Tue, 13 Feb 2007 12:21:08 +0900, "Roman Mashak" <mr*@tusur.ru >
wrote:
>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[i] = &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
It would if the & operator were applied to tp->tx_bufs. But it isn't.
Se below.
>seems to me the wrong way, but ot works as it's supposed to do.
The [] operator has higher precedence than the & operator. The
expression on the right hand side evaluates as &(tp->tx_bufs[...]).

Start at the beginning and work it through step by step.

tx_buf is an array of pointers.
tx_buf[i] is the i-th pointer in the array. It will hold an
address.

tx_bufs is a pointer. It already holds an address (courtesy
of code you omitted). This address is the address of an object. By
program design, this object is the first of many objects arranged
sequentially in memory (just like an array of objects).
tx_bufs[i*TX_BUF_SIZE] is one of these objects.
&tx_bufs[i*TX_BUF_SIZE] is the address of the object describe
above.

The address of this object is assigned to the i-th pointer in
tx_buf.
Remove del for email
Feb 13 '07 #5
yu*******@gmail .com wrote:
I think there may be some codes like these which do not be shown here
code. code. code.
tx_bufs = (unsigned char *)malloc(TX_BUF _SIZE * 4);
No cast.

Feb 13 '07 #6
Hello, Barry!
You wrote on Mon, 12 Feb 2007 21:07:17 -0800:

[skip]
BS tx_bufs is a pointer. It already holds an address (courtesy
BSof code you omitted). This address is the address of an object. By
BSprogram design, this object is the first of many objects arranged
BSsequentially in memory (just like an array of objects).
BStx_bufs[i*TX_BUF_SIZE] is one of these objects.
BS&tx_bufs[i*TX_BUF_SIZE] is the address of the object describe
BSabove. The address of this object is assigned to the i-th pointer in
BStx_buf.
I thank everybody for comprehensive explanation. The code is now obvious for
me.

---
Best regards, Roman Mashak
Feb 13 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1792
by: Alexander Landgraf | last post by:
Hi group, I am not sure if this is the correct place for my question. If not, please tell me. I tried to compile Python 2.3.4 under AT&T MP RAS SVR4: $./configure --prefix=${HOME} --with-threads=no $make but make fails because each invocation of ld causes errors like:
1
11443
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a href="mailto:DrTebi@yahoo.com">DrTebi</a> This would work like a regular mailto link in any browser, but wouldn't be visible to spiders if they don't have a function to decode it.
6
2471
by: jstaggs39 | last post by:
I want to create a Dcount and an If...Then...Else statement to count the number of records in a table based on the date that is entered to run the form. The If....Else statment comes in because if the amount of records for a particular date is positive, i want the form to stop running, if there are no records that contain the date in question, then it should continue to run the form. The problem i'm having is referencing the date that is...
6
2476
by: Mikey_Doc | last post by:
Hi We are running cms 2002, Framework 1.0 with Visual studio 2002. We have just upgraded to Framework 1.1 and visual studio 2003. All of our database connection strings are stored within the machine config, this was necessary as our web site has 4 environments and the database server has a different name in each. Since the upgrade the applications can't read the strings in the
3
3875
by: gary | last post by:
Hi, I am trying to reference an anchor in a user control with a url. This worked in 1.1 but no longer works in 2.0. The ascx control is located in a "/include" folder If you have a hyperlink control and you assign the navigateurl property = "../#anchor" whereby you want to add this # reference to the current url the url ends up with the #anchor twice.
9
1594
by: Brett Romero | last post by:
Say I have a library (A.dll) with a method that accepts a collection of a specific type. The type is defined in B.dll. In A.dll, I need to loop through this collection and reference fields of the type. For example, a Person type. It may have arm, leg, feet, and hands. Hands is a collection containing types of fingers. The only way to get at fingers is by accepting the parameter as its true type. This means I'll need a file...
0
1155
by: Demetri | last post by:
I have a performance question with regards to packaging assemblies and referencing them. First lets say you have created the following assemblies: Company.Framework.dll Company.Framework.IO.dll Company.Framework.Xml.dll Company.Framework.Data.dll Company.Framework.Web.dll
14
5935
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only because of this, so I hope some of you gurus can enlighten me with this :) In what circumstances can the "&amp;" in the source code be involuntary changed to "&" by a browser when or other software, when editing and uploading the file to the web...
1
2370
by: Tim F | last post by:
Problem: I'm receiving the error "File or assembly name XXXXX or one of its dependencies, was not found." when trying to execute code in an assmebly that has both a strong-name and has been installed into the GAC. We originally had this assembly without a strong-name and we were successfully using it by referencing it when it was NOT in the GAC. The assembly was built using the 1.0 framework and we were able to call it from both 1.0...
0
1259
by: dba123 | last post by:
I need to "tie together the cookie with the domain" by referencing the information from my web.config below in my login's codebhind. I don't understand how. I need to also save that after doing so: <add key="cookieDomainName" value=".test.com" /> <authentication mode="Forms"> <forms loginUrl="login.aspx" name="CH.COM.AUTH" path="/"/> </authentication>
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.