problem understanding c code

Discussion in 'C Programming' started by Andreas, Nov 14, 2007.

  1. Andreas

    Andreas Guest

    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
     
    Andreas, Nov 14, 2007
    #1
    1. Advertising

  2. On Nov 14, 11:56 am, Andreas <> wrote:
    > 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".
     
    Spiros Bousbouras, Nov 14, 2007
    #2
    1. Advertising

  3. Andreas

    Andreas Guest

    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:

    > On Nov 14, 11:56 am, Andreas <> wrote:
    >> 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".
     
    Andreas, Nov 14, 2007
    #3
  4. Andreas

    Mark Bluemel Guest

    Spiros Bousbouras wrote:
    > On Nov 14, 11:56 am, Andreas <> wrote:
    >> 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".


    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.
     
    Mark Bluemel, Nov 14, 2007
    #4
  5. Andreas <> writes:
    > Am Wed, 14 Nov 2007 12:03:54 +0000 schrieb Spiros Bousbouras:
    >> On Nov 14, 11:56 am, Andreas <> wrote:
    >>> 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".

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

    --
    Ben.
     
    Ben Bacarisse, Nov 14, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JM
    Replies:
    3
    Views:
    2,185
    Andy Dingley
    Oct 1, 2005
  2. Dhruva Hein

    Help understanding code

    Dhruva Hein, Apr 10, 2005, in forum: Python
    Replies:
    2
    Views:
    346
    Bruno Desthuilliers
    Apr 11, 2005
  3. Fredrik Lundh

    Re: Help understanding code

    Fredrik Lundh, Apr 10, 2005, in forum: Python
    Replies:
    1
    Views:
    337
    Duncan Booth
    Apr 11, 2005
  4. Thomas Ploch
    Replies:
    0
    Views:
    342
    Thomas Ploch
    Nov 17, 2006
  5. yezi

    Code understanding problem

    yezi, Nov 30, 2005, in forum: C Programming
    Replies:
    2
    Views:
    311
    Niklas Norrthon
    Dec 1, 2005
Loading...

Share This Page