rablentain
rablentain

Reputation: 6715

How does this typedef works?

I saw this line of code and I do not understand what is typedef:ed where...

typedef void *(*SomeType)(OtherType mem, thirdtype size);

So, how do I use this?

Upvotes: 0

Views: 114

Answers (4)

MohitSingh
MohitSingh

Reputation: 73

Found this useful link once when i was facing the problem in reading complex c declarations. Might help you.

How to read complex C deaclaration

Upvotes: 0

This is a typedef of a function pointer.

typedef void *(*SomeType)(OtherType mem, thirdtype size);

void* func (OtherType mem, thirdtype size) {}

Which can then be used like this:

SomeType fptr = &func;

fptr(someMem, someSize);

Or can itself be passed as a parameter to a function

void memory_visitor (SomeType visit_cb)
{
  ...
  if (visit_cb)
    visit_cb (visistedMem, visistedMemSize);
}

Upvotes: 5

mouviciel
mouviciel

Reputation: 67831

There is a web site for that: C gibberish ↔ English, aka cdecl

To the question: void *(*SomeType)(OtherType, thirdtype),

its answer is:

declare SomeType as pointer to function (OtherType, thirdtype) returning pointer to void

cdecl is a great way for learning C declaration system.

Upvotes: 2

user1814023
user1814023

Reputation:

SomeType is a pointer to function which accepts OtherType and thirdtype as arguments and returns void *

Upvotes: 0

Related Questions