{"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='712f7a0d3d' 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='712f7a0d3d' 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":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false,"_ppma_block_editor_authors":""},"categories":[1563],"tags":[],"ppma_author":[4478],"class_list":["post-3920","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guide"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What Is a Struct in Programming? - Digilent Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/digilent.com\/blog\/what-is-a-struct\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What Is a Struct in Programming? - Digilent Blog\" \/>\n<meta property=\"og:description\" content=\"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.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/digilent.com\/blog\/what-is-a-struct\/\" \/>\n<meta property=\"og:site_name\" content=\"Digilent Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-01-05T20:38:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-28T19:31:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"498\" \/>\n\t<meta property=\"og:image:height\" content=\"661\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Josh\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Josh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/digilent.com\\\/blog\\\/what-is-a-struct\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/digilent.com\\\/blog\\\/what-is-a-struct\\\/\"},\"author\":{\"name\":\"Josh\",\"@id\":\"https:\\\/\\\/digilent.com\\\/blog\\\/#\\\/schema\\\/person\\\/3ef2fa34925e6a68162b523150634186\"},\"headline\":\"What Is a Struct in Programming?\",\"datePublished\":\"2015-01-05T20:38:45+00:00\",\"dateModified\":\"2024-12-28T19:31:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/digilent.com\\\/blog\\\/what-is-a-struct\\\/\"},\"wordCount\":312,\"commentCount\":3,\"image\":{\"@id\":\"https:\\\/\\\/digilent.com\\\/blog\\\/what-is-a-struct\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/digilent.com\\\/blog\\\/wp-content\\\/uploads\\\/2014\\\/12\\\/b2.png\",\"articleSection\":[\"How-To Guides and Technical Workflows \u2013 Digilent Blog\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/digilent.com\\\/blog\\\/what-is-a-struct\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/digilent.com\\\/blog\\\/what-is-a-struct\\\/\",\"url\":\"https:\\\/\\\/digilent.com\\\/blog\\\/what-is-a-struct\\\/\",\"name\":\"What Is a Struct in Programming? - Digilent Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/digilent.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/digilent.com\\\/blog\\\/what-is-a-struct\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/digilent.com\\\/blog\\\/what-is-a-struct\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/digilent.com\\\/blog\\\/wp-content\\\/uploads\\\/2014\\\/12\\\/b2.png\",\"datePublished\":\"2015-01-05T20:38:45+00:00\",\"dateModified\":\"2024-12-28T19:31:40+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/digilent.com\\\/blog\\\/#\\\/schema\\\/person\\\/3ef2fa34925e6a68162b523150634186\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/digilent.com\\\/blog\\\/what-is-a-struct\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/digilent.com\\\/blog\\\/what-is-a-struct\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/digilent.com\\\/blog\\\/what-is-a-struct\\\/#primaryimage\",\"url\":\"https:\\\/\\\/digilent.com\\\/blog\\\/wp-content\\\/uploads\\\/2014\\\/12\\\/b2.png\",\"contentUrl\":\"https:\\\/\\\/digilent.com\\\/blog\\\/wp-content\\\/uploads\\\/2014\\\/12\\\/b2.png\",\"width\":498,\"height\":661},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/digilent.com\\\/blog\\\/what-is-a-struct\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/digilent.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What Is a Struct in Programming?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/digilent.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/digilent.com\\\/blog\\\/\",\"name\":\"Digilent Blog\",\"description\":\"Learning for Engineers, Students, and Hobbyists\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/digilent.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/digilent.com\\\/blog\\\/#\\\/schema\\\/person\\\/3ef2fa34925e6a68162b523150634186\",\"name\":\"Josh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/166ab2302d3f5f5f33ecab52f79a1ab44277f4ef8f5073fe66cf54070f6cd419?s=96&d=mm&r=g4ae6798685a47b0faa75585ec8c54683\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/166ab2302d3f5f5f33ecab52f79a1ab44277f4ef8f5073fe66cf54070f6cd419?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/166ab2302d3f5f5f33ecab52f79a1ab44277f4ef8f5073fe66cf54070f6cd419?s=96&d=mm&r=g\",\"caption\":\"Josh\"},\"description\":\"I love Coding!\",\"url\":\"https:\\\/\\\/digilent.com\\\/blog\\\/author\\\/josh-woldstad\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What Is a Struct in Programming? - Digilent Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/digilent.com\/blog\/what-is-a-struct\/","og_locale":"en_US","og_type":"article","og_title":"What Is a Struct in Programming? - Digilent Blog","og_description":"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.","og_url":"https:\/\/digilent.com\/blog\/what-is-a-struct\/","og_site_name":"Digilent Blog","article_published_time":"2015-01-05T20:38:45+00:00","article_modified_time":"2024-12-28T19:31:40+00:00","og_image":[{"width":498,"height":661,"url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b2.png","type":"image\/png"}],"author":"Josh","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Josh","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/digilent.com\/blog\/what-is-a-struct\/#article","isPartOf":{"@id":"https:\/\/digilent.com\/blog\/what-is-a-struct\/"},"author":{"name":"Josh","@id":"https:\/\/digilent.com\/blog\/#\/schema\/person\/3ef2fa34925e6a68162b523150634186"},"headline":"What Is a Struct in Programming?","datePublished":"2015-01-05T20:38:45+00:00","dateModified":"2024-12-28T19:31:40+00:00","mainEntityOfPage":{"@id":"https:\/\/digilent.com\/blog\/what-is-a-struct\/"},"wordCount":312,"commentCount":3,"image":{"@id":"https:\/\/digilent.com\/blog\/what-is-a-struct\/#primaryimage"},"thumbnailUrl":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b2.png","articleSection":["How-To Guides and Technical Workflows \u2013 Digilent Blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/digilent.com\/blog\/what-is-a-struct\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/digilent.com\/blog\/what-is-a-struct\/","url":"https:\/\/digilent.com\/blog\/what-is-a-struct\/","name":"What Is a Struct in Programming? - Digilent Blog","isPartOf":{"@id":"https:\/\/digilent.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/digilent.com\/blog\/what-is-a-struct\/#primaryimage"},"image":{"@id":"https:\/\/digilent.com\/blog\/what-is-a-struct\/#primaryimage"},"thumbnailUrl":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b2.png","datePublished":"2015-01-05T20:38:45+00:00","dateModified":"2024-12-28T19:31:40+00:00","author":{"@id":"https:\/\/digilent.com\/blog\/#\/schema\/person\/3ef2fa34925e6a68162b523150634186"},"breadcrumb":{"@id":"https:\/\/digilent.com\/blog\/what-is-a-struct\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/digilent.com\/blog\/what-is-a-struct\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/digilent.com\/blog\/what-is-a-struct\/#primaryimage","url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b2.png","contentUrl":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b2.png","width":498,"height":661},{"@type":"BreadcrumbList","@id":"https:\/\/digilent.com\/blog\/what-is-a-struct\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/digilent.com\/blog\/"},{"@type":"ListItem","position":2,"name":"What Is a Struct in Programming?"}]},{"@type":"WebSite","@id":"https:\/\/digilent.com\/blog\/#website","url":"https:\/\/digilent.com\/blog\/","name":"Digilent Blog","description":"Learning for Engineers, Students, and Hobbyists","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/digilent.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/digilent.com\/blog\/#\/schema\/person\/3ef2fa34925e6a68162b523150634186","name":"Josh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/166ab2302d3f5f5f33ecab52f79a1ab44277f4ef8f5073fe66cf54070f6cd419?s=96&d=mm&r=g4ae6798685a47b0faa75585ec8c54683","url":"https:\/\/secure.gravatar.com\/avatar\/166ab2302d3f5f5f33ecab52f79a1ab44277f4ef8f5073fe66cf54070f6cd419?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/166ab2302d3f5f5f33ecab52f79a1ab44277f4ef8f5073fe66cf54070f6cd419?s=96&d=mm&r=g","caption":"Josh"},"description":"I love Coding!","url":"https:\/\/digilent.com\/blog\/author\/josh-woldstad\/"}]}},"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","author_category":"","user_url":"","last_name":"Woldstad","last_name_2":"","first_name":"Josh","first_name_2":"","job_title":"","description":"I love Coding!"}],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b2.png","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}]}}