Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Problem with function pointers and structures

Reply
Thread Tools

Re: Problem with function pointers and structures

 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      07-02-2009
Simon Connah <(E-Mail Removed)> wrote:
> This is most likely a stupid question but I am having difficulty
> implementing it.


> Basically I want to create a structure which holds data about an object
> and then have a pointer to another structure which holds function
> pointers to manipulate the data in the first structure (add, edit,
> delete etc etc).


> The problem is I am not entirely sure of the best way to go about this,
> here is a basic mock up of what I have so far:


> user.h


> /* forward declaration */


> struct user;


> typedef struct user_interface
> {
> int (*add)(PGconn *, struct user *);
> int (*delete)(PGconn *, struct user *);
> int (*edit)(PGconn *, struct user *);
> } user_interface;


> typedef struct user
> {
> char *username;
> char *password;


> /* lots more... omitted for brevity */


> struct user *interface;


As far as I understood you this should be a pointer to the structure
with the function pointers. Then you would need here

user_interface *interface;

> } user;


The you can do in the C code

user_interface if = { add, delete, edit );
user a_user;

a_user.interface = &if;

a_user.interface->add( PGconn_arg1, &a_user );

etc. Is it that what you're planning to do?

Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      07-02-2009
(E-Mail Removed) (Jens Thoms Toerring) writes:
[...]
> The you can do in the C code
>
> user_interface if = { add, delete, edit );
> user a_user;
>
> a_user.interface = &if;
>
> a_user.interface->add( PGconn_arg1, &a_user );

[...]

You might want to choose an identifier other than "if".

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      07-02-2009
Keith Thompson <(E-Mail Removed)> wrote:
> (E-Mail Removed) (Jens Thoms Toerring) writes:
> [...]
> > The you can do in the C code
> >
> > user_interface if = { add, delete, edit );
> > user a_user;
> >
> > a_user.interface = &if;
> >
> > a_user.interface->add( PGconn_arg1, &a_user );

> [...]


> You might want to choose an identifier other than "if".


Ooops, yes, definitely
Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Moi
Guest
Posts: n/a
 
      07-03-2009
On Thu, 02 Jul 2009 23:54:25 +0000, Jens Thoms Toerring wrote:

> Keith Thompson <(E-Mail Removed)> wrote:
>> (E-Mail Removed) (Jens Thoms Toerring) writes: [...]
>> > The you can do in the C code


>> > a_user.interface = &if;


>
>> You might want to choose an identifier other than "if".

>
> Ooops, yes, definitely


I would suggest "this" or "new"


AvK

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-03-2009
Moi <(E-Mail Removed)> writes:
> On Thu, 02 Jul 2009 23:54:25 +0000, Jens Thoms Toerring wrote:
>
>> Keith Thompson <(E-Mail Removed)> wrote:
>>> (E-Mail Removed) (Jens Thoms Toerring) writes: [...]
>>> > The you can do in the C code

>
>>> > a_user.interface = &if;

>
>>
>>> You might want to choose an identifier other than "if".

>>
>> Ooops, yes, definitely

>
> I would suggest "this" or "new"
>
>


Or "restrict" or "inline" if you're using a C90 compiler.

}

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      07-03-2009
Simon Connah <(E-Mail Removed)> wrote:
> On 2009-07-02 21:13:20 +0100, (E-Mail Removed) (Jens Thoms Toerring) said:


> > As far as I understood you this should be a pointer to the structure
> > with the function pointers. Then you would need here
> >
> > user_interface *interface;
> >
> >> } user;

> >
> > The you can do in the C code
> >
> > user_interface ifc = { add, delete, edit );
> > user a_user;
> >
> > a_user.interface = &ifc;
> >
> > a_user.interface->add( PGconn_arg1, &a_user );
> >
> > etc. Is it that what you're planning to do?


> Yeah that is pretty much the plan.


> I'm basically looking for a way to achieve some level of data
> abstraction with my library so that client applications can perform
> certain tasks on data without having to know specific implementation
> details specifically the layout of the structures in use.


> The real issue is to expose a public interface for manipulating the
> structures which is sufficiently general to allow radical redesign of
> the underlying implementation without requireing programs to be
> rewritten to take advantage of new or improved code.


To me it looks a bit as if you're still in a design stage of your
project. There are still a lot of words like "some level", "certain
tasks", "sufficiently general" etc. in your problem description
that make it rather difficult to decide on a certain way of how to
implement things.

> While it would most likely be easier in an object orientated language,
> it is more fun in C.


It looks as if you intent to use some OO techniques implemented
in C. That definitely could be a way to go. But if it is I can't
deduced from what you have posted. All what's rather clear is
that you seem to have some "users" (perhaps going to be stored in
a database if I interpret the 'PGconn' variable correctly - it
looks a bit like you intent to use PostgreSQL and, if that's the
case, you might want to think about the question if there isn't
a way to write your program to not bind on a certain database)
to be added, changed or deleted. But that's about all what one
(or at least I) can deduce from what you've posted. But I guess
what the "client application' is going to be that has to do
"certain tasks" remains a mystery for everyone but you, so
commenting on that (and especially on how exactly to do it)
is a bit difficult.

Perhaps it would be advantageous to concentrate on the design
at the moment and not to worry too much about how to implement
it in C (or another language) at this stage. You may end up not
using C if it is to be too cumbersome for what you want to do,
or arrive at questions that are easier to answer

Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      07-03-2009
Keith Thompson <(E-Mail Removed)> wrote:
> Moi <(E-Mail Removed)> writes:
> > On Thu, 02 Jul 2009 23:54:25 +0000, Jens Thoms Toerring wrote:
> >
> >> Keith Thompson <(E-Mail Removed)> wrote:
> >>> (E-Mail Removed) (Jens Thoms Toerring) writes: [...]
> >>> > The you can do in the C code

> >
> >>> > a_user.interface = &if;

> >
> >>> You might want to choose an identifier other than "if".
> >>
> >> Ooops, yes, definitely

> >
> > I would suggest "this" or "new"
> >


> Or "restrict" or "inline" if you're using a C90 compiler.
> }


Now you made me spew my last beer over the keyboard - and who
is going to buy me a new one? At least insert some spoiler with
a warning, you bastards!
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      07-04-2009
Keith Thompson <(E-Mail Removed)> wrote:

> Moi <(E-Mail Removed)> writes:
> > On Thu, 02 Jul 2009 23:54:25 +0000, Jens Thoms Toerring wrote:
> >> Keith Thompson <(E-Mail Removed)> wrote:
> >>> (E-Mail Removed) (Jens Thoms Toerring) writes: [...]


> >>> > a_user.interface = &if;

> >
> >>> You might want to choose an identifier other than "if".
> >>
> >> Ooops, yes, definitely

> >
> > I would suggest "this" or "new"
> >
> >

>
> Or "restrict" or "inline" if you're using a C90 compiler.
>
> }


That's not funny... before C99, I _did_ often use "inline" as an
identifier. Grmbl.

Richard
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Re: Problem with function pointers and structures jameskuyper C Programming 2 07-09-2009 04:25 PM
Re: Problem with function pointers and structures Eric Sosman C Programming 3 07-04-2009 05:35 PM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM



Advertisments