0

Below code is to implement vtable.

In the below code,

struct A;

typedef struct {
    void (*A)(struct A*);
    void (*update)(struct A*);
    int (*access)(struct A*);
} A_functable;

typedef struct A{
    int a;
    A_functable *vmt;
} A;

I could not understand mentioning (*A) as function pointer in void (*A)(struct A*); that is member in A_functable, where A is

  typedef struct A{
        int a;
        A_functable *vmt;
    } A;

How to understand this syntax?

5
  • 2
    Same way you read the other two function pointers, just that it's named A. Commented Jan 9, 2017 at 5:56
  • 4
    A inside A_functable is completely separate to the typename A you introduce later. To avoid confusion the author should have used different name Commented Jan 9, 2017 at 5:56
  • probably a test / homework question... Commented Jan 9, 2017 at 6:01
  • @Rafael No, it is in continuation with softwareengineering.stackexchange.com/a/339736/131582 Commented Jan 9, 2017 at 6:04
  • Read Chapter 3 ( Unscrambling Declarations in C ) in Expert C Programming Commented Jan 9, 2017 at 6:17

2 Answers 2

2

In

    void (*A)(struct A*);

, the first A does not refer to typedef struct A { ... } A as that is only defined further below. At this point the compiler doesn't know anything about a type called A. A is simply the name of the struct member, just like update and access.

(struct A does refer to the struct, however: There's a struct A; declaration further up.)

Sign up to request clarification or add additional context in comments.

Comments

0

They are in different namespaces.

In C there are four different namespaces

  • Tags for a struct/union/enum
  • Members of struct/union (actually a separate namespace is assigned to each struct/union)
  • Labels
  • Ordinary identifiers.

(Section 6.1.2.3 of C90)

Identifiers in different namespaces will not clash with one other and will be referred as separate entities.

So, in your case,

  • The member of the structure, (*A) being a function pointer is in the second namespace.
  • The tags for the struct typedef struct A is in the first namespace,
  • The struct type being an ordinary identifier is in the fourth namespace.
  • Additionally, the function type for the function pointer (*A)(struct A*) is in the fourth namespace, being an ordinary type.

1 Comment

"object" means a region of storage, not an identifier. Some objects have identifiers associated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.