Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question from the 2nd edition of "The C Programming Language"

Reply
Thread Tools

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

 
 
Chad
Guest
Posts: n/a
 
      02-01-2006
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

 
Reply With Quote
 
 
 
 
olaf_giezenaar@hotmail.com
Guest
Posts: n/a
 
      02-01-2006
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


 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      02-01-2006
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

 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      02-01-2006

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

 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      02-01-2006

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

 
Reply With Quote
 
olaf_giezenaar@hotmail.com
Guest
Posts: n/a
 
      02-01-2006
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

 
Reply With Quote
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      02-01-2006
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.)
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      02-01-2006
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;
>

 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      02-01-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-01-2006
"Chad" <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
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
2nd Call for Papers | Extended deadline: April 07 |CENTERIS'2009 - Conference on ENTERprise Information Systems | 2nd Callfor Papers CENTERIS'2009 - Conference on ENTERprise Information Systems Python 0 03-16-2009 01:59 PM
C Programming: A Modern Approach 2nd Edition Scott W C Programming 14 07-18-2006 12:08 AM
Review: Icemat 2nd Edition Silverstrand Reviews & How-To's 37 10-19-2005 08:08 AM
1st edition vs. 2nd edition book for 70-215? Kian Wah Lai MCSA 0 07-09-2003 01:40 AM
Re: 1st edition vs. 2nd edition book for 70-215? Diana Myers MCSA 0 07-08-2003 11:32 PM



Advertisments