Question from the 2nd edition of "The C Programming Language"

Discussion in 'C Programming' started by Chad, Feb 1, 2006.

  1. Chad

    Chad Guest

    On page 180 from the 2nd edition of "The C Programming Language" by
    Kernighan and Richtie, they use the following:

    #define NAME_MAX 14

    typedef struct {
    long ino;
    char name[NAME_MAX+1];
    } Dirent;

    typedef struct {
    int fd;
    Dirent d;
    } DIR;

    What is the reasoning behind typedefing these structures?

    Chad
    Chad, Feb 1, 2006
    #1
    1. Advertising

  2. Chad

    Guest

    Hello

    The reason is that after a typedef you can use the struct as if it is a
    type.

    whithout typedef you have to write tom declare

    struct Dirent someName;

    with this is simplified to

    Dirent someName;

    So it is easy to use typedef for structures you are going to use more
    times.


    Chad schreef:

    > On page 180 from the 2nd edition of "The C Programming Language" by
    > Kernighan and Richtie, they use the following:
    >
    > #define NAME_MAX 14
    >
    > typedef struct {
    > long ino;
    > char name[NAME_MAX+1];
    > } Dirent;
    >
    > typedef struct {
    > int fd;
    > Dirent d;
    > } DIR;
    >
    > What is the reasoning behind typedefing these structures?
    >
    > Chad
    , Feb 1, 2006
    #2
    1. Advertising

  3. Chad wrote:
    > On page 180 from the 2nd edition of "The C Programming Language" by
    > Kernighan and Richtie, they use the following:
    >
    > #define NAME_MAX 14
    >
    > typedef struct {
    > long ino;
    > char name[NAME_MAX+1];
    > } Dirent;
    >
    > typedef struct {
    > int fd;
    > Dirent d;
    > } DIR;
    >
    > What is the reasoning behind typedefing these structures?


    Because they are new data types, and that's what `typedef` is there
    for. Because it's convenient to have them as data type for the code
    that follows?

    Immediatelly after the above, a few utility functions are prototyped,
    all of them using the new types in one way or another, as data types:

    DIR *opendir(char *dirname);
    Dirent *readdir(DIR *dfd);
    void closedir(DIR *dfd);

    In the code that follows, new types are again heavily used (e.g.
    dirwalk( ) uses them internally as well). For enhanced readability, and
    sheer ease of typing, it was worth `typedef`-ing them, IMHO.

    Cheers

    Vladimir
    Vladimir S. Oka, Feb 1, 2006
    #3
  4. Chad

    Chad Guest

    Vladimir S. Oka wrote:
    > Chad wrote:
    > > On page 180 from the 2nd edition of "The C Programming Language" by
    > > Kernighan and Richtie, they use the following:
    > >
    > > #define NAME_MAX 14
    > >
    > > typedef struct {
    > > long ino;
    > > char name[NAME_MAX+1];
    > > } Dirent;
    > >
    > > typedef struct {
    > > int fd;
    > > Dirent d;
    > > } DIR;
    > >
    > > What is the reasoning behind typedefing these structures?

    >
    > Because they are new data types, and that's what `typedef` is there
    > for. Because it's convenient to have them as data type for the code
    > that follows?
    >
    > Immediatelly after the above, a few utility functions are prototyped,
    > all of them using the new types in one way or another, as data types:
    >
    > DIR *opendir(char *dirname);
    > Dirent *readdir(DIR *dfd);
    > void closedir(DIR *dfd);
    >
    > In the code that follows, new types are again heavily used (e.g.
    > dirwalk( ) uses them internally as well). For enhanced readability, and
    > sheer ease of typing, it was worth `typedef`-ing them, IMHO.
    >
    > Cheers
    >
    > Vladimir


    I think what I am (was?) really drawing a blank is why don't they go
    something like:

    struct internal_name {
    int fd;
    Dirent d;
    } DIR;

    Yes, I'm this boneheaded. I need at least 24 hours for this to sink in.

    Thanks
    Chad
    Chad, Feb 1, 2006
    #4
  5. Chad

    Chad Guest


    > I think what I am (was?) really drawing a blank is why don't they go
    > something like:
    >
    > struct internal_name {
    > int fd;
    > Dirent d;
    > } DIR;
    >
    > Yes, I'm this boneheaded. I need at least 24 hours for this to sink in.
    >
    > Thanks
    > Chad


    I think I just answered myself

    ------------------kr.c--------------------------------------------
    #include <stdio.h>

    struct DIR{
    int fd;
    char d;
    };

    DIR *opendir(char *getname);

    int main(void) {
    return 0;
    }
    ----------------------------------------------------------------------------

    m-net% gcc -Wall kr.c -o kr
    kr.c:8: syntax error before `*'
    kr.c:8: warning: type defaults to `int' in declaration of `opendir'
    kr.c:8: warning: data definition has no type or storage class
    m-net%
    Chad, Feb 1, 2006
    #5
  6. Chad

    Guest

    Hello

    It will work if You set

    typedef struct {
    int fd;
    char d;
    } DIR;

    A new type is created

    What you are doing is creating a struct. not a type of that struct.

    It is also posible to do it in 2 steps

    struct DIR{
    int fd;
    char d;
    };

    typedef struct DIR DIRtype;


    Greetings Olaf
    , Feb 1, 2006
    #6
  7. Chad wrote:
    > Vladimir S. Oka wrote:
    >> Chad wrote:
    >>> On page 180 from the 2nd edition of "The C Programming Language" by
    >>> Kernighan and Richtie, they use the following:
    >>>
    >>> #define NAME_MAX 14
    >>>
    >>> typedef struct {
    >>> long ino;
    >>> char name[NAME_MAX+1];
    >>> } Dirent;
    >>>
    >>> typedef struct {
    >>> int fd;
    >>> Dirent d;
    >>> } DIR;
    >>>
    >>> What is the reasoning behind typedefing these structures?

    >> Because they are new data types, and that's what `typedef` is there
    >> for. Because it's convenient to have them as data type for the code
    >> that follows?
    >>
    >> Immediatelly after the above, a few utility functions are prototyped,
    >> all of them using the new types in one way or another, as data types:
    >>
    >> DIR *opendir(char *dirname);
    >> Dirent *readdir(DIR *dfd);
    >> void closedir(DIR *dfd);
    >>
    >> In the code that follows, new types are again heavily used (e.g.
    >> dirwalk( ) uses them internally as well). For enhanced readability, and
    >> sheer ease of typing, it was worth `typedef`-ing them, IMHO.
    >>
    >> Cheers
    >>
    >> Vladimir

    >
    > I think what I am (was?) really drawing a blank is why don't they go
    > something like:
    >


    Better to think of typedef as just another name for a thing.
    typedef esablishes an alias for another type, so you can refer to it
    by that name.

    struct{
    int fd;
    Dirent d;
    } DIR;

    Establishes the name 'DIR' for the (unnamed) struct that is defined.

    typedef struct internal_name {
    int fd;
    Dirent d;
    } DIR;

    or

    struct internal_name {
    int fd;
    Dirent d;
    };
    typedef struct internal_name DIR;

    Makes 'struct internal_name' and 'DIR' the name of the (same) struct type.

    (So I guess they just were "lazy" and didn't want to type
    'struct DIR' all the time, but rather just 'DIR' and since they never
    needed the 'struct DIR' (or in your case 'struct internal_name') they
    never esablished that name either.)
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Feb 1, 2006
    #7
  8. Nils O. SelÄsdal wrote:
    > Chad wrote:
    >> Vladimir S. Oka wrote:
    >>> Chad wrote:
    >>>> On page 180 from the 2nd edition of "The C Programming Language" by
    >>>> Kernighan and Richtie, they use the following:
    >>>>
    >>>> #define NAME_MAX 14
    >>>>
    >>>> typedef struct {
    >>>> long ino;
    >>>> char name[NAME_MAX+1];
    >>>> } Dirent;
    >>>>
    >>>> typedef struct {
    >>>> int fd;
    >>>> Dirent d;
    >>>> } DIR;
    >>>>
    >>>> What is the reasoning behind typedefing these structures?
    >>> Because they are new data types, and that's what `typedef` is there
    >>> for. Because it's convenient to have them as data type for the code
    >>> that follows?
    >>>
    >>> Immediatelly after the above, a few utility functions are prototyped,
    >>> all of them using the new types in one way or another, as data types:
    >>>
    >>> DIR *opendir(char *dirname);
    >>> Dirent *readdir(DIR *dfd);
    >>> void closedir(DIR *dfd);
    >>>
    >>> In the code that follows, new types are again heavily used (e.g.
    >>> dirwalk( ) uses them internally as well). For enhanced readability, and
    >>> sheer ease of typing, it was worth `typedef`-ing them, IMHO.
    >>>
    >>> Cheers
    >>>
    >>> Vladimir

    >>
    >> I think what I am (was?) really drawing a blank is why don't they go
    >> something like:
    >>

    >
    > Better to think of typedef as just another name for a thing.
    > typedef esablishes an alias for another type, so you can refer to it
    > by that name.
    >
    > struct{

    Sorry, the above line should naturally read
    typedef struct {

    > int fd;
    > Dirent d;
    > } DIR;
    >
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Feb 1, 2006
    #8
  9. Chad

    Chad Guest

    wrote:
    > Hello
    >
    > It will work if You set
    >
    > typedef struct {
    > int fd;
    > char d;
    > } DIR;
    >
    > A new type is created
    >
    > What you are doing is creating a struct. not a type of that struct.
    >
    > It is also posible to do it in 2 steps
    >
    > struct DIR{
    > int fd;
    > char d;
    > };
    >
    > typedef struct DIR DIRtype;
    >
    >
    > Greetings Olaf


    Okay, I was always under the impression that something like

    struct dir{
    int fd;
    char d;

    }DIR;

    created a type called struct

    Chad
    Chad, Feb 1, 2006
    #9
  10. "Chad" <> writes:
    [...]
    > Okay, I was always under the impression that something like
    >
    > struct dir{
    > int fd;
    > char d;
    >
    > }DIR;
    >
    > created a type called struct


    That creates a type called "struct dir", and an object of that type
    called "DIR".

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Feb 1, 2006
    #10
  11. "Vladimir S. Oka" <> writes:
    > Chad wrote:
    >> On page 180 from the 2nd edition of "The C Programming Language" by
    >> Kernighan and Richtie, they use the following:
    >>
    >> #define NAME_MAX 14
    >>
    >> typedef struct {
    >> long ino;
    >> char name[NAME_MAX+1];
    >> } Dirent;
    >>
    >> typedef struct {
    >> int fd;
    >> Dirent d;
    >> } DIR;
    >>
    >> What is the reasoning behind typedefing these structures?

    >
    > Because they are new data types, and that's what `typedef` is there
    > for. Because it's convenient to have them as data type for the code
    > that follows?


    The struct declaration creates a new type. The typedef only creates
    an alias for that type.

    In my opinion, there's no real advantage in using "typedef" here at
    all. I would have written the declarations as:

    #define NAME_MAX 14

    struct Dirent {
    long ino;
    char name[NAME_MAX+1];
    };

    struct DIR {
    int fd;
    struct Dirent d;
    };

    Using a typedef allows code that uses the type to refer to it with
    just a single identifier. This hides the fact that it's a struct.
    This can be useful in some cases, but not when the code refers to
    members of the struct.

    > Immediatelly after the above, a few utility functions are prototyped,
    > all of them using the new types in one way or another, as data types:
    >
    > DIR *opendir(char *dirname);
    > Dirent *readdir(DIR *dfd);
    > void closedir(DIR *dfd);


    struct DIR *opendir(char *dirname);
    struct Dirent *readdir(struct DIR *dfd);
    void closedir(struct DIR *dfd);

    > In the code that follows, new types are again heavily used (e.g.
    > dirwalk( ) uses them internally as well). For enhanced readability, and
    > sheer ease of typing, it was worth `typedef`-ing them, IMHO.


    IMHO, it isn't. Ease of typing is a trivial concern; code will be
    read more often than it's written. The language gives us an easy way
    to make it obvious that a given type is a struct; we might as well use
    it rather than hiding it behind a typedef (unless, of course, there's
    a good reason to hide it).

    (Yes, I'm disagreeing with K&R, which means I'm running a considerable
    risk of being wrong. It's a risk I'm willing to take.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Feb 1, 2006
    #11
  12. Keith Thompson wrote:

    > "Vladimir S. Oka" <> writes:
    >> Chad wrote:
    >>> On page 180 from the 2nd edition of "The C Programming Language" by
    >>> Kernighan and Richtie, they use the following:


    < snip >

    >> DIR *opendir(char *dirname);
    >> Dirent *readdir(DIR *dfd);
    >> void closedir(DIR *dfd);

    >
    > struct DIR *opendir(char *dirname);
    > struct Dirent *readdir(struct DIR *dfd);
    > void closedir(struct DIR *dfd);
    >
    >> In the code that follows, new types are again heavily used (e.g.
    >> dirwalk( ) uses them internally as well). For enhanced readability,
    >> and sheer ease of typing, it was worth `typedef`-ing them, IMHO.

    >
    > IMHO, it isn't. Ease of typing is a trivial concern; code will be
    > read more often than it's written. The language gives us an easy way
    > to make it obvious that a given type is a struct; we might as well use
    > it rather than hiding it behind a typedef (unless, of course, there's
    > a good reason to hide it).


    I agree about the typing. When I was originally composing my post I
    hesitated a bit before including that comment, but decided to leave it
    in for completeness.

    For the other one, it's almost a matter of taste, and possibly
    background. My first programming language was Pascal (I don't count
    BASIC ;-) ), where once a type is created it's indistinguishable from
    the built-in ones.

    Another argument may be that, knowing something is a struct doesn't
    necessarily save you looking for it's definition (what are the members,
    apart from the ones you see referenced in front of you?). You'd
    essentially have to do the same for all typedefs with non-obvious names
    (and even those are always suspect).

    Yet another is: if you're creating a new type -- spell it out. It's
    marginally easier, and possibly less error-prone to grep for typedefs,
    than for typedefs and structs, and then have to sort out which are
    defining new types, and which are not.

    > (Yes, I'm disagreeing with K&R, which means I'm running a considerable
    > risk of being wrong. It's a risk I'm willing to take.)


    Nothing wrong with either. ;-)

    In the end, I believe this is a matter to be decided when writing a
    coding standard for any particular shop, and then be done with it.

    Cheers

    Vladimir

    --
    The economy depends about as much on economists as the weather does on
    weather forecasters.
    -- Jean-Paul Kauffmann
    Vladimir S. Oka, Feb 2, 2006
    #12
  13. Chad

    Nelu Guest

    wrote:
    > Hello
    >
    > It will work if You set
    >
    > typedef struct {
    > int fd;
    > char d;
    > } DIR;
    >
    > A new type is created
    >
    > What you are doing is creating a struct. not a type of that struct.
    >
    > It is also posible to do it in 2 steps
    >
    > struct DIR{
    > int fd;
    > char d;
    > };
    >
    > typedef struct DIR DIRtype;
    >

    Please quote some context and do not top-post.
    Read http://cfaj.freeshell.org/google/



    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)
    Nelu, Feb 3, 2006
    #13
    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. pete
    Replies:
    12
    Views:
    1,264
    Mark McIntyre
    Aug 30, 2003
  2. DaLoverhino
    Replies:
    0
    Views:
    329
    DaLoverhino
    Oct 3, 2006
  3. E. Robert Tisdale

    The C Programming Language: Third Edition

    E. Robert Tisdale, Feb 24, 2005, in forum: C Programming
    Replies:
    4
    Views:
    315
    Michael Mair
    Feb 25, 2005
  4. Replies:
    57
    Views:
    1,666
  5. Christos Kokaliaris
    Replies:
    45
    Views:
    289
    Jorgen Grahn
    Jun 16, 2014
Loading...

Share This Page