News for hackers. Stuff that matters.
Re:Ideas I would like to build / see built by mkramlich on Sunday April 11, 2010 @12:42AM (#1256360)
I'll assume you're right. Thanks for pointing that out. Your C sounds fresher than mine. I used it heavily from 90-95 or so but later moved on to higher languages like Java and Python. If what you point out about the syntax is accurate, I find that to be a counter-intuitive design decision.
Re:Ideas I would like to build / see built by mhansen on Sunday April 11, 2010 @02:08AM (#1256442)
It works because C structs are just contiguous bits of memory, interpreted as various fields. A pointer to a struct points to the start of that contiguous bit of memory.

  Child Struct:
  +---------------+-----------------+
  | parent struct | child fields    |
  +---------------+-----------------+
  ^
  \--- pointers to the struct point at the start
You can cast this child struct to a parent struct, which is like saying 'interpret memory starting here as a 'parent' struct'.

This works: The memory from location to location+sizeof(parent) IS a parent struct. The child fields following the parent struct are simply ignored.

Re:Ideas I would like to build / see built by cperciva on Sunday April 11, 2010 @05:08AM (#1256594)
It works because C structs are just contiguous bits of memory, interpreted as various fields

Well... almost. C structs can have internal padding -- but the C standard doesn't allow padding before the first element, so that first element is guaranteed to begin at the beginning of the struct.

Re:Ideas I would like to build / see built by mhansen on Sunday April 11, 2010 @07:38AM (#1256708)
Thanks! You learn something every day :)