473,324 Members | 2,581 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,324 software developers and data experts.

Function Pointers in a Structure?

It's been mentioned to me that, in standard c, that you can have
structures behave very much like classes in relation to something to do
with function calls within a structure.

Now, I didn't get the details, but does someone have a clue of what
this person perhaps was talking about? Do you think he just ment
something such as function pointers? Or could I, much like a C++
Class, call a class function that has access to the structure it's
associated with, without including a pointer refrence to the object as
one of the variables of the function?

This will be coded on a Sun/Solaris system using Forte 5.0, by the
way if that's important. I can't use explicit C++ with this project
but if I can use c and have it work a lot like C++ that would be great.

Tony

Nov 15 '05 #1
10 8624
gm********@yahoo.com wrote:
It's been mentioned to me that, in standard c, that you can have
structures behave very much like classes in relation to something to do
with function calls within a structure.

Now, I didn't get the details, but does someone have a clue of what
this person perhaps was talking about? Do you think he just ment
something such as function pointers? Or could I, much like a C++
Class, call a class function that has access to the structure it's
associated with, without including a pointer refrence to the object as
one of the variables of the function?

This will be coded on a Sun/Solaris system using Forte 5.0, by the
way if that's important. I can't use explicit C++ with this project
but if I can use c and have it work a lot like C++ that would be great.


The person was talking about function pointers in structures;
this gives you your "class methods". Appropriate access macros
can make access to these "methods" slightly clearer.
You can fake single inheritance by including the parent struct
as first member into the child struct; if you access a pointer
to the child struct as a pointer to an ancestor, everything
still works. In much the same way you can effectively overwrite
parent methods of ancestors by storing another function pointer
in the child.
It is usually a good idea to have a base class every other class
is derived from; if you have a control word encoding the type of
the respective class, you have RTTI.
And so on. Just look for object oriented programming in C in the
newsgroups archives.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

gm********@yahoo.com writes:
It's been mentioned to me that, in standard c, that you can have
structures behave very much like classes in relation to something to do
with function calls within a structure.

Now, I didn't get the details, but does someone have a clue of what
this person perhaps was talking about? Do you think he just ment
something such as function pointers? Or could I, much like a C++
Class, call a class function that has access to the structure it's
associated with, without including a pointer refrence to the object as
one of the variables of the function?


You can do single inheritance, virtual functions, polymorphism and
RTTI, interfaces/mixins, object properties and all sorts of OO stuff
in plain C. However, there are libraries to help you do it properly,
GObject being one of the most widely used:

http://developer.gnome.org/doc/API/2...ect/index.html
http://www.le-hacker.org/papers/gobject/
Regards,
Roger

- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iD8DBQFDDi6PVcFcaSW/uEgRAqunAJ4uBUqYKUx/7z0pwUxpqVjKKHSGNACgts5W
qIWsX2tQiWYkUwPNaqjnm1I=
=tcwj
-----END PGP SIGNATURE-----
Nov 15 '05 #3
gm********@yahoo.com wrote on 25/08/05 :
It's been mentioned to me that, in standard c, that you can have
structures behave very much like classes in relation to something to do
with function calls within a structure.
Well, sort of... The syntax is gory, because there is no implicit
'this' in C... All is explicit, thus gory...
Now, I didn't get the details, but does someone have a clue of what
this person perhaps was talking about? Do you think he just ment
something such as function pointers? Or could I, much like a C++
Class, call a class function that has access to the structure it's
associated with, without including a pointer refrence to the object as
one of the variables of the function?

This will be coded on a Sun/Solaris system using Forte 5.0, by the
way if that's important. I can't use explicit C++ with this project
but if I can use c and have it work a lot like C++ that would be great.


What about Objective C?
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
Nov 15 '05 #4
Ian
gm********@yahoo.com wrote:
This will be coded on a Sun/Solaris system using Forte 5.0, by the
way if that's important. I can't use explicit C++ with this project
but if I can use c and have it work a lot like C++ that would be great.

Obvious question, why can't you use C++?

