Newbie: complex typedef 
Author Message
 Newbie: complex typedef

Hello,

Consider the following typedef:

typedef DWORD (__stdcall *MYFUNC)(PIP_ADAPTER_INFO, ULONG*);

Am I correct in assuming that this declares MYFUNC as "pointer to
__stdcall function that returns DWORD and takes PIP_ADAPTER_INFO and a
pointer to ULONG as args"?

If no, what does it mean?
If yes, once I've got a pointer of type MYFUNC, how do I use it to
call the function?

Thanks,
Mahak
--



Sun, 16 Oct 2005 07:21:23 GMT  
 Newbie: complex typedef

comp.lang.c.moderated:

Quote:
> Hello,

> Consider the following typedef:

> typedef DWORD (__stdcall *MYFUNC)(PIP_ADAPTER_INFO, ULONG*);

> Am I correct in assuming that this declares MYFUNC as "pointer to
> __stdcall function that returns DWORD and takes PIP_ADAPTER_INFO and a
> pointer to ULONG as args"?

Assuming that DWORD, PIP_ADAPTER_INFO, and ULONG are all defined as
something reasonable (because C doesn't define them at all), and that
__stdcall is defined as nothing, then it means what you think.

Quote:
> If no, what does it mean?
> If yes, once I've got a pointer of type MYFUNC, how do I use it to
> call the function?

> Thanks,
> Mahak

Assuming:

   MYFUNC myfunc;

....and assuming myfunc is initialized or has assigned to it a suitable
function with a matching signature, you call it like this:

   myfunc(arg1, arg2);

....or...

DWORD dword = myfunc(arg1, arg2);

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
--



Tue, 18 Oct 2005 15:27:14 GMT  
 Newbie: complex typedef


Quote:
>Hello,

>Consider the following typedef:

>typedef DWORD (__stdcall *MYFUNC)(PIP_ADAPTER_INFO, ULONG*);

>Am I correct in assuming that this declares MYFUNC as "pointer to
>__stdcall function that returns DWORD and takes PIP_ADAPTER_INFO and a
>pointer to ULONG as args"?

almost except it is a ULONG*

Quote:

>If no, what does it mean?
>If yes, once I've got a pointer of type MYFUNC, how do I use it to
>call the function?

typedef gives a name to a type. You cannot call a type however you can
have variables of that type so:

MYFUNC foo_ptr;

// do something to initialise foo_ptr with a function of the correct
type

foo_ptr(/* suitable arguments */);

If you want to see how function pointers are useful study a good text on
the design and use of qsort.

--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Tue, 18 Oct 2005 15:27:37 GMT  
 Newbie: complex typedef

Quote:

> typedef DWORD (__stdcall *MYFUNC)(PIP_ADAPTER_INFO, ULONG*);
> Am I correct in assuming that this declares MYFUNC as "pointer to
> __stdcall function that returns DWORD and takes PIP_ADAPTER_INFO and a
> pointer to ULONG as args"?

Setting aside the fact that from the point of view of this newsgroup,
__stdcall doesn't exist: yes.

Quote:
> If yes, once I've got a pointer of type MYFUNC, how do I use it to
> call the function?

Just call it.  

And get yourself a usable textbook on C, preferredly K&R2 (more
details in the FAQ).  You'll run into more of these questions, and
your current one, if any, obviously isn't up to the task of explaining
them.
--

Even if all the snow were burnt, ashes would remain.
--



Tue, 18 Oct 2005 15:27:41 GMT  
 Newbie: complex typedef

Quote:
>Hello,

>Consider the following typedef:

>typedef DWORD (__stdcall *MYFUNC)(PIP_ADAPTER_INFO, ULONG*);

>Am I correct in assuming that this declares MYFUNC as "pointer to
>__stdcall function that returns DWORD and takes PIP_ADAPTER_INFO and a
>pointer to ULONG as args"?

>If no, what does it mean?
>If yes, once I've got a pointer of type MYFUNC, how do I use it to
>call the function?

MYFUNC func_ptr;
DWORD dword_var;

func_ptr = name_of_previously_declared_function;

dword_var = (*func_ptr)(arg_1_of_correct_type, arg_2_of_correct_type);

<<Remove the del for email>>
--



Tue, 18 Oct 2005 15:27:43 GMT  
 Newbie: complex typedef

Quote:

> Consider the following typedef:

> typedef DWORD (__stdcall *MYFUNC)(PIP_ADAPTER_INFO, ULONG*);

> Am I correct in assuming that this declares MYFUNC as "pointer to
> __stdcall function that returns DWORD and takes PIP_ADAPTER_INFO and a
> pointer to ULONG as args"?

Yes, assuming that DWORD, PIP_ADAPTER_INFO, and ULONG are previously
defined types, and that __stdcall is a function modifier defined as an
extension by your compiler.

Quote:
> If yes, once I've got a pointer of type MYFUNC, how do I use it to
> call the function?

Use it in place of the actual function name:

DWORD __stdcall bar (PIP_ADAPTER_INFO, ULONG*);   /* prototype for
actual function */

MYFUNC foo;             /* define func ptr */
....
  foo = bar;            /* assign ptr to actual function address */
....

  (*foo) (pip, ulp);    /* call bar via ptr foo */
  foo (pip, ulp);       /* same as above  */

Thad
--



Tue, 18 Oct 2005 15:27:45 GMT  
 Newbie: complex typedef

Quote:

> Hello,
> Consider the following typedef:
> typedef DWORD (__stdcall *MYFUNC)(PIP_ADAPTER_INFO, ULONG*);
> Am I correct in assuming that this declares MYFUNC as "pointer to
> __stdcall function that returns DWORD and takes PIP_ADAPTER_INFO and a
> pointer to ULONG as args"?
> If no, what does it mean?
> If yes, once I've got a pointer of type MYFUNC, how do I use it to
> call the function?

First, remove the typedef, and you have:

    DWORD (__stdcall *MYFUNC)(PIP_ADAPTER_INFO, ULONG*);

This declares MYFUNC to be a pointer to a function which takes
PIP_ADAPTER_INFO, and pointer to a ULONG and returns a DWORD.
It looks to me that __stdcall is a compiler extension; see your
documentation.

Now, if you add the typedef, as in your example, instead of
declaring MYFUNC to be a pointer to the mentioned function,
MYFUNC becomes an alias for a pointer to a function with the
aforementioned semantics.

Hence, if you have

    typedef DWORD (__stdcall *MYFUNC)(PIP_ADAPTER_INFO, ULONG*);

and

    DWORD my_func(PIP_ADAPTER_INFO, ULONG*);

You can do

    MYFUNC fptr = &my_func;

[ Note that the & is actually superfluous since the function name
decays into its pointer. ]

Now, to call the function via fptr you will do the following:
    (*fptr)(your_pip_info, your_p_ulong);

Alex
--



Tue, 18 Oct 2005 15:27:47 GMT  
 Newbie: complex typedef

Quote:

>Consider the following typedef:

>typedef DWORD (__stdcall *MYFUNC)(PIP_ADAPTER_INFO, ULONG*);

>Am I correct in assuming that this declares MYFUNC as "pointer to
>__stdcall function that returns DWORD and takes PIP_ADAPTER_INFO and a
>pointer to ULONG as args"?

Assuming that your compiler does something reasonable with the
non-standard __stdcall flag, then you've got it right.

Quote:
>If yes, once I've got a pointer of type MYFUNC, how do I use it to
>call the function?

void f(MYFUNC p)
{
    ULONG array[10];
    p(pip, array);

Quote:
}

You can also use the syntax "(*p)(pip, array);" which is preferred by
some for clarity (it's obvious that 'p' is a function pointer).

-andy
--



Tue, 18 Oct 2005 15:27:49 GMT  
 Newbie: complex typedef

Quote:

> Hello,

> Consider the following typedef:

> typedef DWORD (__stdcall *MYFUNC)(PIP_ADAPTER_INFO, ULONG*);

I'm not fully certain about the __stdcall as that's an implementation
defined symbol.  Ignoring that however, the typedef seems to be correct.

Quote:
> If yes, once I've got a pointer of type MYFUNC, how do I use it to
> call the function?

MYFUNC func = the_real_func;
....
a_dword = func(some_pip_adpt_info, &a_ulong)

HTH
Neal
--



Tue, 18 Oct 2005 15:27:51 GMT  
 Newbie: complex typedef


Quote:
> Hello,

> Consider the following typedef:

> typedef DWORD (__stdcall *MYFUNC)(PIP_ADAPTER_INFO, ULONG*);

MYFUNC is a typedef to a function that returns DWORD and takes 2 args
PIP_ADAPTER_INFO et ULONG*.

Quote:
> once I've got a pointer of type MYFUNC, how do I use it to
> call the function?

a pointer to function acts like a function because a function is a
pointer...:

DWORD the_function(PIP_ADAPTER_INFO arg1, ULONG* arg2)
{
    [...]

Quote:
}

MYFUNC pf = the_function;

you can call the_function like this:
1) the_function(arg1, arg2)
2) (*the_function)(arg1, arg2)
3) pf(arg1, arg2)
4) (*pf)(arg1, arg2)

