473,327 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Structs with functions?

Hi all,

I'm trying to understand some code - u-boot 1.3rc3 - that reads data
from an ethernet phy. A struct is defined as:

struct mii_dev {
struct list_head link;
char *name;
int (* read)(char *devname, unsigned char addr,
unsigned char reg, unsigned short *value);
int (* write)(char *devname, unsigned char addr,
unsigned char reg, unsigned short value);
};

I've never seen syntax like this for its read and write members, how
does that work - what is doing the underlying read() and write() ?
Where is that defined? Here's an example of its use:

/
************************************************** ***************************
*
* Read to variable <valuefrom the PHY attached to device <devname>,
* use PHY address <addrand register <reg>.
*
* Returns:
* 0 on success
*/
int miiphy_read(char *devname, unsigned char addr, unsigned char reg,
unsigned short *value)
{
struct list_head *entry;
struct mii_dev *dev;
int found_dev = 0;
int read_ret = 0;

if (!devname) {
printf("NULL device name!\n");
return 1;
}

list_for_each(entry, &mii_devs) {
dev = list_entry(entry, struct mii_dev, link);

if (strcmp(devname, dev->name) == 0) {
found_dev = 1;
read_ret = dev->read(devname, addr, reg,
value);
break;
}
}

if (found_dev == 0)
printf("No such device: %s\n", devname);

return ((found_dev) ? read_ret : 1);
}

Just trying to understand, please help,
Robert

Oct 29 '07 #1
5 1546
robert <ro************@gmail.comwrites:
I'm trying to understand some code - u-boot 1.3rc3 - that reads data
from an ethernet phy. A struct is defined as:

struct mii_dev {
struct list_head link;
char *name;
int (* read)(char *devname, unsigned char addr,
unsigned char reg, unsigned short *value);
int (* write)(char *devname, unsigned char addr,
unsigned char reg, unsigned short value);
};

I've never seen syntax like this for its read and write members, how
does that work - what is doing the underlying read() and write() ?
read and write are pointers to functions. That is what the extra
brackets do:

char *f(int) /* f is function taking an int and returning a
char pointer */
char (*f)(int) /* f is pointer to function taking an int and
returning a char */
Where is that defined? Here's an example of its use:
<snip>
int miiphy_read(char *devname, unsigned char addr, unsigned char reg,
unsigned short *value)
{
<snip>
}
Somewhere there will be a struct mii_dev object whose read field is
set to point to miiphy_read:

struct mii_dev actual_mii_device = {
.read = miiphy_read,
.write = miiphy_write,
};

