Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Pointer to function inside struct (http://www.velocityreviews.com/forums/t958495-pointer-to-function-inside-struct.html)

dmjcunha@gmail.com 03-08-2013 05:27 PM

Pointer to function inside struct
 
If I have on a header file:

struct structure1 {
int a;
int b; };

struct structure2 {
struct structure1 *(*create)(int k, int j);
...; }; |
|
\/
should I read this as a pointer to function create returning pointer to structure1?

And more, how should I implement the function? Should it follow exactly the same naming as in the declaration? Something else?

That's because I'm searching in the directory source tree for this same name declaration "struct structure1 *(*create)(int k, int j)" in *.c files and cannot find anything...

Please, some help. Thank's.

Eric Sosman 03-08-2013 06:12 PM

Re: Pointer to function inside struct
 
On 3/8/2013 12:27 PM, dmjcunha@gmail.com wrote:
> If I have on a header file:
>
> struct structure1 {
> int a;
> int b; };
>
> struct structure2 {
> struct structure1 *(*create)(int k, int j);
> ...; }; |
> |
> \/
> should I read this as a pointer to function create returning pointer to structure1?


Almost. It's a pointer that can point at a function of
two `int' arguments returning a `struct structure1 *' -- thus
far, you're right. But `create' is only the name of the field
in the `struct structure2', just as `a' and `b' name fields of
`struct structure1'. The functions that the `create' field
can point at will all have the same "signature" -- they'll all
take two `int' arguments and return a `struct structure1 *' --
but they can have any names at all.

> And more, how should I implement the function? Should it follow exactly the same naming as in the declaration? Something else?


Here are two functions that a `create' field might point at:

struct structure2 * make(int x, int y) {
struct structure2 *ptr = malloc(sizeof *ptr);
if (ptr != NULL) {
ptr->a = x;
ptr->b = y;
}
return ptr;
}

struct structure2 * bake(int foo, int bar) {
struct structure2 *p = malloc(sizeof *p);
if (p) {
p->a = foo / bar;
p->b = foo % bar;
}
return p;
}

Both these functions might exist in your program, and the `create'
field of a `struct structure2' could point at either:

struct structure2 buzz;
struct structure2 bomb;
buzz.create = make;
bomb.create = bake;

Later, you could call whatever the `create' field points at,
without needing to know exactly what function that is:

struct structure2 beep;
...
struct structure1 *grog = beep.create(42, 27);

> That's because I'm searching in the directory source tree for this same name declaration "struct structure1 *(*create)(int k, int j)" in *.c files and cannot find anything...


A target function *might* be named `create', but it might
be named `make' or `bake' or `zaphodBeeblebrox'. What you need
to search for is the place where the `create' field is initialized
or assigned to, and see what function names are used.

--
Eric Sosman
esosman@comcast-dot-net.invalid

Noob 03-08-2013 06:41 PM

Re: Pointer to function inside struct
 
dmjcunha wrote:

> If I have on a header file:
>
> struct structure1 {
> int a;
> int b; };
>
> struct structure2 {
> struct structure1 *(*create)(int k, int j);
> ...; };


I always use a typedef for function pointers.

typedef struct structure1 *func_t(int k, int j);
struct structure2 {
func_t *create;
};

struct structure1 *foobar(int k, int j)
{
return NULL;
}

struct structure2 xyz;
xyz.create = foobar;


Tim Rentsch 03-16-2013 03:38 AM

Re: Pointer to function inside struct
 
Eric Sosman <esosman@comcast-dot-net.invalid> writes:

> On 3/8/2013 12:27 PM, dmjcunha@gmail.com wrote:
>> If I have on a header file:
>>
>> struct structure1 {
>> int a;
>> int b; };
>>
>> struct structure2 {
>> struct structure1 *(*create)(int k, int j);
>> ...; }; |
>> |
>> \/
>> should I read this as a pointer to function create returning pointer to structure1?

>
> Almost. It's a pointer that can point at a function of
> two `int' arguments returning a `struct structure1 *' -- thus
> far, you're right. But `create' is only the name of the field
> in the `struct structure2', just as `a' and `b' name fields of
> `struct structure1'. The functions that the `create' field
> can point at will all have the same "signature" -- they'll all
> take two `int' arguments and return a `struct structure1 *' --
> but they can have any names at all. [snip]


The last sentence is wrong in a couple of different ways. In C
the term is type, not signature; the various target functions
will have the same type (provided they have been suitably
declared, which they may not have been, and indeed they might not
even be written in C; even so the appropriate term is type).
Furthermore, the sentence above misuses the term "signature" to
mean _only_ the type, which isn't how it is generally used.
Different functions, even if they have the same type, will not
have the same 'type signature'

http://en.wikipedia.org/wiki/Type_signature

Personally I think 'type signature' is kind of a stupid term,
because basically it means the same thing as "declaration".
But people want to sound smart so they say 'type signature'
instead of 'declaration'. However, if one is going to use
the term, at least use it correctly: appropriate targets
for the above pointer-to-function will all have compatible
types, but they will not have the same type signature.

glen herrmannsfeldt 03-16-2013 05:08 AM

Re: Pointer to function inside struct
 
Tim Rentsch <txr@alumni.caltech.edu> wrote:

(snip, someone wrote)
> The last sentence is wrong in a couple of different ways. In C
> the term is type, not signature; the various target functions
> will have the same type (provided they have been suitably
> declared, which they may not have been, and indeed they might not
> even be written in C; even so the appropriate term is type).
> Furthermore, the sentence above misuses the term "signature" to
> mean _only_ the type, which isn't how it is generally used.
> Different functions, even if they have the same type, will not
> have the same 'type signature'


> http://en.wikipedia.org/wiki/Type_signature


> Personally I think 'type signature' is kind of a stupid term,
> because basically it means the same thing as "declaration".
> But people want to sound smart so they say 'type signature'
> instead of 'declaration'. However, if one is going to use
> the term, at least use it correctly: appropriate targets
> for the above pointer-to-function will all have compatible
> types, but they will not have the same type signature.


I don't especially like it either.

Still, in Java the signature does not include the return type.
I am not sure about other languages.

-- glen

BartC 03-17-2013 12:04 PM

Re: Pointer to function inside struct
 
"Tim Rentsch" <txr@alumni.caltech.edu> wrote in message
news:kfny5doau9v.fsf@x-alumni2.alumni.caltech.edu...
> Eric Sosman <esosman@comcast-dot-net.invalid> writes:


>> `struct structure1'. The functions that the `create' field
>> can point at will all have the same "signature" -- they'll all
>> take two `int' arguments and return a `struct structure1 *' --
>> but they can have any names at all. [snip]

>
> The last sentence is wrong in a couple of different ways. In C
> the term is type, not signature; the various target functions
> will have the same type (provided they have been suitably
> declared, which they may not have been, and indeed they might not
> even be written in C; even so the appropriate term is type).
> Furthermore, the sentence above misuses the term "signature" to
> mean _only_ the type, which isn't how it is generally used.
> Different functions, even if they have the same type, will not
> have the same 'type signature'
>
> http://en.wikipedia.org/wiki/Type_signature


That article says the signature includes the name of the function. That's
wrong. And not very useful. (It suggests a function is only compatible with
itself...)

> Personally I think 'type signature' is kind of a stupid term,
> because basically it means the same thing as "declaration".
> But people want to sound smart so they say 'type signature'
> instead of 'declaration'. However, if one is going to use
> the term, at least use it correctly: appropriate targets
> for the above pointer-to-function will all have compatible
> types, but they will not have the same type signature.


There are two attributes of a function: its signature (the set of
parameters, plus the return type), and its type (what value it yields when
a call to it is used in an expression, which is the same as its return
type).

So signature and type can mean two different things.

Two functions can be compatible if they have the same signature (you can
substitute the name of one, with the name of the other, and it should still
be valid type-wise). And when you have function pointers, there is no name
of the function to consider, only signature (which in C you might call the
'type' just before you call it, and then have another type as its result).

A declaration isn't quite the same as a signature either, but you can define
a signature using a declaration. It would be like saying a type is the same
as a declaration too.

--
Bartc


Stephen Sprunk 03-17-2013 04:54 PM

Re: Pointer to function inside struct
 
On 17-Mar-13 07:04, BartC wrote:
> "Tim Rentsch" <txr@alumni.caltech.edu> wrote in message
> news:kfny5doau9v.fsf@x-alumni2.alumni.caltech.edu...
>> Different functions, even if they have the same type, will not
>> have the same 'type signature'
>>
>> http://en.wikipedia.org/wiki/Type_signature

>
> That article says the signature includes the name of the function.
> That's wrong. And not very useful. (It suggests a function is only
> compatible with itself...)


