C Struct

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Balroj

C Struct

Post by Balroj »

Hi,

I was taking a look at the linux kernel source, and find this struc:

Code: Select all

struct consw {
const char *(*con_startup)(void);
void (*con_init)(struct vc_data *, int);
void (*con_deinit)(struct vc_data *);
void (*con_clear)(struct vc_data *, int, int, int, int);
void (*con_putc)(struct vc_data *, int, int, int);
void (*con_putcs)(struct vc_data *,const unsigned short *,int ,int ,int);
void (*con_cursor)(struct vc_data *, int);
int (*con_scroll)(struct vc_data *, int, int, int, int);
.....
But i don't understand it well, void (*con_init)(struct vc_data *, int);i don't understand this. Can anyone please explain it? :)

thanks.
anubis

Re:C Struct

Post by anubis »

Hi,
con_init is a function asscociated with console initialization.

vc_data is a structure containig the properties regarding the console.

using vc_data, con_init initializes the console. i am not sure what the other parameter(int) in con_init refer to...
be

Re:C Struct

Post by be »

I think this is a function pointers, some pointer assigned to a function, and then this function called not by name, but by the pointer. ::) :)
TB

Re:C Struct

Post by TB »

void (*con_init)(struct vc_data *, int);

Yes, it's pointer to a function with (struct vc_data *, int) as parameters.
So, you would do something like:
con_init = (address of the function) and then you can do things like con_init(&data, 123);
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:C Struct

Post by Pype.Clicker »

basically, this technique provide something looking like polymorphism in C: you can define how a console is initialized by setting the right function as console.con_init=f and then call it with something like console_ptr->con_init(data,int)
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:C Struct

Post by df »

good for dirvers / vfs interfaces etc
-- Stu --
Post Reply