{"id":217,"date":"2020-08-31T14:53:24","date_gmt":"2020-08-31T14:53:24","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=217"},"modified":"2020-08-31T14:53:24","modified_gmt":"2020-08-31T14:53:24","slug":"c-data-structures","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/cplusplus\/c-data-structures\/","title":{"rendered":"C++ Data Structures"},"content":{"rendered":"<p>In this lesson, we would learn about C++ data structures. We&#8217;ll see how to create them and how to use them in programs. We&#8217;ll cover the following sub-topics:<\/p>\n<ol>\n<li><a href=\"#t1\">What are Structures?<\/a><\/li>\n<li><a href=\"#t2\">Declaring a Structure<\/a><\/li>\n<li><a href=\"#t3\">Accessing Members of a Structure<\/a><\/li>\n<li><a href=\"#t4\">Using the <em>typedef<\/em> Keyword<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. What are Structures?<\/strong><\/h4>\n<p>A structure in C++ is a collection of variables of different data type under a common name. It is similar to a record in a table. So while a C++ Array is a collection of variables of same type, a structure contains variables of different types.<\/p>\n<p>Use cases include:<\/p>\n<ul>\n<li>a student information made up of id, name, admission date, age, and department<\/li>\n<li>a book record consisting of if, title, author and publish date<\/li>\n<\/ul>\n<p>You may probably think of other examples as well<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Declaring\u00a0 a Structure<\/strong><\/h4>\n<p>Use use the <em><strong>struct<\/strong> <\/em>keyword to define a structure. Specify the struct keyword, followed by an identifier. Then inside curly braces, you can declare all the variables (called members) that make up the structure.<\/p>\n<p>For example:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">struct<\/span> Student {\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> id;\r\n\tstring firstname;\r\n\tstring lastname;\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> age;\r\n\tstring department;\r\n};\r\n<\/pre>\n<p>Here a structure <em><strong>Student<\/strong> <\/em>is defined and is has five members: id, firstname, lastname, age and department.<\/p>\n<p>However, when a struct is created, unlike arrays, no memory is allocated. It&#8217;s similar to declaring a variable without initialization.<\/p>\n<p>After declaring your struct, you can then declare structure type variable in same way you declare other variables. For example<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Student archana;\r\n<\/pre>\n<p>Here, we have declare a variable archana which is of type Student. At this point, the compiler then allocates the required memory.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Accessing Member Variables of Struct<\/strong><\/h4>\n<p>Members of a struct variable are accessed using the<strong> member access operator, dot (.)<\/strong>.<\/p>\n<p>Assuming we want to access department of structure variable archana and assign a value to it. We can achieve it using<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333333;\">&lt;<\/span>struct_name<span style=\"color: #333333;\">&gt;<\/span>.<span style=\"color: #333333;\">&lt;<\/span>member_name<span style=\"color: #333333;\">&gt;<\/span>\r\n<\/pre>\n<p>Like so<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">archana.department <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Computer Science\"<\/span>\r\n<\/pre>\n<p>The program below show how a struct works. We define a struct, then assign value from the user input<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">using<\/span> <span style=\"color: #008800; font-weight: bold;\">namespace<\/span> std;\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">struct<\/span> Student {\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> id;\r\n\tstring firstname;\r\n\tstring lastname;\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> age;\r\n\tstring department;\r\n};\r\n\r\n<span style=\"color: #888888;\">\/\/ main function<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n\r\n\tStudent archana;\r\n\r\n\tarchana.id <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>;\r\n\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Enter Student firstname: \"<\/span>;\r\n\tcin<span style=\"color: #333333;\">&gt;&gt;<\/span> archana.firstname;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Enter Student lastname: \"<\/span>;\r\n\tcin <span style=\"color: #333333;\">&gt;&gt;<\/span> archana.lastname;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Enter age: \"<\/span>;\r\n\tcin <span style=\"color: #333333;\">&gt;&gt;<\/span> archana.age;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Enter department: \"<\/span>;\r\n\tcin <span style=\"color: #333333;\">&gt;&gt;<\/span> archana.department;\r\n\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"<\/span><span style=\"color: #666666; font-weight: bold; background-color: #fff0f0;\">\\n<\/span><span style=\"background-color: #fff0f0;\">******* Student Details ********\"<\/span><span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"ID:         \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> archana.id        <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Firstname:  \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> archana.firstname <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Lastname:   \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> archana.lastname  <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Age:        \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> archana.age       <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Department: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> archana.department<span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>One thing to note is that the structure is defined outside the main method.<\/p>\n<p>The output of this program is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Enter Student firstname: Kindson\r\nEnter Student lastname: Munonye\r\nEnter age: 34\r\nEnter department: Computer Science\r\n\r\n******* Student Details ********\r\nID:         1\r\nFirstname:  Kindson\r\nLastname:   Munonye\r\nAge:        34\r\nDepartment: Computer\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Using the <em>typedef<\/em> Keyword<\/strong><\/h4>\n<p>Another way to create a structure is to use the <strong>typedef<\/strong> keyword. This make it kind of easier. For example, the code below creates a student structure.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">typedef<\/span> <span style=\"color: #008800; font-weight: bold;\">struct<\/span> {\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> regNo;\r\n\tstring name;\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> email;\r\n\tstring course;\r\n} Student;\r\n<\/pre>\n<p>With this, you can then create Student objects just like before.<\/p>\n<p>In addition to creation of structures, the <strong><em>typedef<\/em> <\/strong>keyword can also be used to define variables of other data types. Here are a few examples:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">typedef<\/span> <span style=\"color: #333399; font-weight: bold;\">long<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> myInt; <span style=\"color: #888888;\">\/\/I just created my own data type!<\/span>\r\n\r\nmyInt x, y; <span style=\"color: #888888;\">\/\/x any y are now long ints<\/span>\r\n<\/pre>\n<p>So this is the much we have for C++ Structures!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, we would learn about C++ data structures. We&#8217;ll see how to create them and how to use them in programs. We&#8217;ll cover &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[33,34],"class_list":["post-217","post","type-post","status-publish","format-standard","hentry","category-c-tutorials","tag-struct","tag-typedef"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C++ Data Structures - C++ Tutorials<\/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:\/\/kindsonthegenius.com\/cplusplus\/c-data-structures\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Data Structures - C++ Tutorials\" \/>\n<meta property=\"og:description\" content=\"In this lesson, we would learn about C++ data structures. We&#8217;ll see how to create them and how to use them in programs. We&#8217;ll cover &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/cplusplus\/c-data-structures\/\" \/>\n<meta property=\"og:site_name\" content=\"C++ Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-31T14:53:24+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-data-structures\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-data-structures\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/#\\\/schema\\\/person\\\/59a11a9f72bb132f56f91ab9f8a58a0e\"},\"headline\":\"C++ Data Structures\",\"datePublished\":\"2020-08-31T14:53:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-data-structures\\\/\"},\"wordCount\":415,\"commentCount\":1,\"keywords\":[\"Struct\",\"typedef\"],\"articleSection\":[\"C++ Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-data-structures\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-data-structures\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-data-structures\\\/\",\"name\":\"C++ Data Structures - C++ Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/#website\"},\"datePublished\":\"2020-08-31T14:53:24+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/#\\\/schema\\\/person\\\/59a11a9f72bb132f56f91ab9f8a58a0e\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-data-structures\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-data-structures\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-data-structures\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Data Structures\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/\",\"name\":\"C++ Tutorials\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/#\\\/schema\\\/person\\\/59a11a9f72bb132f56f91ab9f8a58a0e\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Data Structures - C++ Tutorials","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:\/\/kindsonthegenius.com\/cplusplus\/c-data-structures\/","og_locale":"en_US","og_type":"article","og_title":"C++ Data Structures - C++ Tutorials","og_description":"In this lesson, we would learn about C++ data structures. We&#8217;ll see how to create them and how to use them in programs. We&#8217;ll cover &hellip;","og_url":"https:\/\/kindsonthegenius.com\/cplusplus\/c-data-structures\/","og_site_name":"C++ Tutorials","article_published_time":"2020-08-31T14:53:24+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-data-structures\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-data-structures\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/#\/schema\/person\/59a11a9f72bb132f56f91ab9f8a58a0e"},"headline":"C++ Data Structures","datePublished":"2020-08-31T14:53:24+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-data-structures\/"},"wordCount":415,"commentCount":1,"keywords":["Struct","typedef"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kindsonthegenius.com\/cplusplus\/c-data-structures\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-data-structures\/","url":"https:\/\/kindsonthegenius.com\/cplusplus\/c-data-structures\/","name":"C++ Data Structures - C++ Tutorials","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/cplusplus\/#website"},"datePublished":"2020-08-31T14:53:24+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/cplusplus\/#\/schema\/person\/59a11a9f72bb132f56f91ab9f8a58a0e"},"breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-data-structures\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/cplusplus\/c-data-structures\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-data-structures\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/cplusplus\/"},{"@type":"ListItem","position":2,"name":"C++ Data Structures"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/#website","url":"https:\/\/kindsonthegenius.com\/cplusplus\/","name":"C++ Tutorials","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/cplusplus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/#\/schema\/person\/59a11a9f72bb132f56f91ab9f8a58a0e","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/kindsonthegenius.com\/cplusplus\/author\/kindsonthegenius-3\/"}]}},"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/comments?post=217"}],"version-history":[{"count":3,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/217\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/217\/revisions\/220"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}