If an article needs improvement, it's generally faster to do so yourself
than to complain about it to others.

(You don't even need to create an account, though IMHO there are a good
reasons to do so.)

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

Tim Rentsch 03-17-2013 08:59 PM

Re: Pointer to function inside struct
 
"BartC" <bc@freeuk.com> writes:

> "Tim Rentsch" <txr@alumni.caltech.edu> wrote in message
> news:kfny5doau9v.fsf@x-alumni2.alumni.caltech.edu...
>> Eric Sosman <esosman@comcast-dot-net.invalid> writes:

>
>>> `struct structure1'. The functions that the `create' field
>>> can point at will all have the same "signature" -- they'll all
>>> take two `int' arguments and return a `struct structure1 *' --
>>> but they can have any names at all. [snip]

>>
>> The last sentence is wrong in a couple of different ways. In C
>> the term is type, not signature; the various target functions
>> will have the same type (provided they have been suitably
>> declared, which they may not have been, and indeed they might not
>> even be written in C; even so the appropriate term is type).
>> Furthermore, the sentence above misuses the term "signature" to
>> mean _only_ the type, which isn't how it is generally used.
>> Different functions, even if they have the same type, will not
>> have the same 'type signature'
>>
>> http://en.wikipedia.org/wiki/Type_signature

