{"id":116,"date":"2026-05-21T04:39:07","date_gmt":"2026-05-21T04:39:07","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=116"},"modified":"2026-05-22T10:46:59","modified_gmt":"2026-05-22T10:46:59","slug":"structures-basics","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=structures-basics","title":{"rendered":"Structures Basics"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Structures in C++ are user-defined data types that allow you to combine different types of data into a single unit. They are useful for organizing related information together.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Structure?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A structure (<code>struct<\/code>) is a collection of variables of different data types grouped under one name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, a student may have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Name<\/li>\n\n\n\n<li>Age<\/li>\n\n\n\n<li>Marks<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of creating separate variables, a structure stores them together.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Structures?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Structures are useful because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Organize related data<\/li>\n\n\n\n<li>Reduce code complexity<\/li>\n\n\n\n<li>Improve readability<\/li>\n\n\n\n<li>Represent real-world entities<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Structure<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>struct StructureName {<br>    data_type member1;<br>    data_type member2;<br>};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Structure<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>struct Student {<br>    string name;<br>    int age;<br>    float marks;<br>};<br><br>int main() {<br><br>    Student s1;<br><br>    s1.name = \"Ali\";<br>    s1.age = 18;<br>    s1.marks = 85.5;<br><br>    cout &lt;&lt; s1.name &lt;&lt; endl;<br>    cout &lt;&lt; s1.age &lt;&lt; endl;<br>    cout &lt;&lt; s1.marks &lt;&lt; endl;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Ali<br>18<br>85.5<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Structures Work<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>struct<\/code> defines a custom data type<\/li>\n\n\n\n<li>Variables inside structure are called members<\/li>\n\n\n\n<li>Structure variables are called objects<\/li>\n\n\n\n<li>Members are accessed using the dot (<code>.<\/code>) operator<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing Structure Members<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>s1.name = \"Ali\";<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Structure Initialization<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can initialize structure values directly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Student s1 = {\"Ahmed\", 20, 90.5};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Array of Structures<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Structures can also be used in arrays.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Student students&#91;3];<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Array of Structures<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>struct Student {<br>    string name;<br>    int age;<br>};<br><br>int main() {<br><br>    Student s&#91;2];<br><br>    s&#91;0].name = \"Ali\";<br>    s&#91;0].age = 18;<br><br>    s&#91;1].name = \"Ahmed\";<br>    s&#91;1].age = 20;<br><br>    cout &lt;&lt; s&#91;0].name &lt;&lt; endl;<br>    cout &lt;&lt; s&#91;1].name &lt;&lt; endl;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Nested Structures<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A structure can contain another structure.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Address {<br>    string city;<br>};<br><br>struct Student {<br>    string name;<br>    Address addr;<br>};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Structure with Functions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Structures can also contain functions in C++.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Demo {<br>    int x;<br><br>    void show() {<br>        cout &lt;&lt; x;<br>    }<br>};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Difference Between Structure and Class<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Structure<\/th><th>Class<\/th><\/tr><\/thead><tbody><tr><td>Members are public by default<\/td><td>Members are private by default<\/td><\/tr><tr><td>Mainly used for simple data grouping<\/td><td>Used for full OOP concepts<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Structures<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Easy data organization<\/li>\n\n\n\n<li>Supports different data types together<\/li>\n\n\n\n<li>Improves code clarity<\/li>\n\n\n\n<li>Useful for records and databases<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Structures are Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Structures are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Represent real-world objects<\/li>\n\n\n\n<li>Help manage complex data<\/li>\n\n\n\n<li>Support better program design<\/li>\n\n\n\n<li>Form the base of advanced concepts<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a student ID card:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Name<\/li>\n\n\n\n<li>Roll number<\/li>\n\n\n\n<li>Class<\/li>\n\n\n\n<li>Marks<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">All related information is stored together, just like a structure.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Structures in C++ are powerful user-defined data types used to group related data together. They improve program organization, readability, and efficiency, making them essential for handling complex information in real-world applications.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate C++ > Structures and Enums > Structures Basics<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":34,"template":"","class_list":["post-116","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Structures Basics - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ structures basics with examples to store related data types, organize information, and model real-world objects efficiently.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Structures Basics - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ structures basics with examples to store related data types, organize information, and model real-world objects efficiently.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-22T10:46:59+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=structures-basics\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Structures Basics - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T04:39:07+00:00\",\"dateModified\":\"2026-05-22T10:46:59+00:00\",\"description\":\"Learn C++ structures basics with examples to store related data types, organize information, and model real-world objects efficiently.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate C++ > Structures and Enums > Structures Basics\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Structures Basics - Learn C++Language with GiGz.PK","description":"Learn C++ structures basics with examples to store related data types, organize information, and model real-world objects efficiently.","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:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Structures Basics - Learn C++Language with GiGz.PK","og_description":"Learn C++ structures basics with examples to store related data types, organize information, and model real-world objects efficiently.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T10:46:59+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=structures-basics","url":"https:\/\/gigz.pk\/","name":"Structures Basics - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T04:39:07+00:00","dateModified":"2026-05-22T10:46:59+00:00","description":"Learn C++ structures basics with examples to store related data types, organize information, and model real-world objects efficiently.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Intermediate C++ > Structures and Enums > Structures Basics"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}