For example, you might create this header file,
"mumbletyfrog.h",
for an abstract type mumbletyfrog:
struct mumbletyfrog;
extern struct mumbletyfrog *makefrog(void);
extern void setcroaks(struct mumbletyfrog *, int);
The caller would include the header file,
and manipulate pointers to struct mumbletyfrog:
#include "mumbletyfrog.h"
...
struct mumbletyfrog *frogp;
frogp = makefrog();
setcroaks(frogp, 3);
croaker(frogp);
The code that defines the mumbletyfrog type,
presumably in mumbletyfrog.c,
actually defines the structure:
#include "mumbletyfrog.h"
struct mumbletyfrog
	{
	char *name;
	int ncroaks;
	};
struct mumbletyfrog *makefrog(void)
{
	struct mumbletyfrog *frogp = malloc(sizeof(struct mumbletyfrog));
	if(frogp == NULL)
		return NULL;
	frogp->name = NULL;
	frogp->ncroaks = 0;
	return frogp;
}
void setcroaks(struct mumbletyfrog *frogp, int n)
{
	frogp->ncroaks = n;
}
void croaker(struct mumbletyfrog *frogp)
{
	int i;
	for(i = 0; i < frogp->ncroaks; i++)
		printf("ribbet\n");
}
Another popular strategy is to use a typedef,
so that the keyword struct can be omitted;
see questions
2.1
and
2.2.
back
about this FAQ list
 
about eskimo
 
search
 
feedback
 
copyright
Hosted by