{"id":3920,"date":"2015-01-05T12:38:45","date_gmt":"2015-01-05T20:38:45","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=3920"},"modified":"2024-12-28T11:31:40","modified_gmt":"2024-12-28T19:31:40","slug":"what-is-a-struct","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/what-is-a-struct\/","title":{"rendered":"What Is a Struct in Programming?"},"content":{"rendered":"<p><em>Structures<\/em>\u00a0are a group of related variables that are placed under one name. Unlike arrays, structures are not limited to one data type. The <em>struct<\/em> keyword will allow us to create a structure.<br \/>\n<!--more--><br \/>\n<!--more--><\/p>\n<p>Below us is a brief example of a struct, with us printing out the information we assigned the struct to.<\/p>\n<p><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/a1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3937\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/a1.png\" alt=\"a\" width=\"370\" height=\"441\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/a1.png 370w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/a1-188x225.png 188w\" sizes=\"auto, (max-width: 370px) 100vw, 370px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/struct.txt\">Struct Code<\/a><\/p>\n<p><!--more--><br \/>\n<!--more--><\/p>\n<p>The keyword <em>typedef<\/em> allows us to create an &#8220;alias&#8221; of a previously defined data type. The capitalized &#8220;Animal&#8221; at the end of our struct allows us to use &#8220;Animal&#8221; as a variable declaration.<br \/>\n<!--more--><br \/>\n<!--more--><br \/>\nThe &#8220;.&#8221;, known as the dot operator, is a structure member operator. When we create a struct, we the dot operator to access the information inside the struct.<\/p>\n<p>We can make arrays out of structs too! The below code has an entire array of the Animal data type.<\/p>\n<p><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-3952\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b2-452x600.png\" alt=\"b\" width=\"452\" height=\"600\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b2-452x600.png 452w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b2-169x225.png 169w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b2.png 498w\" sizes=\"auto, (max-width: 452px) 100vw, 452px\" \/><\/a><br \/>\n<a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/zoo.txt\">Animal Array Code<\/a><br \/>\n<!--more--><br \/>\n<!--more--><\/p>\n<p>Just like other data types, structs can be created as pointers. This is used for linked lists, which will be discussed in a later blog post. When we use struct pointers, instead of the dot operator, we use &#8220;-&gt;&#8221;, which is known as the arrow operator.<\/p>\n<p>&nbsp;<\/p>\n<p>Below is code to print out another zoo, but this time we are using pointers.<br \/>\n<a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/c1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-3956\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/c1-458x600.png\" alt=\"c\" width=\"458\" height=\"600\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/c1-458x600.png 458w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/c1-171x225.png 171w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/c1.png 541w\" sizes=\"auto, (max-width: 458px) 100vw, 458px\" \/><\/a><br \/>\n<a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/zooptr.txt\">Zoo Pointer Code<\/a><\/p>\n<p><!--more--><br \/>\n<!--more--><br \/>\nThere are other additions we need\u00a0to use pointers. The <em>stdlib<\/em> contains important functions like <em>malloc,<\/em>\u00a0malloc stands for \u00a0&#8220;memory allocate&#8221;. Before memory allocation, the &#8220;zooptr&#8221; is pointing to a space in memory that hasn&#8217;t been allocated yet, which will cause a segmentation fault. After malloc has been called, space the size of the Animal struct has been allocated.<\/p>\n<p>&nbsp;<\/p>\n<p>This is just an intro on how structures work. In the next blog post, we will go over how to create a linked list of structures, which is the basis for\u00a0creating binary search trees, queues, stacks, and other &#8220;data structures&#8221;.<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-3920 jlk' data-task='like' data-post_id='3920' data-nonce='8896bc70a6' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-3920 lc'>+1<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-3920 jlk' data-task='unlike' data-post_id='3920' data-nonce='8896bc70a6' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-3920 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-3920 status align-left'><\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>Structures are a group of related variables that are placed under one name. Unlike arrays, structures are not limited to one data type. The struct keyword will allow us to create a structure.<\/p>\n","protected":false},"author":29,"featured_media":3952,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1563],"tags":[],"ppma_author":[4478],"class_list":["post-3920","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guide"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b2.png","jetpack_sharing_enabled":true,"authors":[{"term_id":4478,"user_id":29,"is_guest":0,"slug":"josh-woldstad","display_name":"Josh","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/166ab2302d3f5f5f33ecab52f79a1ab44277f4ef8f5073fe66cf54070f6cd419?s=96&d=mm&r=g","1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":"","9":"","10":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/3920","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=3920"}],"version-history":[{"count":1,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/3920\/revisions"}],"predecessor-version":[{"id":31135,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/3920\/revisions\/31135"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/3952"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=3920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=3920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=3920"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=3920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}