>
> That article says the signature includes the name of the
> function. That's wrong. [snip]


No, it isn't. Try looking up what a signature is in, for
example, the C++ language definition document or the Java
language definition document.

>> Personally I think 'type signature' is kind of a stupid term,
>> because basically it means the same thing as "declaration".
>> But people want to sound smart so they say 'type signature'
>> instead of 'declaration'. However, if one is going to use
>> the term, at least use it correctly: appropriate targets
>> for the above pointer-to-function will all have compatible
>> types, but they will not have the same type signature.

>
> There are two attributes of a function: its signature (the set
> of parameters, plus the return type),


Again, this usage is out of step with how the term is used
in languages that have an official definition.

> and its type (what value it yields when a call to it is used in
> an expression, which is the same as its return type).


And this is wrong for C. In C the type of a function also gives
a specification for the types of the function parameters. That
may be a partial specification rather than a full specification,
but a function type always includes some information about what
parameter typings are allowed.

> So signature and type can mean two different things.


They do mean different things, because a signature includes
the name of the function, method, or constructor in question.

> Two functions can be compatible if they have the same signature
> (you can substitute the name of one, with the name of the other,
> and it should still be valid type-wise). And when you have
> function pointers, there is no name of the function to consider,
> only signature (which in C you might call the 'type' just before
> you call it, and then have another type as its result).


In C the type of a function includes both the type of its result
and the types of its parameters. If the distinction is important
we might say "fully specified type" rather than just "type", but
these days developers almost always write function declarations
using prototypes, so in most cases there is no difference. Some
people misuse the term "signature" to mean essentially the same
thing as "fully specified type", but (a) there's no reason to do
so, since "type" or "fully specified type" works just fine, and
(b) the term "signature" is not defined (indeed, the word never
appears) in the Standard, and other languages that use the term
define it differently.

> A declaration isn't quite the same as a signature either,


The term "signature" means different things in different languages,
but AFAIAA all languages that use the term (ie, as part of their
official definition) include some kind of name information along
with type information. Furthermore my comment was about the term
"type signature", as explained in the Wikipedia article. That
definition is pretty close to what most languages would call a
declaration, the main differences being (a) declarations often
include some additional keywords or punctuation so that they may be
identified as declarations in the program, and (b) declarations
may be given for both non-functions and functions, whereas "type
signature" applies only to function or function-like entities.
The two terms clearly aren't the same, but there is an obvious
mapping between them which is (for functions) basically an
isomorphism.

> but you can define a signature using a declaration.


Here again you're assuming that your personal usage corresponds
to how the term is used in other languages. It doesn't. In C++,
for example, a declaration by itself is not enough to infer a
function signature, which also includes the namespace(s) in which
the function is defined.

> It would be like saying a type is the same as a declaration
> too.


Only if one mistakenly assumes that the terms "signature" or "type
signature" are basically synonymous with "fully specified type",
which they are not.

James Kuyper 03-17-2013 11:29 PM

Re: Pointer to function inside struct
 
On 03/17/2013 08:04 AM, BartC wrote:
> "Tim Rentsch" <txr@alumni.caltech.edu> wrote in message
> news:kfny5doau9v.fsf@x-alumni2.alumni.caltech.edu...

....
>> http://en.wikipedia.org/wiki/Type_signature

>
> That article says the signature includes the name of the function. That's
> wrong. And not very useful. (It suggests a function is only compatible with
> itself...)


The concept of a function signature plays no part in C's definition of
function compatibility, so the participation of a function's name in
it's signature is no barrier to compatibility.
I'm only familiar with one language which does use the concept of
signatures (which is a measure of my ignorance, not an assertion that
there are few such languages) - that language is C++. It quite clearly
defines the signature as including the name (sections 1.3.17-22, in the
latest draft I have copy of, dated 2012-01-16). This conflicts with what
I thought "signature" meant, but I searched for text making use of that
term, and didn't find any such text where the fact that the signature
includes the function name would be a problem. In particular, I didn't
see any problems it might cause for compatibility. I might have missing
something.
--
James Kuyper

Seebs 03-19-2013 06:12 AM

Re: Pointer to function inside struct
 
On 2013-03-17, Tim Rentsch <txr@alumni.caltech.edu> wrote:
> No, it isn't. Try looking up what a signature is in, for
> example, the C++ language definition document or the Java
> language definition document.


This is interesting. I've often seen "signature" used to refer to the
function's type, as in "you need a function matching the signature of..."

> Again, this usage is out of step with how the term is used
> in languages that have an official definition.


I don't have specs for them, but I am not sure that this is persuasive
in a discussion of C, and even in C++, I see plenty of examples of people
using the term in the sense of "type of arguments and return".

I suspect that what's happened here is that the concept of "a signature which
is compatible with the signature of X" or something similar got compressed and
extra words dropped. So in practice, it appears that people frequently talk of
two signatures "matching" even if one or more of the names involved are
different.

-s
--
Copyright 2013, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.


All times are GMT. The time now is 07:56 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.