linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steve Graegert <graegerts@gmail.com>
To: "leslie.polzer@gmx.net" <leslie.polzer@gmx.net>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Problem with pointer to external function
Date: Fri, 1 Jul 2005 13:17:26 +0200	[thread overview]
Message-ID: <6a00c8d505070104172be85b4c@mail.gmail.com> (raw)
In-Reply-To: <20050701103026.GA2965@paktahn.black.cat>

On 7/1/05, leslie.polzer@gmx.net <leslie.polzer@gmx.net> wrote:
> Hello list,

Hi Leslie

> why do it get the segfault below? What's wrong?

See below...
 
> 
> [sky@paktahn ~/code/cbtest]% cat main.c
> #include <stdio.h>
> 
> typedef void (*callback)();
> 
> extern callback cb;
> 
> int
> main (int argc, char** argv)
> {
>         cb();
> 
>         return(0);
> }
> 

Your code can be corrected easily,  Can you see the difference between
the following two typedefs and their usage?  Both versions work as
expected:

#include <stdio.h>

typedef void callback();
typedef void (*callback2)();

extern callback cb;
extern callback cb2;

int main (int argc, char** argv)
{
	cb();
	(*cb2)();

	return(0);
}

You can simplify your code like this:

extern void cb();
void (*callback)() = cb;

Remember: function names are pointers.  That's why the following two
assignments are equally legal and do the same thing:

int myfunc(int a, int b) { return a + b; }

/* pointer to a function that takes two arguments */
int (*p)(int a, int b) = NULL;
p = myfunc;  /* p points to myfunc now */
p = &myfunc; /* this way is recommended */

I usally encourage people not to combine typdefs and function pointers
because they are often misleading and reduce readability of code.  C
has everything on board to implement and use callbacks without the
need to define new types.

Kind Regards

    \Steve

--

Steve Graegert <graegerts@gmail.com> || <http://www.technologies.de/~sg/>
Independent Software Consultant {C/C++ && Java && .NET}
Mobile: +49 (176)  21 24 88 69
Office: +49 (9131) 71 26 40 9

  parent reply	other threads:[~2005-07-01 11:17 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-01 10:30 Problem with pointer to external function leslie.polzer
2005-07-01 10:54 ` Rechberger Markus
2005-07-01 11:17 ` Steve Graegert [this message]
2005-07-01 16:41 ` Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6a00c8d505070104172be85b4c@mail.gmail.com \
    --to=graegerts@gmail.com \
    --cc=leslie.polzer@gmx.net \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).