Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > What happens when I write struct xx_type {...}

Reply
Thread Tools

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

 
 
Joakim Hove
Guest
Posts: n/a
 
      08-29-2006

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
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      08-29-2006
Joakim Hove wrote:
>

.... 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.

--
Chuck F () ()
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE maineline address!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
What happens when type conversion between signed and unsigned happens? NM C++ 6 09-20-2006 05:39 PM
struct in struct Gunnar G C++ 14 06-02-2004 06:43 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57