What happens when I write struct xx_type {...}

J

Joakim Hove

Hello,

to promote data-hiding I have come across the following technique,
which I quite like:

Header file xxx.h:
------------------
typedef struct xxx_type xxx_type;


xxx_type * xxx_alloc(int);
-------------------------



Source file xxx.c:
-----------------
struct xxx_type {
double *data;
int size;
};

xxx_type * xxx_alloc(int size) {
xxx_type *xxx;
xxx = malloc(sizeof *xxx);
xxx->size = size;
....
return xxx;
}
-------------------------


What I wonder is what really happens at the struct xxx_type {};
statement in the source file. To me it seems like a struct is defined,
but it is not used for anything - i.e. not for a typedef nor to
instantiate an object.

So - I guess I my question is really: "What does the compiler do -
when it meets a

struct foo {
....
....
};

statement.


Best Regards

Joakim Hove

--
Joakim Hove
hove AT ntnu.no /
Tlf: +47 (73 5)9 34 27 / Stabburveien 18
Fax: ................. / N-5231 Paradis
http://www.ift.uib.no/~hove/ / 55 91 28 18 / 92 68 57 04
 
C

CBFalconer

Joakim said:
.... snip ...

So - I guess I my question is really: "What does the compiler do -
when it meets a

struct foo {
....
....
};

statement.

It defines a new type, known as "struct foo", which you can now use
in defining storage, parameters, etc. typedef DOES NOT define a
new type, it simply defines a synonym for the already defined type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top