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


C

Chad

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
 
Ad

Advertisements

O

olaf_giezenaar

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:
 
V

Vladimir S. Oka

Chad said:
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
 
C

Chad

Vladimir said:
Chad said:
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
 
C

Chad

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%
 
O

olaf_giezenaar

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
 
Ad

Advertisements

?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Chad said:
Vladimir said:
Chad said:
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?=

Nils said:
Chad said:
Vladimir said:
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 {
 
C

Chad

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
 
K

Keith Thompson

Chad said:
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".
 
K

Keith Thompson

Vladimir S. Oka said:
Chad said:
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.)
 
Ad

Advertisements

V

Vladimir S. Oka

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


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
 
Ad

Advertisements

N

Nelu

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/
 

Top