(this uses the new "designator" syntax from C99 to initialise named
fields in the structure.

--
Ben.
Oct 29 '07 #2
robert wrote:
Hi all,

I'm trying to understand some code - u-boot 1.3rc3 - that reads data
from an ethernet phy. A struct is defined as:

struct mii_dev {
struct list_head link;
char *name;
int (* read)(char *devname, unsigned char addr,
unsigned char reg, unsigned short *value);
int (* write)(char *devname, unsigned char addr,
unsigned char reg, unsigned short value);
};

I've never seen syntax like this for its read and write members, how
does that work - what is doing the underlying read() and write() ?
Where is that defined? Here's an example of its use:
The read and write members are not actual functions; they are pointers
to functions.
....
struct mii_dev *dev;
....
dev = list_entry(entry, struct mii_dev, link);
It is probably the case that list_entry() is what actually causes
those pointers to be initialized to point at some particular
functions. Since 'struct mii_dev' is a type name, list_entry is
presumably a function-like macro. Find out what the definition of
that macro is, and you'll have a better idea of what's going on.
However, it's quite possible that the setting of the 'read' and
'write' members occurs inside code that you have no access to, so it
won't necessarily be possible to track this down in full detail.

Oct 29 '07 #3
Ben Bacarisse <be********@bsb.me.ukwrites:
char (*f)(int) /* f is pointer to function taking an int and
returning a char */
Yeah, but returning a char pointer divided by what?
..
..
..
..
..
..
..
..
..
..<joke>
Oct 29 '07 #4
robert wrote:
Hi all,

I'm trying to understand some code - u-boot 1.3rc3 - that reads data
from an ethernet phy. A struct is defined as:

struct mii_dev {
struct list_head link;
char *name;
int (* read)(char *devname, unsigned char addr,
unsigned char reg, unsigned short *value);
int (* write)(char *devname, unsigned char addr,
unsigned char reg, unsigned short value);
};

I've never seen syntax like this for its read and write members, how
does that work - what is doing the underlying read() and write() ?
Those two struct members are pointer to functions. Let say you have a
library and want to support two versions of mii_dev I/O:

int mii_dev_read_v1(char *devname, unsigned char addr, unsigned char reg,
unsigned short *value)
{
[...]
}

int mii_dev_read_v2(char *devname, unsigned char addr, unsigned char reg,
unsigned short *value)
{
[...]
}
etc.

then during initialization of struct mii_dev, you can dynamically assign
which version to use, like this:

struct mii_dev dev;

if (version == v1) {
dev->read = mii_dev_read_v1;
dev->write = mii_dev_write_v1;
} else {
dev->read = mii_dev_read_v2;
dev->write = mii_dev_write_v2;
}

Hence, the caller don't see the nasty details.

--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>
Oct 29 '07 #5
On Oct 29, 9:58 am, robert <robertlazar...@gmail.comwrote:
Hi all,

I'm trying to understand some code - u-boot 1.3rc3 - that reads data
from an ethernet phy. A struct is defined as:

struct mii_dev {
struct list_head link;
char *name;
int (* read)(char *devname, unsigned char addr,
unsigned char reg, unsigned short *value);
int (* write)(char *devname, unsigned char addr,
unsigned char reg, unsigned short value);

};

I've never seen syntax like this for its read and write members, how
does that work - what is doing the underlying read() and write() ?
Where is that defined? Here's an example of its use:

/
************************************************** ****************************
*
* Read to variable <valuefrom the PHY attached to device <devname>,
* use PHY address <addrand register <reg>.
*
* Returns:
* 0 on success
*/
int miiphy_read(char *devname, unsigned char addr, unsigned char reg,
unsigned short *value)
{
struct list_head *entry;
struct mii_dev *dev;
int found_dev = 0;
int read_ret = 0;

if (!devname) {
printf("NULL device name!\n");
return 1;
}

list_for_each(entry, &mii_devs) {
dev = list_entry(entry, struct mii_dev, link);

if (strcmp(devname, dev->name) == 0) {
found_dev = 1;
read_ret = dev->read(devname, addr, reg,
value);
break;
}
}

if (found_dev == 0)
printf("No such device: %s\n", devname);

return ((found_dev) ? read_ret : 1);

}

Just trying to understand, please help,
Robert
They are function pointers.
Your program is expected to assign a function to do the task.
For example,

struct mii_dev myStruct;

myStruct.read = MyReadFunction;
myStruct.write = myWriteFunction;

where you have defined MyreadFunction and MyWriteFunction using
the prototypes specified in the struct definition; i.e., the read
function is a function that returns an int and has
arguments char *, unsigned char, unsigned char, and unsigned short *

--
Fred L. Kleinschmidt
Oct 29 '07 #6

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

Similar topics

11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
8
by: subnet | last post by:
Excuse me for the perhaps silly question, but is the following permitted: struct mystruct { int f1; int f2; char f3; float f4; };
10
by: Angel | last post by:
I'm using several C functions (in a dll) that receive a struct as parameter. Since I'm doing it in C#, I assume I need to recreate the struct in C# in order to call the function with the required...
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
17
by: Johan Tibell | last post by:
Could someone outline the pros and cons of typedefing pointers to structs like this? typedef struct exp_ { int val; struct exp_ *child; } *exp; (This is straight from memory so it might not...
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
19
by: desktop | last post by:
There is a lot of info on this topic on google. But in Bjarne Stroustrup 's book page 225 he writes: "The declaration of Date in the previous subsection (declared as a struct) provides a set of...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
4
by: call_me_anything | last post by:
I have different kind of data structures in different applications. The data structures are big and complex and I would like to print the members of each struct. Can we write a generic piece of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.