how to define a function pointer variable witout typdef?

Discussion in 'C Programming' started by baumann@pan, May 27, 2005.

  1. baumann@pan

    baumann@pan Guest

    hi all,

    typedef int (*pfunc)(int , int);
    pfunc a_func;

    i know it's ok,

    but how can define a_func without typedef statement?

    thanks .

    baumann@pan
     
    baumann@pan, May 27, 2005
    #1
    1. Advertising

  2. baumann@pan wrote:
    > hi all,
    >
    > typedef int (*pfunc)(int , int);
    > pfunc a_func;
    >
    > i know it's ok,
    >
    > but how can define a_func without typedef statement?


    int (*a_func)(int, int);

    Rob Gamble
     
    Robert Gamble, May 27, 2005
    #2
    1. Advertising

  3. baumann@pan wrote:
    > hi all,
    >
    > typedef int (*pfunc)(int , int);
    > pfunc a_func;
    >
    > i know it's ok,
    >
    > but how can define a_func without typedef statement?
    >


    int (*a_func)(int, int);

    cdecl <<EOD
    explain int (*a_func)(int, int)
    quit
    EOD
    declare a_func as pointer to function (int, int) returning int
     
    Martin Ambuhl, May 27, 2005
    #3
  4. "baumann@pan" <> wrote in message news:...
    > hi all,
    >
    > typedef int (*pfunc)(int , int);
    > pfunc a_func;
    >
    > i know it's ok,
    >
    > but how can define a_func without typedef statement?
    >
    > thanks .
    >


    int (*a_func)(int, int);
    But this is a declaration not a definition.


    --
    "combination is the heart of chess"

    A.Alekhine

    Mail to:
    sathyashrayan AT gmail DOT com
     
    sathyashrayan, May 27, 2005
    #4
  5. baumann@pan

    -berlin.de Guest

    sathyashrayan <> wrote:

    > "baumann@pan" <> wrote in message news:...
    >> typedef int (*pfunc)(int , int);
    >> pfunc a_func;
    >>
    >> i know it's ok,
    >>
    >> but how can define a_func without typedef statement?


    > int (*a_func)(int, int);
    > But this is a declaration not a definition.


    No, this creates a variable, named 'a_func', that can hold a pointer
    to a function taking two int arguments and returning an int. No
    'extern' to see seen in front of it, so it's a definition, not a
    declaration.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, May 27, 2005
    #5
  6. baumann@pan

    CBFalconer Guest

    -berlin.de wrote:
    > sathyashrayan <> wrote:
    >> "baumann@pan" <> wrote in message

    >
    >>> typedef int (*pfunc)(int , int);
    >>> pfunc a_func;
    >>>
    >>> i know it's ok,
    >>>
    >>> but how can define a_func without typedef statement?

    >
    >> int (*a_func)(int, int);
    >> But this is a declaration not a definition.

    >
    > No, this creates a variable, named 'a_func', that can hold a
    > pointer to a function taking two int arguments and returning an
    > int. No 'extern' to see seen in front of it, so it's a definition,
    > not a declaration.


    It's a definition of a pointer variable, which in turn is useless
    until filled with a pointer to a compatible function. Such a
    function can be defined with:

    int this_func(int a, int b)
    {
    /* the actual definition goes here */
    }

    and then the statement:

    a_func = this_func;

    can initialize a_func, and make it usable. Because there is no '('
    immediately following a_func or this_func these are pointer values,
    not function calls.

    --
    Some informative links:
    news:news.announce.newusers
    http://www.geocities.com/nnqweb/
    http://www.catb.org/~esr/faqs/smart-questions.html
    http://www.caliburn.nl/topposting.html
    http://www.netmeister.org/news/learn2quote.html
     
    CBFalconer, May 27, 2005
    #6
  7. baumann@pan

    pete Guest

    -berlin.de wrote:
    >
    > sathyashrayan <> wrote:
    >
    > > "baumann@pan" <> wrote in message
    > > news:...
    > >> typedef int (*pfunc)(int , int);
    > >> pfunc a_func;
    > >>
    > >> i know it's ok,
    > >>
    > >> but how can define a_func without typedef statement?

    >
    > > int (*a_func)(int, int);
    > > But this is a declaration not a definition.

    >
    > No, this creates a variable, named 'a_func', that can hold a pointer
    > to a function taking two int arguments and returning an int. No
    > 'extern' to see seen in front of it, so it's a definition, not a
    > declaration.


    It's a definition and a declaration.

    --
    pete
     
    pete, May 28, 2005
    #7
  8. baumann@pan wrote:

    > typedef int (*pfunc)(int , int);
    > pfunc a_func;
    >
    > I know it's ok,
    > but how can I define a_func without typedef statement?


    > cat main.c

    #include <stdio.h>

    int this_func(const int a, const int b) {
    return fprintf(stdout, "a = %d\tb = %d\n", a, b);
    }

    int (*a_func)(int, int) = this_func;

    int main(int argc, char* argv[]) {
    a_func(13, 42);
    return 0;
    }

    > gcc -Wall -std=c99 -pedantic -o main main.c
    > ./main

    a = 13 b = 42
     
    E. Robert Tisdale, May 28, 2005
    #8
  9. <-berlin.de> wrote in message news:...
    > sathyashrayan <> wrote:
    >
    > > "baumann@pan" <> wrote in message news:...
    > >> typedef int (*pfunc)(int , int);
    > >> pfunc a_func;
    > >>
    > >> i know it's ok,
    > >>
    > >> but how can define a_func without typedef statement?

    >
    > > int (*a_func)(int, int);
    > > But this is a declaration not a definition.

    >
    > No, this creates a variable, named 'a_func', that can hold a pointer
    > to a function taking two int arguments and returning an int.


    Is it the declaration 'a_func' satisfies the criteria of definition
    when 'a_func' pointes with the properly defined function (as you said)? Or I am missing some simple thing?


    >No 'extern' to see seen in front of it, so it's a definition, not a
    > declaration.


    I dont understand.


    --
    "combination is the heart of chess"

    A.Alekhine

    Mail to:
    sathyashrayan AT gmail DOT com
     
    sathyashrayan, May 28, 2005
    #9
  10. "sathyashrayan" <> writes:
    > <-berlin.de> wrote in message
    > news:...
    >> sathyashrayan <> wrote:
    >>
    >> > "baumann@pan" <> wrote in message
    >> > news:...
    >> >> typedef int (*pfunc)(int , int);
    >> >> pfunc a_func;
    >> >>
    >> >> i know it's ok,
    >> >>
    >> >> but how can define a_func without typedef statement?

    >>
    >> > int (*a_func)(int, int);
    >> > But this is a declaration not a definition.

    >>
    >> No, this creates a variable, named 'a_func', that can hold a pointer
    >> to a function taking two int arguments and returning an int.

    >
    > Is it the declaration 'a_func' satisfies the criteria of definition
    > when 'a_func' pointes with the properly defined function (as you
    > said)? Or I am missing some simple thing?


    It's an object definition (it defines the pointer-to-function object
    "a_func"). It's just not a function definition.

    Roughly speaking, a definition is a declaration that creates the
    entity being declared, whereas a declaration that isn't a definition
    merely declares that the entity exists, but doesn't actually create
    it. (All definitions are declarations.) For example:

    int x; /* a definition; it creates x */
    extern int y; /* not a definition; y is defined elsewhere */
    void foo(void) { printf("Hello\n"); }
    /* a definition of the function "foo" */
    void bar(void); /* not a definition; bar is defined elsewhere */

    Typedefs are a bit odd in that a typedef doesn't actually create a new
    type, merely an alias for an existing type. But a typedef is a
    definition because the thing it creates is the alias, not the type.

    And now we wait for the experts to point out my errors.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 28, 2005
    #10
  11. baumann@pan wrote on 27/05/05 :
    > typedef int (*pfunc)(int , int);
    > pfunc a_func;
    >
    > i know it's ok,


    Yes, and encouraged to have readable sources...

    > but how can define a_func without typedef statement?


    You can :

    int (*pfunc)(int , int);

    by why would you do that except to obscure the code ?

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "C is a sharp tool"
     
    Emmanuel Delahaye, May 28, 2005
    #11
  12. sathyashrayan wrote on 27/05/05 :
    > int (*a_func)(int, int);
    > But this is a declaration not a definition.


    It actually is a defintion of a non initialized pointer.

    extern int (*a_func)(int, int);

    would be a declaration

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    I once asked an expert COBOL programmer, how to
    declare local variables in COBOL, the reply was:
    "what is a local variable?"
     
    Emmanuel Delahaye, May 28, 2005
    #12
  13. sathyashrayan wrote on 28/05/05 :
    >>> int (*a_func)(int, int);


    > Is it the declaration 'a_func' satisfies the criteria of definition
    > when 'a_func' pointes with the properly defined function (as you said)? Or I
    > am missing some simple thing?


    You are making a mixture here.

    /* declaration of x */
    extern int x;

    /* definition of x with an undefined value */
    int x;

    /* definition of x with a defined value */
    int x = 123;

    An unitialized variable has a undefined value, but it occupies a region
    of memory. The fact that the value is undefined doesn't turn the
    variable to be undefined.

    Sounds to be a common mistake...

    >> No 'extern' to see seen in front of it, so it's a definition, not a
    >> declaration.

    >
    > I dont understand.


    That's the point! extern is used to declare a public variable. Read
    your C-book again.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    ..sig under repair
     
    Emmanuel Delahaye, May 28, 2005
    #13
  14. baumann@pan

    pete Guest

    Emmanuel Delahaye wrote:
    >
    > sathyashrayan wrote on 27/05/05 :
    > > int (*a_func)(int, int);
    > > But this is a declaration not a definition.

    >
    > It actually is a defintion of a non initialized pointer.
    >
    > extern int (*a_func)(int, int);
    >
    > would be a declaration


    But the defintion of a non initialized pointer,
    is also a declaration.

    N869
    6.7 Declarations
    Semantics
    [#5]
    A definition of an
    identifier is a declaration for that identifier that:
    -- for an object, causes storage to be reserved for that
    object;
    -- for a function, includes the function body;

    --
    pete
     
    pete, May 28, 2005
    #14
  15. pete wrote on 28/05/05 :
    > Emmanuel Delahaye wrote:
    >>
    >> sathyashrayan wrote on 27/05/05 :
    >>> int (*a_func)(int, int);
    >>> But this is a declaration not a definition.

    >>
    >> It actually is a defintion of a non initialized pointer.
    >>
    >> extern int (*a_func)(int, int);
    >>
    >> would be a declaration

    >
    > But the defintion of a non initialized pointer,
    > is also a declaration.
    >
    > N869
    > 6.7 Declarations
    > Semantics
    > [#5]
    > A definition of an
    > identifier is a declaration for that identifier that:
    > -- for an object, causes storage to be reserved for that
    > object;
    > -- for a function, includes the function body;


    Yes the 'declaration' thing is embedded into the definition.

    But a stand-alone declaration certainely is not a defintion.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "Mal nommer les choses c'est ajouter du malheur au
    monde." -- Albert Camus.
     
    Emmanuel Delahaye, May 28, 2005
    #15
  16. baumann@pan

    Joe Wright Guest

    Keith Thompson wrote:

    [ much snippage ]

    > It's an object definition (it defines the pointer-to-function object
    > "a_func"). It's just not a function definition.
    >
    > Roughly speaking, a definition is a declaration that creates the
    > entity being declared, whereas a declaration that isn't a definition
    > merely declares that the entity exists, but doesn't actually create
    > it. (All definitions are declarations.) For example:
    >
    > int x; /* a definition; it creates x */
    > extern int y; /* not a definition; y is defined elsewhere */
    > void foo(void) { printf("Hello\n"); }
    > /* a definition of the function "foo" */
    > void bar(void); /* not a definition; bar is defined elsewhere */
    >
    > Typedefs are a bit odd in that a typedef doesn't actually create a new
    > type, merely an alias for an existing type. But a typedef is a
    > definition because the thing it creates is the alias, not the type.
    >
    > And now we wait for the experts to point out my errors.
    >


    I suppose a typedef is not a definition. This because it does not create
    an object in memory. It's another case of C overloading the English
    language. Odd, isn't it that '#define X 2' is not a definition either.

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, May 28, 2005
    #16
  17. Joe Wright <> writes:
    > Keith Thompson wrote:

    [snip]
    >> Typedefs are a bit odd in that a typedef doesn't actually create a
    >> new type, merely an alias for an existing type. But a typedef is a
    >> definition because the thing it creates is the alias, not the type.
    >> And now we wait for the experts to point out my errors.
    >>

    >
    > I suppose a typedef is not a definition. This because it does not
    > create an object in memory. It's another case of C overloading the
    > English language. Odd, isn't it that '#define X 2' is not a definition
    > either.


    No, it doesn't create an object in memory. Neither does a function
    definition. A definition, as I understand it, is a declaration that
    creates the named entity, rather than merely referring to an entity
    that's created elsewhere. The entity doesn't have to be an object.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 28, 2005
    #17
  18. baumann@pan

    pete Guest

    Keith Thompson wrote:
    >
    > Joe Wright <> writes:
    > > Keith Thompson wrote:

    > [snip]
    > >> Typedefs are a bit odd in that a typedef doesn't actually create a
    > >> new type, merely an alias for an existing type. But a typedef is a
    > >> definition because the thing it creates is the alias, not the type.
    > >> And now we wait for the experts to point out my errors.
    > >>

    > >
    > > I suppose a typedef is not a definition. This because it does not
    > > create an object in memory. It's another case of C overloading the
    > > English language.
    > > Odd, isn't it that '#define X 2' is not a definition
    > > either.

    >
    > No, it doesn't create an object in memory. Neither does a function
    > definition. A definition, as I understand it, is a declaration that
    > creates the named entity, rather than merely referring to an entity
    > that's created elsewhere. The entity doesn't have to be an object.


    I only know that typedefs and enums are definitions
    because it's been pointed out to me that the standard says so.

    External typedefs and enums,
    which are both declarations and definitions,
    are not definitional enough to be considered as
    "external definitions".

    --
    pete
     
    pete, May 28, 2005
    #18
  19. baumann@pan

    Joe Wright Guest

    Keith Thompson wrote:
    > Joe Wright <> writes:
    >
    >>Keith Thompson wrote:

    >
    > [snip]
    >
    >>>Typedefs are a bit odd in that a typedef doesn't actually create a
    >>>new type, merely an alias for an existing type. But a typedef is a
    >>>definition because the thing it creates is the alias, not the type.
    >>>And now we wait for the experts to point out my errors.
    >>>

    >>
    >>I suppose a typedef is not a definition. This because it does not
    >>create an object in memory. It's another case of C overloading the
    >>English language. Odd, isn't it that '#define X 2' is not a definition
    >>either.

    >
    >
    > No, it doesn't create an object in memory. Neither does a function
    > definition. A definition, as I understand it, is a declaration that
    > creates the named entity, rather than merely referring to an entity
    > that's created elsewhere. The entity doesn't have to be an object.
    >


    Nice side step. In what way is a typedef a declaration creating a named
    entity?

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, May 29, 2005
    #19
  20. baumann@pan

    Joe Wright Guest

    pete wrote:
    > Keith Thompson wrote:
    >
    >>Joe Wright <> writes:
    >>
    >>>Keith Thompson wrote:

    >>
    >>[snip]
    >>
    >>>>Typedefs are a bit odd in that a typedef doesn't actually create a
    >>>>new type, merely an alias for an existing type. But a typedef is a
    >>>>definition because the thing it creates is the alias, not the type.
    >>>>And now we wait for the experts to point out my errors.
    >>>>
    >>>
    >>>I suppose a typedef is not a definition. This because it does not
    >>>create an object in memory. It's another case of C overloading the
    >>>English language.
    >>>Odd, isn't it that '#define X 2' is not a definition
    >>>either.

    >>
    >>No, it doesn't create an object in memory. Neither does a function
    >>definition. A definition, as I understand it, is a declaration that
    >>creates the named entity, rather than merely referring to an entity
    >>that's created elsewhere. The entity doesn't have to be an object.

    >
    >
    > I only know that typedefs and enums are definitions
    > because it's been pointed out to me that the standard says so.
    >
    > External typedefs and enums,
    > which are both declarations and definitions,
    > are not definitional enough to be considered as
    > "external definitions".
    >


    Show me. I have N869 and it makes no reference to typedef reserving
    memory for anything. Of course, its very name suggests 'type definition'
    in the natural language sense of defining 'last name' as 'surname'.

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, May 29, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Piffer

    typdef'ing from sig_atomic_t valid?

    Mark Piffer, Apr 22, 2004, in forum: C Programming
    Replies:
    21
    Views:
    1,904
    Dan Pop
    Apr 29, 2004
  2. Replies:
    5
    Views:
    328
    Keith Thompson
    Mar 12, 2005
  3. robin liu
    Replies:
    3
    Views:
    825
    Robin Liu
    Apr 21, 2006
  4. Johan Tibell

    Typdef pointers to structs or not?

    Johan Tibell, Jul 27, 2006, in forum: C Programming
    Replies:
    17
    Views:
    549
  5. Mark  Snelling

    How do you typdef a function template

    Mark Snelling, Oct 24, 2005, in forum: C++
    Replies:
    14
    Views:
    456
    Kai-Uwe Bux
    Oct 25, 2005
Loading...

Share This Page