473,814 Members | 3,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(cha r *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(e ntry, &mii_devs) {
dev = list_entry(entr y, 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 1571
robert <ro************ @gmail.comwrite s:
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(cha r *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_devi ce = {
.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(entr y, 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_v 1;
} else {
dev->read = mii_dev_read_v2 ;
dev->write = mii_dev_write_v 2;
}

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(cha r *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(e ntry, &mii_devs) {
dev = list_entry(entr y, 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
3407
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 don't know how many points there will be in every struct in the end, so I have to allocate memory dynamically for them and can't use an array of fixed size, unfortunately. I would like to know if there is a better way to access struct members...
8
5446
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
2078
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 parameter. What would I need to do in order to convert a struct that looks like this: typedef struct { char rsvd0; char iadl1;
5
2921
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 that, a structure definition that includes function pointers only defines the function prototypes to be used with them, but not the actual implementations, whereas in C++, member functions cannot be changed *unless* virtual functions are used, or the
17
3482
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 even compile.)
29
2792
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 overhead (I think). A Struct seems more appropriate. At least it is what I would have used in other languages. But since a Struct *can* hold methods, I wander if I am saving anything. If not, why use it?
19
2563
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 functions for manipulating a Date. However, it does not specify that those functions should be the only ones to depend directly on Date ’s representation and the only ones to directly access objects of class Date . This restriction can be...
43
3846
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 "there is no difference between a struct and a class except the public/private access specification" (and a few minor other things). When I create a class, I always start by declaring the default constructor, copy constructor and assignment operator...
13
2538
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 functions in common implementations? And where is the 'this' ptr tucked away at for POD structs with member functions? John
4
4986
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 code which will automatically find out the type of struct elements and print them (indented manner ?) if they are primitive types like int, float or char* ? else it will recurse into the composite data type.
0
10670
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...
0
10142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9225
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6897
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
5570
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...
0
5708
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4358
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
3886
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3030
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.