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?

Nic
  • 6,211
  • 10
  • 46
  • 69
cjubb39
  • 454
  • 1
  • 5
  • 15
  • Using `typedef` simply defines an alias for a certain type. If that type is a structure, an enumeration or something else doesn't matter. – Some programmer dude Jun 25 '13 at 16:07
  • 3
    @interjayl; but this q is tagged c, so a little odd to refer to c++ title. – Bathsheba Jun 25 '13 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?](http://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c) – interjay Jun 25 '13 at 16:08
  • Also: [typedef struct vs struct definitions](http://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions) – interjay Jun 25 '13 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. – AnT stands with Russia Jun 25 '13 at 17:40

3 Answers3

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;
Community
  • 1
  • 1
dtmland
  • 2,136
  • 4
  • 22
  • 45
  • 4
    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`. – Keith Thompson Jun 27 '13 at 01:07
  • 1
    Agreed. `typedef`'ing a pointer should only be done if the type is truly opaque. – Ed S. Jun 27 '13 at 01:22
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).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 2
    Maybe I wasn't clear enough, but I was asking if there were any other differences. – cjubb39 Jun 25 '13 at 16:10
  • @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 {...} – Bathsheba Jun 25 '13 at 16:14
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.

Community
  • 1
  • 1
macduff
  • 4,655
  • 18
  • 29