taz
--



Tue, 18 Oct 2005 15:27:54 GMT  
 Newbie: complex typedef
[...]

Quote:
> a pointer to function acts like a function because a function is a
> pointer...:

Not quite.  Given a declaration like

    int foo(void);

the identifier "foo" is a function designator.  C99 6.3.2.1p4 says:

    A function designator is an expression that has function type.
    Except when it is the operand of the sizeof operator or the unary
    & operator, a function designator with type "function returning
    type" is converted to an expression that has type "pointer to
    function returning type".

This is very similar to what happens with array names.

Note in particular that this conversion takes place for the prefix of
a function call; the prefix is expected to be a function pointer.
Most of the time, the function pointer comes from the conversion of a
function name, but it can be any expression of an appropriate function
pointer type (such as the value of a function pointer object).  That's
why "foo()" and "(&foo)()" are equivalent; the first involves an
implicit conversion, and the second doesn't.

(The exception for "sizeof" may seem confusing, but a footnote
explains that it means that the expression "sizeof foo" causes a
constraint violation rather than yielding the size of a function
pointer.)

--

San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
--



Mon, 24 Oct 2005 11:15:31 GMT  
 Newbie: complex typedef

Quote:



[...]
> >If yes, once I've got a pointer of type MYFUNC, how do I use it to
> >call the function?

