2
struct abc
{
//some members
}arrayOf[10];
struct def
{
//some data memebers
}obj;

typedef void (*abc) (obj)

I am not sure what does the statement typedef void (*abc) (obj) implies. Please consider that I am new to this.

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
Piyush
  • 65
  • 4
  • 1
    It's a function pointer. `abc` is a type that denotes pointer to a function returning void and taking obj as an argument. Google `C function pointers` for more detailed and correct description. – Violet Giraffe Apr 25 '13 at 17:08
  • 1
    http://stackoverflow.com/questions/89056/how-do-you-read-c-declarations – minopret Apr 25 '13 at 17:11
  • 5
    That shouldn't compile. You're declaring `abc` as a pointer to a function returning nothing, and taking one argument of type `obj`, which isn't a type, so it should complain. – Lee Daniel Crocker Apr 25 '13 at 17:11
  • I'm also perplexed by the `struct abc` followed later by the `typedef abc` -- should this not also be a redeclaration error? Or do you get away with that because it's not a C++ compiler and the `struct abc` is distinguished from the `typedef abc` by the `struct` ?? – K Scott Piel Apr 25 '13 at 17:20
  • 1
    @KScottPiel Struct tags live in a different namespace from ordinary identifiers, so that's no problem (and yes, it's because `struct abc {...}` only introduces a struct tag, and not a type name, as it would in C++). – Daniel Fischer Apr 25 '13 at 17:59
  • I kinda thought so... still makes my eyes bleed. ~smile~ Bad programmer! No moon pie! – K Scott Piel Apr 25 '13 at 18:03

3 Answers3

8

Even ignoring the missing semicolon, it doesn't mean anything; it's an error.

If obj were a type name, it would define abc as an alias for the type void (*)(obj), i.e., as a pointer to a function taking a single argument of type obj and returning void.

But since obj is an object name, not a type name, that's not actually what it means.

When I compile it with gcc, I get a warning:

warning: parameter names (without types) in function declaration

When I compile with -pedantic-errors, the warning becomes a fatal error.

An old-style (non-prototype) function definition can legally specify names, but not types, for arguments:

void old_style_func(obj)
/* obj is implicitly of type int */
{
}

But that form is not valid for a function declaration. In a declaration, a single identifier between the parentheses has to be a type name, not the name of a parameter -- which makes the declaration a prototype.

gcc's error message implies that it's trying to interpret it as if it were the beginning of an old-style definition, and then deciding that it's invalid because it's just a declaration.

The bottom line is that you should pay close attention to compiler warnings.

We could speculate about what was intended. Perhaps obj was meant to be a type name, and this:

struct def
{
    //some data memebers
}obj;

was supposed to be:

typedef struct def
{
    // some data members
} obj;

Or perhaps a type name was accidentally omitted, and the last line should have been:

typedef void (*abc) (struct def obj);

though in that case the name obj is being used twice for different purposes (legal, but confusing). But without more context, it's impossible to be sure how to "fix" it.

Where did this code come from? Are you sure you've shown us the exact code? What happened when you tried to compile it?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
2

it defines the type abc as a pointer to a function that accepts one argument of type int called obj and returns nothing (void).

Sergey L.
  • 21,822
  • 5
  • 49
  • 75
0

It introduces a new name abc for the type of a pointer to a function that takes an obj and returns void. The type of a pointer-to-function is always specified in this roundabout way in a declaration:

return-type (* name-being-declared)(parameter-list)

Incidentally, the * in the declaration is probably unnecessary. Function types always magically turn into pointer-to-function types when you need them (like how an array type magically turns into a pointer-to-element type), and you can use a function type directly (instead of a pointer-to-function type) to declare a new function.

As others have pointed out, the context you give doesn't have a type named obj, only an object named obj. If this code compiles, there must be another type named obj defined somewhere else, in code you haven't given.

Dan Hulme
  • 14,779
  • 3
  • 46
  • 95