[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

strange functions binutils-2.21

Bill Cunningham

7/13/2011 12:03:00 AM

I am looking at the binutils API and some of there function look strange
to me. For one thing they don't seem to want to simply show the prototype of
the functions with their explaination. Here's a link and a few odd functions
maybe someone can help me with. I hope this isn't too OT.

http://sourceware.org/binutils/docs-2.21/bfd/...

typedef void (*bfd_error_handler_type) (const char *, ...);

What does that mean? There's a typedef void. And then
(*bfd_error_handler_type)... is that a cast? The parameter looks like to
take a variable argument list life printf. (const char *,...). What is this
function's return type?

Bill


14 Answers

Ian Collins

7/13/2011 12:08:00 AM

0

On 07/13/11 12:02 PM, Bill Cunningham wrote:
> I am looking at the binutils API and some of there function look strange
> to me. For one thing they don't seem to want to simply show the prototype of
> the functions with their explaination. Here's a link and a few odd functions
> maybe someone can help me with. I hope this isn't too OT.
>
> http://sourceware.org/binutils/docs-2.21/bfd/...
>
> typedef void (*bfd_error_handler_type) (const char *, ...);
>
> What does that mean?

That you are either a) still trolling or b) yet to read a decent C book.

--
Ian Collins

Keith Thompson

7/13/2011 12:15:00 AM

0

"Bill Cunningham" <nospam@nspam.invalid> writes:
> I am looking at the binutils API and some of there function look strange
> to me. For one thing they don't seem to want to simply show the prototype of
> the functions with their explaination. Here's a link and a few odd functions
> maybe someone can help me with. I hope this isn't too OT.
>
> http://sourceware.org/binutils/docs-2.21/bfd/...
>
> typedef void (*bfd_error_handler_type) (const char *, ...);
>
> What does that mean? There's a typedef void. And then
> (*bfd_error_handler_type)... is that a cast? The parameter looks like to
> take a variable argument list life printf. (const char *,...). What is this
> function's return type?

No, it's not a cast; yes, that is a variadic parameter list,
similar to printf's.

It declares bfd_error_handler_type as a typedef for a
pointer-to-function type.

I frankly do not expect you to be able to make any use of this information.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Bill Cunningham

7/13/2011 12:31:00 AM

0

Keith Thompson wrote:

> I frankly do not expect you to be able to make any use of this
> information.

I already know what some of this API does but these functions like
this...How would you call them? I don't think the sockets API has any
functions like that and except for rarer instances I for the most part know
what it's doing designing a socket.

"It declares bfd_error_handler_type as a typedef for a
pointer-to-function type."

So it sounds kind of like a generic function maybe if that's the right
word? Maybe like void * ? CAn you give me an example Keith on how to call
it?

Bill



Bill Cunningham

7/13/2011 12:56:00 AM

0

Instead of a pointer to a function a function to a pointer which is a
type?

Bill


Seebs

7/13/2011 1:23:00 AM

0

On 2011-07-13, Ian Collins <ian-news@hotmail.com> wrote:
> That you are either a) still trolling or b) yet to read a decent C book.

Objection! We can't disprove the theory that he read it and then forgot
everything from it.

And honestly, an API that uses function pointers like that is probably not
the right place for someone who regularly gets stuck on questions comparable
to "this program has mismatched braces, and the compiler says that's a syntax
error, so I tried converting all the header names to uppercase, and now it
gives me a different error".

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

Gene

7/13/2011 1:30:00 AM

0

On Jul 12, 8:30 pm, "Bill Cunningham" <nos...@nspam.invalid> wrote:
> Keith Thompson wrote:
> > I frankly do not expect you to be able to make any use of this
> > information.
>
>     I already know what some of this API does but these functions like
> this...How would you call them? I don't think the sockets API has any
> functions like that and except for rarer instances I for the most part know
> what it's doing designing a socket.
>
> "It declares bfd_error_handler_type as a typedef for a
> pointer-to-function type."
>
>     So it sounds kind of like a generic function maybe if that's the right
> word? Maybe like void * ? CAn you give me an example Keith on how to call
> it?
>
> Bill

Hi Bill,

You'd write a function such as this:

#include <stdio.h>
#include <stdarg.h>

void error_handler(const char *message, ...)
{
va_list ap; // follow the variadic function protocol of stdarg.h
va_start(ap, message);
vfprintf(stderr, message, ap); // print to standard error
va_end(ap);
}

Now in later code you can kake a pointer to this function:

.....
{
bfd_error_handler_type handler;
char *instruction = "Danger, Danger!";

// For example assign the address of the function to handler:
handler = &error_handler;

// Could make decisions to assign different values to handler here.

// Make a call on the handler pointer like this:
(*handler)("Will Robinson: %s\n", instruction);

// But more likely you'll pass the handler to an API function that
// will use it to deal with internal errors. This is known as a
"callback."
api_call(&error_handler);

}

Keith Thompson

7/13/2011 1:34:00 AM

0

"Bill Cunningham" <nospam@nspam.invalid> writes:
> Instead of a pointer to a function a function to a pointer which is a
> type?

What? No.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson

7/13/2011 1:38:00 AM

0

"Bill Cunningham" <nospam@nspam.invalid> writes:
> Keith Thompson wrote:
>> I frankly do not expect you to be able to make any use of this
>> information.
>
> I already know what some of this API does but these functions like
> this...How would you call them? I don't think the sockets API has any
> functions like that and except for rarer instances I for the most part know
> what it's doing designing a socket.
>
> "It declares bfd_error_handler_type as a typedef for a
> pointer-to-function type."
>
> So it sounds kind of like a generic function maybe if that's the right
> word? Maybe like void * ? CAn you give me an example Keith on how to call
> it?

No, there's nothing generic about it. When I wrote that it's a
typedef for a pointer-to-function type, I didn't mean that it's a
typedef for *any* pointer-to-function type. It's for one specific
type. I just didn't bother to go into more detail.

As always, you are in *way* over your head.

Yes, I can give you an example. No, I won't waste my time doing so.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Bill Cunningham

7/13/2011 1:48:00 AM

0

Gene wrote:
> On Jul 12, 8:30 pm, "Bill Cunningham" <nos...@nspam.invalid> wrote:
>> Keith Thompson wrote:
>>> I frankly do not expect you to be able to make any use of this
>>> information.
>>
>> I already know what some of this API does but these functions like
>> this...How would you call them? I don't think the sockets API has any
>> functions like that and except for rarer instances I for the most
>> part know what it's doing designing a socket.
>>
>> "It declares bfd_error_handler_type as a typedef for a
>> pointer-to-function type."
>>
>> So it sounds kind of like a generic function maybe if that's the
>> right word? Maybe like void * ? CAn you give me an example Keith on
>> how to call it?
>>
>> Bill
>
> Hi Bill,
>
> You'd write a function such as this:
>
> #include <stdio.h>
> #include <stdarg.h>
>
> void error_handler(const char *message, ...)
> {
> va_list ap; // follow the variadic function protocol of stdarg.h
> va_start(ap, message);
> vfprintf(stderr, message, ap); // print to standard error
> va_end(ap);
> }
>
> Now in later code you can kake a pointer to this function:
>
> ....
> {
> bfd_error_handler_type handler;
> char *instruction = "Danger, Danger!";
>
> // For example assign the address of the function to handler:
> handler = &error_handler;
>
> // Could make decisions to assign different values to handler here.
>
> // Make a call on the handler pointer like this:
> (*handler)("Will Robinson: %s\n", instruction);
>
> // But more likely you'll pass the handler to an API function that
> // will use it to deal with internal errors. This is known as a
> "callback."
> api_call(&error_handler);
>
> }

I will study this. Callback. What's its purpose in code? I've never
quite seen anything like it in C.

Bill


Bill Cunningham

7/13/2011 7:05:00 PM

0

Gene wrote:

> Hi Bill,
>
> You'd write a function such as this:
>
> #include <stdio.h>
> #include <stdarg.h>
>
> void error_handler(const char *message, ...)
> {
> va_list ap; // follow the variadic function protocol of stdarg.h
> va_start(ap, message);
> vfprintf(stderr, message, ap); // print to standard error
> va_end(ap);
> }
>
> Now in later code you can kake a pointer to this function:
>
> ....
> {
> bfd_error_handler_type handler;
> char *instruction = "Danger, Danger!";
>
> // For example assign the address of the function to handler:
> handler = &error_handler;
>
> // Could make decisions to assign different values to handler here.
>
> // Make a call on the handler pointer like this:
> (*handler)("Will Robinson: %s\n", instruction);
>
> // But more likely you'll pass the handler to an API function that
> // will use it to deal with internal errors. This is known as a
> "callback."
> api_call(&error_handler);
>
> }

I got pissed at kandr2 and put it up for quite some time now. But I must
admit it is very thorough and delves in quite deep a little too quickly for
me. I'm not even sure to be quite honest if it mentions callbacks because I
never got through it and haven't picked it up to look through it thoroughly
for awhile. I might check when I get time. I'm just an amatuer hobbyist with
aspirations. I think I can put a note in an ELF file at SH_NOTE. That's all
I want to do right now.

Bill

PS It's reall not kandr2's fault it's my learning curve.