> void f(MYFUNC p)
> {
>     ULONG array[10];
>     p(pip, array);
> }

> You can also use the syntax "(*p)(pip, array);" which is preferred by
> some for clarity (it's obvious that 'p' is a function pointer).

I prefer that syntax myself, but note that it works whether p is the
name of a function pointer or the name of a function.

Given declarations like

    void func(void);
    void (*ptr)(void) = func;

all of the following calls are valid:

    func();
    (&func)();
    (*func)();
    (**func)();
    (***func)();
    ptr();
    (*ptr)();
    (**ptr)();
    (***ptr)();
    (&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*ptr)();

This is because any function designator (i.e., a function name or any
expression of some function type) decays to a function pointer (unless
its the operand of the unary "&" or "sizeof" operator).  If you
dereference a function pointer, you get a function value, which decays
back to a function pointer, which you can then dereference again.

Having said that, I agree that using the forms
    func();
and
    (*ptr)();
makes it clearer to the reader that func is the name of a function and
ptr is the name of a pointer object -- as long as the author of the
code wasn't being overly clever.

--

San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
--



Mon, 24 Oct 2005 11:15:34 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Newbie: typedef?

2. newbie typedef question

3. newbie help with structure/typedef

4. newbie typedef question

5. Typedef structs within typedef structs...

6. Typedef structs within typedef structs...

7. To typedef or not to typedef, that is the question

8. What the difference between "typedef struct" and "typedef struct tag_Struct"?

9. When is a typedef name not a typedef name?

10. When is a typedef name not a typedef name?

11. typedef struct tagNAME {} NAME; vs. typedef struct NAME {};

12. component design question-complex

 

 
Powered by phpBB® Forum Software