By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,429 Members | 1,484 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,429 IT Pros & Developers. It's quick & easy.

Function pointer in structures

P: n/a
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é

Jan 2 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Jan 2, 2:47 pm, "André" <andre....@gmail.comwrote:
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.
--
WYCIWYG - what you C is what you get.

Jan 2 '07 #2

P: n/a
André wrote:
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?

--
Chris "hopefully not Pyecroft" Dollin
Meaning precedes definition.

Jan 2 '07 #3

P: n/a
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é

Jan 2 '07 #4

P: n/a
André wrote:
>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.
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.)

--
Chris "hopefully not Pyecroft" Dollin
"The path to the web becomes deeper and wider" - October Project

Jan 2 '07 #5

P: n/a
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.
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é

Jan 2 '07 #6

P: n/a
"André" <an*******@gmail.comwrote in message
news:11**********************@n51g2000cwc.googlegr oups.com...
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

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from http://www.teranews.com

Jan 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.