4

In C, is there any effective difference between declaring a struct as

typedef struct {...} Foo; 

and

struct Foo {...}; 

I know the second requires you to prefix uses with struct, but what are the differences between these two definitions that I'll notice when writing or executing the program? What about with enums?

5
  • Using typedef simply defines an alias for a certain type. If that type is a structure, an enumeration or something else doesn't matter. Commented Jun 25, 2013 at 16:07
  • 3
    @interjayl; but this q is tagged c, so a little odd to refer to c++ title. Commented Jun 25, 2013 at 16:07
  • 1
    Oops, I meant to mark as duplicate of this one which is C: Why should we typedef a struct so often in C? Commented Jun 25, 2013 at 16:08
  • Also: typedef struct vs struct definitions Commented Jun 25, 2013 at 16:10
  • There's no way to say what this question is about. Is this about typedef without a typedef-name? Is this about the purpose of typedef in general? Is this about nameless struct types? There's no way to say whether the OP's examples are built that way deliberately or by mistake. Please, clarify the question. Commented Jun 25, 2013 at 17:40

3 Answers 3

5

Update: please see comments attached to answer for clarification.

Original post.

Besides having to write "struct" everywhere, something else of note is that using a typedef will allow you to avoid subtle syntax errors when working with pointers:

Quote:

Typedefs can also simplify declarations for pointer types. Consider this:

struct Node { int data; struct Node *nextptr; }; 

Using typedef, the above code can be rewritten like this:

typedef struct Node Node; struct Node { int data; Node *nextptr; }; 

In C, one can declare multiple variables of the same type in a single statement, even mixing pointer and non-pointers. However, one would need to prefix an asterisk to each variable to designate it as a pointer. In the following, a programmer might assume that errptr was indeed a Node *, but a typographical error means that errptr is a Node. This can lead to subtle syntax errors.

struct Node *startptr, *endptr, *curptr, *prevptr, errptr, *refptr; 

By defining a Node * typedef, it is assured that all the variables will be pointer types.

typedef struct Node *NodePtr; ... NodePtr startptr, endptr, curptr, prevptr, errptr, refptr; 
Sign up to request clarification or add additional context in comments.

2 Comments

That last is an advantage of using typedef for a pointer type, not for a structure type. Typedefs for pointer types are often considered to be poor style, the rationale being that pointers are so integral to C that hiding somethings "pointerness" can cause confusion. The multiple declaration problem can be avoided by declaring just one object per line (or by being very careful). And the error will probably be detected the first time you try to use errptr.
Agreed. typedef'ing a pointer should only be done if the type is truly opaque.
0

If you write

typedef struct {...} foo;

It saves you from having to write struct foo everywhere: you can just write foo.

(You get this notational convenience for free in C++ by the way).

2 Comments

Maybe I wasn't clear enough, but I was asking if there were any other differences.
@interjay's reference is comprehensive enough to understand this full; but in summary there is no difference unless you're exploiting a typedef to create an opaque type - which you're not since you have {...}
0

I would look at this SO question and then summarize that there is no appreciable functional difference between struct { ... } and typedef struct { ... } although the latter may make your code less cumbersome and easier to understand if used correctly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.