problem understanding c code

A

Andreas

hi

i'm going to develope an mp3 encoder and therefor i'm studiing the source
code of lame mp3 encoder. but i'm not able to anderstand the nixt three
lines of the sourde.

struct lame_global_struct;
typedef struct lame_global_struct lame_global_flags;
typedef lame_global_flags *lame_t;

maybe you can tell me how it works.

thank you
andi
 
S

Spiros Bousbouras

hi

i'm going to develope an mp3 encoder and therefor i'm studiing the source
code of lame mp3 encoder. but i'm not able to anderstand the nixt three
lines of the sourde.

struct lame_global_struct;
typedef struct lame_global_struct lame_global_flags;
typedef lame_global_flags *lame_t;

The 2nd line defines "lame_global_flags" to be a name
for the type "struct lame_global_struct" and the 3rd
line defines "lame_t" to be a name for the type "pointer
to lame_global_flags". In other words lame_t is a name
for the type "pointer to struct lame_global_struct".
 
A

Andreas

and the fist line? what means it? - no structure of the datatype is
defined?

struct lame_global_struct;




Am Wed, 14 Nov 2007 12:03:54 +0000 schrieb Spiros Bousbouras:
 
M

Mark Bluemel

Spiros said:
The 2nd line defines "lame_global_flags" to be a name
for the type "struct lame_global_struct" and the 3rd
line defines "lame_t" to be a name for the type "pointer
to lame_global_flags". In other words lame_t is a name
for the type "pointer to struct lame_global_struct".

Which the OP could have found out by opening his C reference text.

I suspect his question is really about the declaration of the "opaque"
structure...

This is a common technique in interface design, when a subsystem needs
to be passed a (pointer to a) structure but we don't want the users of
the subsystem to be aware of its composition.

I would expect users of lame to only really work with lame_t data -
pointers to structures, but we don't know (or care) what the structures
looks like.
 
B

Ben Bacarisse

Andreas said:
Am Wed, 14 Nov 2007 12:03:54 +0000 schrieb Spiros Bousbouras:

and the fist line? what means it? - no structure of the datatype is
defined?

Please don't top post.
struct lame_global_struct;

It says that there is a struct called lame_global_struct and no more.
It is used when the programmer wants to talk about a structure without
saying anything more about it. In the example above, the programmer
wants to define a synonym for the structure type and for a pointer to
it. There is not much else you can do with it which is sometimes the
whole purpose (look up "opaque types").

There is no need to write it out like that.

typedef struct lame_global_struct lame_global_flags;
typedef lame_global_flags *lame_t;

serves the same purpose (in C). The original may have been written
because, to a C++ compiler, a structure tag is a legal type name and
the code may have once have had things like:

some_function(lame_global_struct *sp);

in it once, or simply the author may have adopted that style because
he/she is used to it from C++.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top