Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

I don't understand this syntax for typedef and structs

47 views
Skip to first unread message

ag...@drrob1.com

unread,
Dec 15, 2014, 5:45:32 PM12/15/14
to
Hi. I'm new to c and c++. I am coming from the world of Modula-2 and
Ada and I'm trying to understand the following syntax

typedef struct _win_border_struct
{
chtype ls,rs,ts,bs,tl,tr,bl,br;
} WIN_BORDER;

typedef struct _WIN_struct
{
int startx, starty, height, width;
WIN_BORDER border;
} WIN;

I am conceptualizing a struct as a RECORD type in Modula-2 and Ada.
Typedef is the modula-2 equivalent of TYPE identifier =

From reading what I could, I understand that the identifier that
follows struct is the name of the type, and the identifier that
follows the closing } is the name of a variable/object of this type.

But the 2nd typedef struct does not make reference to a
_win_border_struct, but instead uses the WIN_BORDER identifier.

So my question is, what is the difference between the identifier that
follows struct and the one that follows the closing }?

Thx
Rob

Ian Collins

unread,
Dec 15, 2014, 6:05:31 PM12/15/14
to
ag...@drrob1.com wrote:
> Hi. I'm new to c and c++.

Which one are you intending to use? The distinction is important.

> I am coming from the world of Modula-2 and
> Ada and I'm trying to understand the following syntax
>
> typedef struct _win_border_struct
> {
> chtype ls,rs,ts,bs,tl,tr,bl,br;
> } WIN_BORDER;

In C++, the typedef is unnecessary clutter, just write

struct WIN_BORDER
{
chtype ls,rs,ts,bs,tl,tr,bl,br;
};

Note many coding standards reserve all cast for macro names, not
class/struct names.

In C, things are somewhat more complex, struct declarations live in
their own pseudo namespace and a typedef is required to map them into
the "global" namespace (C doesn't actually have namespaces, but this is
the easiest way to describe the rules).

If you were to use your original struct definition above, you would need
to explicitly name WIN_BORDER as a struct:

struct _win_border_struct border;

Adding the typedef allows you to write

WIN_BORDER border;

The "typedef struct" syntax is a shorthand for

struct _win_border_struct
{
chtype ls,rs,ts,bs,tl,tr,bl,br;
};

typedef struct _win_border_struct WIN_BORDER;


--
Ian Collins

Christopher Pisz

unread,
Dec 15, 2014, 6:17:51 PM12/15/14
to
You will often see C programmers write this in C++ projects:
typedef struct _win_border_struct
{
chtype ls,rs,ts,bs,tl,tr,bl,br;
} WIN_BORDER;

because they failed to make the distinction between the two. Don't be
one of those. Make a distinction between writing C and writing C++. This
is one of the differences between the two.

If you are using a C++ compiler then write:

struct WIN_BORDER
{
chtype ls,rs,ts,bs,tl,tr,bl,br;
};

You will also see the former in libraries written by C programmers. Like
the Windows API, because it is largely C and began long long ago.

As to the reason that was needed in C and not in C++ give a read at:
the section titled "More Peculiarities from Unification" on
http://www.drdobbs.com/c-theory-and-practice/184403396?pgno=3



Barry Schwarz

unread,
Dec 15, 2014, 7:03:00 PM12/15/14
to
On Mon, 15 Dec 2014 17:45:09 -0500, ag...@drrob1.com wrote:

>Hi. I'm new to c and c++. I am coming from the world of Modula-2 and
>Ada and I'm trying to understand the following syntax

Trying to learn both languages at the same time can be confusing. The
languages have just enough similarity to lead newcomers in the wrong
direction when trying to use a construct from one in the other.

Pick one language. Get comfortable with it. Then tackle the other.

--
Remove del for email

Ian Collins

unread,
Dec 15, 2014, 8:01:23 PM12/15/14
to
Ian Collins wrote:
>
> Note many coding standards reserve all cast for macro names, not
> class/struct names.

That should have said "reserve all caps"...

--
Ian Collins

Robert Wessel

unread,
Dec 15, 2014, 8:51:36 PM12/15/14
to
On Mon, 15 Dec 2014 17:45:09 -0500, ag...@drrob1.com wrote:

If you had just a stand alone struct definition:

struct struct_name {int a; int b;} struct_instance;

The name after the closing brace is, in fact, creating and naming of
an instance of the struct. In this example, there is a structure type
named struct_name defined, and an instance of struct_name called
struct_instance is created.

In a typedef:

typedef struct struct_name {int a; int b;} typedef_name;

You cannot create an actual instance of the structure, and the name
after the closing brace is the name of the new type. So in the above
typedef_name is an alias for "struct struct_name".

Now that will work in both C and C++, but in C++ the struct defintion
automatically creates a typename (the same as the structure name). So
in either case, in C++, a type named struct_name will be created (and
in the second example typedef_name will be created as well).

In C++ you would usually avoid the explicit typedef.

ag...@drrob1.com

unread,
Dec 15, 2014, 9:18:00 PM12/15/14
to
That helps.

I am interested in learning c++ first, and then maybe or maybe not, c.

Thanks guys.
0 new messages