Function pointer in structures

  • Thread starter =?iso-8859-1?B?QW5kcuk=?=
  • Start date
?

=?iso-8859-1?B?QW5kcuk=?=

Hi all,

Let's say I have the following code:

typedef struct
{
void (*function)();
} TYPE;

void function()
{
printf("The caller structure is...?\n");
}

int main()
{
TYPE x;
TYPE y;

x->function = *function;
y->function = *function;

x.function();
y.function();
}

Is it possible to know where am I calling the function "function" from?
(if it is from the structure x or y?) I don't want to pass x or y as a
argument.

Thank you in advance,

André
 
M

matevzb

Hi all,

Let's say I have the following code:

typedef struct
{
void (*function)();

} TYPE;
void function()
{
printf("The caller structure is...?\n");

}
int main()
{
TYPE x;
TYPE y;

x->function = *function;
y->function = *function;
Here you probably meant "x.function = function;".
x.function();
y.function();

}
Is it possible to know where am I calling the function "function" from?
(if it is from the structure x or y?) I don't want to pass x or y as a
argument.

Thank you in advance,

André
The function is not being called from the structure, the structure only
contains a pointer to it, so the answer would be no.
 
C

Chris Dollin

André said:
Hi all,

Let's say I have the following code:

typedef struct
{
void (*function)();
} TYPE;

TYPE NAMES USUALLY DON'T SHOUT AT YOU. IT'S MACROS
THAT SHOUT.
void function()
{
printf("The caller structure is...?\n");
}

int main()
{
TYPE x;
TYPE y;

x->function = *function;
y->function = *function;

Surely you mean

x.function = function;
y.function = function;

since `x` and `y` are struct objects, not pointers-to-struct,
and the `*` applied to `function` is here pointless. (The
name `function` refers to the function object, which decays into
a function pointer. The `*` dereferences it back to the function
object, which then immediately decays back into the pointer before
you assign it to the function fields.)
x.function();
y.function();
}

Is it possible to know where am I calling the function "function" from?
(if it is from the structure x or y?) I don't want to pass x or y as a
argument.

Tough. You'll have to. Why don't you want to?
 
?

=?iso-8859-1?B?QW5kcuk=?=

Surely you mean
x.function = function;
y.function = function;

since `x` and `y` are struct objects, not pointers-to-struct,
and the `*` applied to `function` is here pointless. (The
name `function` refers to the function object, which decays into
a function pointer. The `*` dereferences it back to the function
object, which then immediately decays back into the pointer before
you assign it to the function fields.)

Ah, ok.
Tough. You'll have to. Why don't you want to?

I wanted to build a OO-like structure, so that x and y would be objects
and "function" would be a methods. I know I can do something like
function(type* object), but I meant to do things differently.

I was trying to think of a possible hack, but since I wanted to be ANSI
portable, it's kinda tough... Thank you anyway.

André
 
C

Chris Dollin

André said:
Ah, ok.


I wanted to build a OO-like structure, so that x and y would be objects
and "function" would be a methods. I know I can do something like
function(type* object), but I meant to do things differently.

There's no need to: a typical implementation strategy for
OO-languages is for a method call `x.f(y)` to call the method
`f` (which may depend on `x`, eg by being a field of `x`, or
being a field of a type object pointed to by `x`) with the
arguments `x` and `y`. So your `something like function(type* object)`
is just a C implementation of that tactic.

It may not look as /pretty/, but that's another matter.

(It may be an important matter, since persistent ugliness can
be a bar to maintenance. But that's a different type of technical.)
 
?

=?iso-8859-1?B?QW5kcuk=?=

I wanted to build a OO-like structure, so that x and y would be objects
There's no need to: a typical implementation strategy for
OO-languages is for a method call `x.f(y)` to call the method
`f` (which may depend on `x`, eg by being a field of `x`, or
being a field of a type object pointed to by `x`) with the
arguments `x` and `y`. So your `something like function(type* object)`
is just a C implementation of that tactic.

I see. My point was exactly a attempt to be able to call 'x.f(y)' from
within C. Too bad there isn't a simpler way to do it. I'm thinking that
maybe a object table may help on this matter. We'll see.

André
 
S

Stephen Sprunk

André said:
I wanted to build a OO-like structure, so that x and y would be
objects
and "function" would be a methods. I know I can do something like
function(type* object), but I meant to do things differently.

If you want that syntax, C++ is down the hall. If you're going to stick
to C, then you'll need to add an object parameter so that the function
can know the context it's being called in. In fact, this is what many
(most?) C++ implementations do under the hood; there's a hidden first
parameter called "this" which the compiler automatically inserts.
Hiding it is just a convenience to the programmer; C doesn't do that, so
you'll need to add a "this" parameter yourself.

S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top