1. Computing & Technology

Discuss in my forum

David Bolton

Answer to C Programming Puzzle

By , About.com GuideAugust 29, 2009

Follow me on:

Well yes it compiles and run with the output "d.d = 200". the main function parameter was mistyped but it compiled anyway and ran. Only if you'd used the argv parameters would you have had problems but that was a distraction from the real quirk!

With C (but not C++) struct and enum definitions is that allow the use of tags. These are optional names that give a type name to the struct and are in effect like typedefs. You could declare the struct like this

struct {
  int d;
} d;

which just declares the variable d in place with a int field d. It has no type as such. You wouldn't then need the line

struct d d;

So why would do you need tags? Linked Lists. This isn't particularly useful but it compiles!

struct d2 {
  struct d2 * pd;
};

If a struct has a tag (ie has a named type) then you can declare a pointer to it within the struct.

Comments
No comments yet.  Leave a Comment
Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>
Related Searches c programming puzzle

©2012 About.com. All rights reserved.

A part of The New York Times Company.