Re: Requesting sample code!



On Tue, 4 Nov 2008 20:13:00 -0800, Robby
<Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

snip code that doesn't do what is wanted


But I am looking all over for sample code which embeds the functions or
pointers to function in structures. In other words I am looking for sample

In C, you cannot embed a function in a structure.

A pointer to function is a scalar object conceptually no different
than a pointer to int or pointer to char or pointer to struct. You
have already posted gobs of code in which a member is a pointer.
Putting a pointer to function in a structure is no different.

code which would show how to include funtions or pointers to functions as
members of a struct. Very similar ... you know like we do in C++ when we have

Here is a your pointer to function that was defined at block scope in
the code I snipped above:
int (*pf)(int, int);

Here is some code you posted previously in the thread "static array
question?":
typedef struct i{
short v;
}w;

typedef struct z{
int x;
w *xxx;
}f;

Notice that struct z contains two members, the second being a pointer
to struct i. Adding your function pointer after xxx, we get:
typedef struct z{
int x;
w *xxx;
int (*pf)(int, int);
}f;

struct z now contains three members, the third being a pointer to
function.

Given this simplicity, I have to believe I'm missing the real issue in
your question.

methods in our classes. I want to do the same thing with structures. Is this
possible and can someone direct me to some samples or books which illustrate
on how to code this.

The syntax for declaring an object is the same whether the object is a
member of a structure or "stand-alone". Since you know how to declare
an independent pointer to function, you also know how to declare one
that is a member of a function. (You didn't ask but just for
completeness, you access such a member the same way you access any
structure member, using either the . or -> operator as appropriate.)


I would show an example but I don't even know where to start to code this,
but I know it is possible because I have seen something similar to what I
want to do at:

http://bytes.com/forum/thread583012.html

But I would like a full description of how to implement functions and
pointers to functions as members of a struct which is not posted as a
question but rather as a tutorial or book!

Write 100 times, a function can never be a member of a struct.

--
Remove del for email
.



Relevant Pages

  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • Re: Another spinoza challenge
    ... You should test against the int type's limits: ... typedef struct complex ... a pointer to an integer ... A macro is preferable because it is replaced by inline code, ...
    (comp.lang.c)
  • Re: Another spinoza challenge
    ... int main{ ... using struct and typedef. ... a pointer to an integer ... A macro is preferable because it is replaced by inline code, ...
    (comp.lang.c)
  • Re: [RFC][PATCH 1/6] memcg: fix pre_destory handler
    ... returns struct cgroup of id. ... SwapCgroup uses array of "pointer" to record the owner of swaps. ... struct cgroup_id is freed by RCU. ... changed interface from pointer to "int" ...
    (Linux-Kernel)
  • gcc, aliasing rules and unions
    ... struct B {int x, y;}; ... A struct pointer can be converted to another ... Also I don't know what the "effective type" of a union member is. ...
    (comp.lang.c)

Loading