

Ask HN: Please explain to me how structs work in C - grover_hartmann

Hi,<p>C newbie here. I&#x27;m trying to dig into C so I can do more things other than just web development, like Linux kernel development or systems development.<p>Rust also looks promising, but I&#x27;d like to learn C first.<p>I already got pointers, or rather, some of it:<p>http:&#x2F;&#x2F;sprunge.us&#x2F;DcWS?c<p>However, I&#x27;m still not sure about structs, anyone please?<p>Book suggestions are also welcome.
======
simonblack
Structs can be looked at like any other collection of data items grouped
together under a specific name. Different languages call the grouping by
different names: so you might find amongst the languages words like:

COBOL - record, SQL - row, Excel - row, C - struct, etc.

If you only know Java, a (very) rough 'n' ready conceptualization might be an
object that has data items, but no methods at all.

A struct pointer will point to the start of each struct only, and incrementing
the pointer will point to the start of the next struct in the array (if one
exists). Within that struct the separate field will be only pointed to by a
'->' thus 'str_pointer->item0'.

Note also that you can use the name itself of the struct (which in reality is
also a pointer, but is usually not considered as such), in this case the item
is selected by using a '.' thus 'str_name.item0'

The biggest problem I had in learning structs was the difference between the
'.' and the '->' syntax. I already knew what a record or a row was.

------
greenyoda
Have you tried looking at "Learn C the Hard Way"? Here's their section on
structs:

[http://c.learncodethehardway.org/book/ex16.html](http://c.learncodethehardway.org/book/ex16.html)

------
grover_hartmann
Thanks.

