TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

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

1 pointsby grover_hartmannover 10 years ago
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.

3 comments

simonblackover 10 years ago
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:<p>COBOL - record, SQL - row, Excel - row, C - struct, etc.<p>If you only know Java, a (very) rough &#x27;n&#x27; ready conceptualization might be an object that has data items, but no methods at all.<p>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 &#x27;-&gt;&#x27; thus &#x27;str_pointer-&gt;item0&#x27;.<p>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 &#x27;.&#x27; thus &#x27;str_name.item0&#x27;<p>The biggest problem I had in learning structs was the difference between the &#x27;.&#x27; and the &#x27;-&gt;&#x27; syntax. I already knew what a record or a row was.
greenyodaover 10 years ago
Have you tried looking at &quot;Learn C the Hard Way&quot;? Here&#x27;s their section on structs:<p><a href="http://c.learncodethehardway.org/book/ex16.html" rel="nofollow">http:&#x2F;&#x2F;c.learncodethehardway.org&#x2F;book&#x2F;ex16.html</a>
grover_hartmannover 10 years ago
Thanks.