Ian
Nov 15 '05 #5
gm********@yahoo.com wrote:
It's been mentioned to me that, in standard c, that you can have
structures behave very much like classes in relation to something to do
with function calls within a structure.

Now, I didn't get the details, but does someone have a clue of what
this person perhaps was talking about? Do you think he just ment
something such as function pointers? Or could I, much like a C++
Class, call a class function that has access to the structure it's
associated with, without including a pointer refrence to the object as
one of the variables of the function?

This will be coded on a Sun/Solaris system using Forte 5.0, by the
way if that's important. I can't use explicit C++ with this project
but if I can use c and have it work a lot like C++ that would be great.

Tony


You can define interfaces like this:

// In a header file you do:
struct interface; // forward declaration
// Define a LIST data type for holding linked lists
typedef struct tagList {
struct interface * lpVtbl; // Pointer to the function table
// Data fields
struct tagList *PointerToLastElement;
struct tagList *FirstElement;
int NumberOfElements;
} LIST;

// Define the function table (methods)
struct interface {
int (*Append)(LIST *L,void *data);
void *(*First)(LIST *L);
void *(*Last)(LIST *L);
void *(*Remove)(LIST *L);
int (*Length)(LIST *L);
// Just an example. Many functions are missing
// But you get the idea.
};

// Now, in an implementation file you do:

