C programming abstraction - typedef external declaration

I am programming with abstraction of the data types. This means in my header files I declare my struct pointers and their related functions like so:

application.h

    typedef struct APPLICATION_Context *APPLICATION_Context;

    extern APPLICATION_Context appContextConstruct;

    extern void appContextAddMember(int member);

and in the source file:

application.c

    #include "application.h"

    struct APPLICATION_Context{
        int member0;
        int member1;
        int member2;
    };

    extern APPLICATION_Context appContextConstruct;

Two questions from this set-up:

  1. Why, in the header, do I not have to declare the typedef'd struct as extern? It should also be visible outside the header!

  2. Do I need to repeat the 'extern' keyword in the source file?

Answers


why, in the header, do I not have to declare the typedef'd struct as extern? it should also be visible outside the header!

A typedef is visible to any file including your header. Don't mix up typedef with variable declarations.

In fact, you shouldn't need to use extern at all, it is a keyword that should be avoided, since it means you are using global variables, which is poor design.

extern in front of function prototypes is superfluous.

Also, never hide pointers behind typedefs.


Need Your Help

Regex greedy parse direction

java javascript python regex perl

I found there're two different opinions about how greedy regex is executed:

Using nhibernate <loader> element with HQL queries

c# nhibernate nhibernate-mapping hql

I'm attempting to use an HQL query in a &lt;loader&gt; element to load an entity based on other entities.