Response to your question in comment: (also read HERE for great discussion on extern usage.)
Declaring struct like this, then extern'ing a copy of it (or any variable for that matter) allows it to contain the same value, and be seen with the same value in all files that use it.
In .h
typedef struct {
int a;
char *b;
}A:
//A has just been created as a new type, i.e. struct A, so it now conforms to the same rules other variable conform to with regards to the use of extern. So...
extern A a, *pA;//create a global copy of A with extern modifier
Then in file1.c
A a, *pA; //copy of externed variable
int main(void)
{
pA = &a;//initialize pA here, use everywhere in file;
return 0;
}
Then in file2.c
A a, *pA;
[EDIT] My original answer is untouched, and does answer a very specific question by OP: in clarification of my comment:
Is it not just as easy to do something like typedef struct {...} A; extern A a, *pA; in your header file, then A a, *pA; in each .c module you need to use them? More readable for people who have to maintain the code later I would think
OP asked:
@ryyker: can you clarify that extern example you posted? it's confusing me because structs don't have linkage in c, but all variables and functions do.
The above request is answered above. OP accepted the answer. This edit is in response to @Jim Balter's aggressive complaints/claims about my answers validity. The new code has been cleaned up and tested to illustrate the use of creating and using a project visible variable, using the extern modifier. The following code example will demonstrate:
header.h
int somefunc(void);
typedef struct {
int num;
char *b;
}A;
//A has just been created as a new type, i.e. struct A, so it now
//conforms to the same rules other variable conform to with
//regards to the use of extern. So...
extern A a, *pA;//create a global copy of A with extern modifier
file1.c
#include <ansi_c.h>
#include "header.h"
A a, *pA;
int main(void)
{
pA = &a;//initialize pA here, use everywhere in file;
pA->num = 45;
pA->b = malloc(strlen("value of pA->b")+1);
somefunc();
printf("value of pA->b is: %s\n", pA->b);
getchar();
free(pA->b);
return 0;
}
file2.c
#include <ansi_c.h>
#include "header.h"
A a, *pA; //Note: optional presence of A in this file.
//It is not Not necessary to place this line,
//but for readability (future maintainers of code
//I always include it in each module in which it is used.
int somefunc(void)
{
printf("%d\n", pA->num);//value of pN->num same as in file1.c
strcpy(pA->b, "value of pA->b");
return 0;
}
Building and running these files will demonstrate the project visibility of the A struct created
header.h, and used in both file1.c and file2.c (and as many .c files as are needed)
It also substantiates the claims I made in my original answer.