static int Append(LIST *L,void *data)
{ // Implementation omitted }

static void *First(LIST *L)
{ // Implementation omitted }

static void *Last(LIST *L)
{ // Implementation omitted }

static void *Remove(LIST *L)
{ // Implementation omitted }

static int Length(LIST *L)
{ // Implementation omitted }

// Define a static struct interface holding
// function pointers to the implementation.
static struct interface {
First, Last,
Remove, Length
} MethodTable;

// Define the constructor.
LIST *newList(int NumberOfElements)
{
LIST *result;
result = malloc(sizeof(LIST));
if (result) {
memset(result,0,sizeof(LIST));
// assign the method table
result->lpVtbl = & MethodTable;
// assign the other fields
}
return result;
}

AND NOW (at last) you use it like this:

#include "list.h"
int main(void)
{
LIST *myList = newList(12);

myList->lpVtbl->Append(myList,"Element1");
myList->lpVtbl->Append(myList,"Element2");
int d = myList->lpVtbl->Length(myList); // Should be 2
}

This is quite powerful since you can at any time replace
those function pointers by a function of your own (subclassing)
but you can keep a copy of the function pointer to call the
original method either before or after or in between your
own subclassed method, what you can't do in C++.

C is more explicit than C++, and there is no compiler help.
This is less of a problem than what it seems at first sight,
and MUCH more powerful. You are in control. There is no
black box of a compiler to keep you from doing what you want.

Also, there are no rules other than the rules you decide to follow.

Of course this can be used constructively or can lead to code
that is impossible to follow. It is up to you.

For an example of this kind of programming see the source code
of the container library in lcc-win32.

http://www.cs.virginia.edu/~lcc-win32.
Nov 15 '05 #6
jacob navia wrote:
typedef struct tagList {
struct interface * lpVtbl; // Pointer to the function table
// Data fields
struct tagList *PointerToLastElement;
struct tagList *FirstElement;
int NumberOfElements;
} LIST;


Sorry that should have been
typedef struct tagListElement {
struct tagListElement *Next;
void *Data;
} ListElement;

typedef struct tagList {
struct interface * lpVtbl; // Pointer to the function table
// Data fields
ListElement *Last;
ListElement *First;
int NumberOfElements;
} LIST;

A LIST contains list elements, not LISTs.

jacob
Nov 15 '05 #7
Project leader doesn't want to use C++ for now. The project also
contains 5 year old code (All I've worked on) and I don't want to mix
and match just now. When I re-write the code, I may request I could do
everything in C++ but for now, I have to use C. The thing is, if I can
start using object-oriented practices in my code now, it may make the
transition easier later.

For everyone else, I want to say a big thanks for all this help. Now
to take time and absorb what you've given me so I can try and
understand it.

Tony

Nov 15 '05 #8
jacob navia wrote:
// Define the function table (methods)
struct interface {
int (*Append)(LIST *L,void *data);
void *(*First)(LIST *L);
void *(*Last)(LIST *L);
void *(*Remove)(LIST *L);
int (*Length)(LIST *L);
// Just an example. Many functions are missing
// But you get the idea.
};

// Now, in an implementation file you do:
static int Append(LIST *L,void *data)
{ // Implementation omitted }

// Define a static struct interface holding
// function pointers to the implementation.
static struct interface {
First, Last,
Remove, Length
} MethodTable;


The only thing that concerns me at this point, are multi-threaded
applications. Is this example multi-thread safe? I know this isn't
exactly a strict C problem at this point but, I was wondering if you
knew. The reason I ask this is, your creating a single instance of the
interface structure, MethodTable as a global and then assigning that to
each instance of the LIST structre someone creates and initalizes with
newList(). Would their be a problem or not?

Tony

Nov 15 '05 #9
gm********@yahoo.com wrote:
The only thing that concerns me at this point, are multi-threaded
applications. Is this example multi-thread safe? I know this isn't
exactly a strict C problem at this point but, I was wondering if you
knew. The reason I ask this is, your creating a single instance of the
interface structure, MethodTable as a global and then assigning that to
each instance of the LIST structre someone creates and initalizes with
newList(). Would their be a problem or not?


Notice that in most cases the data in that structure is strictly
READ ONLY and that can be accesses from as many threads as you wish.

I would never modify the data structure directly but make a local copy,
then modify the copy, unless you want to make the change permanent for
all objects using it. It is up to you. In THAT case you need to access
that data structure using a guarded channel, typically a critical
section under windows, or a similar construct under other OS.

Nov 15 '05 #10
Ian
gm********@yahoo.com wrote:
Project leader doesn't want to use C++ for now. The project also
contains 5 year old code (All I've worked on) and I don't want to mix
and match just now. When I re-write the code, I may request I could do
everything in C++ but for now, I have to use C. The thing is, if I can
start using object-oriented practices in my code now, it may make the
transition easier later.

Good reason.

It may be a bit old now, but one of best OO C toolkits I have used was
XView. It is quite small and well structured, you would learn a lot
from it. You can still get the source and the (very good) manual is
available from

http://www.oreilly.com/openbook/openlook/

Ian
Nov 15 '05 #11

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

Similar topics

11
by: chetan | last post by:
How to initialise a function pointer for any particular function in C++ ? so that it would clearly specify difference between the functions that are called by different objects of that class ......
10
by: Barbrawl McBribe | last post by:
Is is possible to use typedefs to cast function pointers? I think I saw this in the WINGs src; grep for '(hashFunc)'. So far, trying to use a typedef to cast function pointers so that a return...
2
by: Mark Olbert | last post by:
First off, the sympathy is for all you poor buggers out there who have to figure out how to marry Managed Extensions for C++ onto your legacy code. My condolences; my brief experience with the...
18
by: ramu | last post by:
Can anyone tell me the advantages of using function pointers? Thanks in advance.
45
by: noridotjabi | last post by:
What is the purpose of the function pointer? Why do you need a pointer to a function. I cannot really think of any application where this is the only or even easiest solution to a problem. I'm...
1
by: Bushido Hacks | last post by:
A private utility function using a function pointer sounds ideal to me. I want to simpify writing the same set of loops. While there are a few functions that can't use it, setting and modifying...
3
by: googlinggoogler | last post by:
Hi This should all be pretty standard C stuff, but I'm going to use terms like mouse callback to communicate what Im tyring to do. Basically I have my program whirling around in an infinite...
9
by: Matt | last post by:
Hi friends, Okay so well, I have quite a problem right now with a file stream. What I am doing is to use the Cannon SDK dlls to get control over my old Cannon A60 Camera for some surveillance...
2
by: sdsjohnny | last post by:
Hi, I following structure typedef struct { UINT32 abc; int (*f)(int a, intb); char *ptr; } my_struct;
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.