experchange > c

G G (07-11-19, 03:44 AM)
why would one choose to use
typedef function pointer

i mean what makes you think of using a typedef function pointer
when writing a program?
James Kuyper (07-11-19, 04:17 AM)
On 7/10/19 9:44 PM, G G wrote:
> why would one choose to use
> typedef function pointer
> i mean what makes you think of using a typedef function pointer
> when writing a program?


As a rule of thumb, I avoid typedefing pointer types: C has special
rules for pointers, and it's a bad idea to lose track of which types are
pointer types, which is something that a pointer typedef would enable.
Also, it's feasible take a typedef for a function type, and to create
from it a declaration for a pointer to that function type. The reverse
is not true.

I have, however, declared typedefs for function types.

One thing that would make me want to think about creating such a typedef
is if the function type is complicated, and if my program will be using
that type in a large number of locations.
A pointer to any function type can be converted to a pointer to any
other function type, and then back to the original type. The result of
that pair of conversions is guaranteed to compare equal to the original
pointer, and can be used just as safely as the original pointer to call
the function that it points at. This is one of the same properties that
makes void* convenient for pointers to object types. However, unlike
void*, function pointer type conversions are never implicit. Therefore,
code that relies on this feature will involve a lot of casts, providing
an incentive for declaring a typedef.

Another reason for using a typedef is the fact that if I provide a
typedef for a function type that is important to my code, other code
which uses that typedef would not need to be re-written, merely
recompiled, if I should change the type that it's a typedef for.
Eli the Bearded (07-11-19, 04:27 AM)
In comp.lang.c, G G <gdotone> wrote:
> why would one choose to use
> typedef function pointer
> i mean what makes you think of using a typedef function pointer
> when writing a program?


Makes it easier to cast.

Elijah
------
seriously, casting function pointers is so tediously verbose
G G (07-11-19, 04:35 AM)
On Wednesday, July 10, 2019 at 10:18:13 PM UTC-4, James Kuyper wrote:

[..]
> typedef for a function type that is important to my code, other code
> which uses that typedef would not need to be re-written, merely
> recompiled, if I should change the type that it's a typedef for.


hm, ok. the thing that came to my mind, after reading your thoughts,
was placing such a typedef inside a struct. similar to a member function
class in c++.

thanks.
James Kuyper (07-11-19, 05:59 AM)
It would be even more appropriate to compare such a typedef (if it were permitted) with a C++ typedef member, which is permitted.

Typedef is a storage class specifier, and the C grammar does not allow for struct or union members to have a storage class specifier (6.7.2.1p1).

In C++, struct members have class scope, a concept that doesn't even exist in C. In C, if members could be typedefs, those typedefs would have the same scope as if they were declared immediately before the struct or union definition, so there would be little point in putting them inside a C struct.
Even worse,the members of each struct or union have their own name space (NOT the same thing as a C++ namespace). As a result, if member typedefs wereallowed, you could declare typedefs with the same name in multiple different struct or union definitions in the same scope. Since C doesn't have the C++ :: operator, there would be no way to distinguish which one you were referring to.
Those issues aren't insoluble, as shown by the fact that C++ allows such declations. But they would need to be solved if member typedefs were to be allowed.
G G (07-11-19, 06:13 AM)
On Thursday, July 11, 2019 at 12:00:01 AM UTC-4, James Kuyper wrote:
> It would be even more appropriate to compare such a typedef (if it were permitted) with a C++ typedef member, which is permitted.
> Typedef is a storage class specifier, and the C grammar does not allow for struct or union members to have a storage class specifier (6.7.2.1p1).
> In C++, struct members have class scope, a concept that doesn't even exist in C. In C, if members could be typedefs, those typedefs would have the same scope as if they were declared immediately before the struct or union definition, so there would be little point in putting them inside a C struct..
> Even worse,the members of each struct or union have their own name space (NOT the same thing as a C++ namespace). As a result, if member typedefs were allowed, you could declare typedefs with the same name in multiple different struct or union definitions in the same scope. Since C doesn't have the C++ :: operator, there would be no way to distinguish which one you were referring to.
> Those issues aren't insoluble, as shown by the fact that C++ allows such declations. But they would need to be solved if member typedefs were to beallowed.


ok, excellent, haven't got to c++ typedef's yet. :-) in the books i'm reading.
Thiago Adams (07-11-19, 01:27 PM)
On Wednesday, July 10, 2019 at 11:18:13 PM UTC-3, James Kuyper wrote:
> On 7/10/19 9:44 PM, G G wrote:
> As a rule of thumb, I avoid typedefing pointer types: C has special
> rules for pointers, and it's a bad idea to lose track of which types are
> pointer types, which is something that a pointer typedef would enable.


I agree with you.
I have seen people doing the following:

typedef char * String;

void Func(const String p)
{

}

typedef for pointers can lead this kind of confusion with
const.
Clearly the author of the code was trying to make
the pointed value const and not the pointer const.
Similar Threads