๐ง What is a Structure?
A structure in C allows you to group variables of different types under one single name. It is a way to create your own custom data type.
๐ฆ A structure is like a box that can hold different types of data โ like a name (string), age (int), and salary (float) โ all in one unit.
๐ ๏ธ Why Use Structures?
C provides primitive data types like int, float, char, etc. But real-world data is usually more complex.
๐ก Example:
Suppose you want to store student data like:
Roll Number (int)
Name (string)
Percentage (float)
You can either use three separate variables:
int roll;
char name[50];
float percentage;But if you want to manage 100 students, it becomes messy.
๐น struct Keyword
The struct keyword in C is used to define a structure, which is a user-defined data type that allows grouping of variables of different types under a single name.
Why Use struct?
Purpose | Explanation |
|---|---|
Group related data | Combine different types (int, char[], float) together |
Real-world mapping | Represent real-world entities like Student, Book, Employee |
Pass as a single unit | Pass a full record to functions easily |
Organize code | Make large programs more readable and maintainable |
โ Instead, define a structure:
struct Student {
int roll;
char name[50];
float percentage;
};Now you can create multiple student objects, each containing all these fields grouped together.
<div class="infographic-cstruct2">
<style>
.infographic-cstruct2 {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--code-bg: #1e1e2e;
--code-text: #d4d4e0;
--syn-keyword: #c586c0;
--syn-func: #dcdcaa;
}
.infographic-cstruct2[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--code-bg: #0d0d14;
--code-text: #d4d4e0;
}
.infographic-cstruct2 {
box-sizing: border-box;
max-width: 480px;
margin: 0 auto;
background: var(--paper);
color: var(--ink);
font-family: 'Inter', -apple-system, sans-serif;
padding: 0 clamp(1rem, 4vw, 2.25rem) 1.25rem;
}
.infographic-cstruct2 *, .infographic-cstruct2 *::before, .infographic-cstruct2 *::after {
box-sizing: border-box;
}
.infographic-cstruct2 h1 {
font-family: 'Fraunces', Georgia, serif;
font-weight: 600;
font-size: clamp(1.3rem, 3.5vw, 1.7rem);
margin: 1.25rem 0 0.2rem;
text-align: center;
}
.infographic-cstruct2 .subhead { color: var(--ink-soft); font-size: 0.85rem; margin-bottom: 1.2rem; text-align: center; }
.infographic-cstruct2 .code-panel {
background: var(--code-bg);
color: var(--code-text);
border-radius: 8px;
padding: 0.65rem 0.9rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.82rem;
line-height: 1.6;
margin-bottom: 1.1rem;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
.infographic-cstruct2 .code-panel .code-line { display: block; white-space: pre; }
.infographic-cstruct2 .syn-keyword { color: var(--syn-keyword); }
.infographic-cstruct2 .syn-func { color: var(--syn-func); }
.infographic-cstruct2 .mem-row { display: flex; justify-content: center; gap: 0.4rem; margin-bottom: 0.3rem; overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 0.2rem; }
.infographic-cstruct2 .mem-cell { text-align: center; flex-shrink: 0; }
.infographic-cstruct2 .mem-name {
font-family: 'IBM Plex Mono', monospace;
font-size: 0.72rem;
color: var(--ink-soft);
margin-bottom: 0.3rem;
white-space: nowrap;
}
.infographic-cstruct2 .mem-box {
height: 3rem;
border: 2px solid var(--accent);
background: var(--accent-soft);
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
padding: 0 0.3rem;
}
.infographic-cstruct2 .mem-box.w-roll { width: 3.6rem; }
.infographic-cstruct2 .mem-box.w-name { width: 7.5rem; }
.infographic-cstruct2 .mem-box.w-pct { width: 3.6rem; }
.infographic-cstruct2 .mem-box input {
all: unset;
width: 100%;
text-align: center;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.9rem;
font-weight: 700;
color: var(--accent);
}
.infographic-cstruct2 .mem-bytes { font-family: 'IBM Plex Mono', monospace; font-size: 0.66rem; color: var(--ink-soft); margin-top: 0.25rem; white-space: nowrap; }
.infographic-cstruct2 .facts { text-align: center; font-family: 'IBM Plex Mono', monospace; font-size: 0.8rem; color: var(--ink-soft); margin: 0.9rem 0 1.1rem; }
.infographic-cstruct2 .facts strong { color: var(--ink); }
.infographic-cstruct2 .access-panel {
background: var(--paper-alt);
border-radius: 8px;
padding: 0.8rem 1rem;
}
.infographic-cstruct2 .access-line {
font-family: 'IBM Plex Mono', monospace;
font-size: 0.82rem;
display: flex;
justify-content: space-between;
gap: 0.5rem;
padding: 0.2rem 0;
}
.infographic-cstruct2 .access-line span:first-child { color: var(--ink-soft); }
.infographic-cstruct2 .access-line strong { color: var(--accent); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 12rem; }
.infographic-cstruct2 .takeaway {
font-size: 0.82rem; color: var(--ink-soft);
margin-top: 1.3rem; border-top: 1px solid var(--line); padding-top: 0.9rem;
}
.infographic-cstruct2 .takeaway strong { color: var(--ink); }
</style>
<h1>๐งฑ Struct in C</h1>
<p class="subhead">One box, made of smaller named boxes.</p>
<div class="code-panel">
<span class="code-line"><span class="syn-keyword">struct</span> <span class="syn-func">Student</span> {</span>
<span class="code-line"> <span class="syn-keyword">int</span> roll;</span>
<span class="code-line"> <span class="syn-keyword">char</span> name[50];</span>
<span class="code-line"> <span class="syn-keyword">float</span> percentage;</span>
<span class="code-line">};</span>
</div>
<div class="mem-row">
<div class="mem-cell">
<div class="mem-name">roll</div>
<div class="mem-box w-roll"><input type="number" id="cs-roll" value="7"></div>
<div class="mem-bytes">4 bytes</div>
</div>
<div class="mem-cell">
<div class="mem-name">name</div>
<div class="mem-box w-name"><input type="text" id="cs-name" value="Alice"></div>
<div class="mem-bytes">50 bytes</div>
</div>
<div class="mem-cell">
<div class="mem-name">percentage</div>
<div class="mem-box w-pct"><input type="number" id="cs-pct" value="88.5" step="0.1"></div>
<div class="mem-bytes">4 bytes</div>
</div>
</div>
<p class="facts">sizeof(struct Student) = <strong>58 bytes</strong> <span style="opacity:0.7;">(4 + 50 + 4)</span></p>
<div class="access-panel">
<div class="access-line"><span>s1.roll</span><strong id="acc-dot-roll">7</strong></div>
<div class="access-line"><span>s1.name</span><strong id="acc-dot-name">Alice</strong></div>
<div class="access-line"><span>s1.percentage</span><strong id="acc-dot-pct">88.5</strong></div>
</div>
<p class="takeaway"><strong>Edit the boxes above</strong> โ each member sits right next to the last one in memory, in the order you declared them.</p>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-cstruct2')
|| document.querySelector('.infographic-cstruct2');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') { dark = true; }
else if (bodyAttr === 'false') { dark = false; }
else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
function $(sel) { return wrapper.querySelector(sel); }
const rollInput = $('#cs-roll'), nameInput = $('#cs-name'), pctInput = $('#cs-pct');
function update() {
$('#acc-dot-roll').textContent = rollInput.value;
$('#acc-dot-name').textContent = nameInput.value;
$('#acc-dot-pct').textContent = pctInput.value;
}
rollInput.addEventListener('input', update);
nameInput.addEventListener('input', update);
pctInput.addEventListener('input', update);
update();
})();
</script>๐งฉ Step-by-Step: Defining a Structure
๐ค Syntax:
struct StructureName {
dataType member1;
dataType member2;
...
};โ Example:
struct Student {
int roll;
char name[50];
float percentage;
};๐งพ This does not create any variables yet, just a template or blueprint.
๐ค Declaring Structure Variables
After defining the structure, you can create variables like this:
struct Student s1, s2;Now s1 and s2 each have their own roll, name, and percentage.
๐งช Accessing Members (Dot Operator)
Use the dot operator (.) to access structure members.
s1.roll = 101;
strcpy(s1.name, "Rahul");
s1.percentage = 92.5;Note: Use strcpy() to copy string data into character arrays.
Print values:
printf("Roll: %d\\n", s1.roll);
printf("Name: %s\\n", s1.name);
printf("Percentage: %.2f\\n", s1.percentage);๐ก Shortcut with typedef
Typing struct Student every time is long. So we can shorten it using typedef.
typedef struct Student {
int roll;
char name[50];
float percentage;
} Student;Now you can declare:
Student s1, s2;Much cleaner and easier!
<div class="infographic-cstruct2">
<style>
.infographic-cstruct2 {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--code-bg: #1e1e2e;
--code-text: #d4d4e0;
--syn-keyword: #c586c0;
--syn-func: #dcdcaa;
}
.infographic-cstruct2[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--code-bg: #0d0d14;
--code-text: #d4d4e0;
}
.infographic-cstruct2 {
box-sizing: border-box;
max-width: 880px;
margin: 0 auto;
background: var(--paper);
color: var(--ink);
font-family: 'Inter', -apple-system, sans-serif;
padding: 0 clamp(1rem, 4vw, 2.25rem) 1.25rem;
}
.infographic-cstruct2 *, .infographic-cstruct2 *::before, .infographic-cstruct2 *::after {
box-sizing: border-box;
}
.infographic-cstruct2 h1 {
font-family: 'Fraunces', Georgia, serif;
font-weight: 600;
font-size: clamp(1.3rem, 3.5vw, 1.7rem);
margin: 1.25rem 0 0.2rem;
text-align: center;
}
.infographic-cstruct2 .subhead { color: var(--ink-soft); font-size: 0.85rem; margin-bottom: 1.2rem; text-align: center; }
.infographic-cstruct2 .chip-row { display: flex; justify-content: center; gap: 0.45rem; margin-bottom: 1rem; }
.infographic-cstruct2 .chip {
all: unset;
cursor: pointer;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.78rem;
font-weight: 600;
padding: 0.4rem 0.85rem;
border-radius: 20px;
border: 1px solid var(--line);
color: var(--ink-soft);
transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.infographic-cstruct2 .chip:hover { border-color: var(--accent); }
.infographic-cstruct2 .chip.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.infographic-cstruct2 .code-panel {
background: var(--code-bg);
color: var(--code-text);
border-radius: 8px;
padding: 0.65rem 0.9rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.82rem;
line-height: 1.6;
margin-bottom: 1.1rem;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
.infographic-cstruct2 .code-panel .code-line { display: block; white-space: pre; }
.infographic-cstruct2 .syn-keyword { color: var(--syn-keyword); }
.infographic-cstruct2 .syn-func { color: var(--syn-func); }
.infographic-cstruct2 .mem-row { display: flex; justify-content: center; gap: 0.4rem; margin-bottom: 0.3rem; overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 0.2rem; }
.infographic-cstruct2 .mem-cell { text-align: center; flex-shrink: 0; }
.infographic-cstruct2 .mem-name {
font-family: 'IBM Plex Mono', monospace;
font-size: 0.72rem;
color: var(--ink-soft);
margin-bottom: 0.3rem;
white-space: nowrap;
}
.infographic-cstruct2 .mem-box {
height: 3rem;
border: 2px solid var(--accent);
background: var(--accent-soft);
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
padding: 0 0.3rem;
}
.infographic-cstruct2 .mem-box.w-roll { width: 3.6rem; }
.infographic-cstruct2 .mem-box.w-name { width: 7.5rem; }
.infographic-cstruct2 .mem-box.w-pct { width: 3.6rem; }
.infographic-cstruct2 .mem-box input {
all: unset;
width: 100%;
text-align: center;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.9rem;
font-weight: 700;
color: var(--accent);
}
.infographic-cstruct2 .mem-bytes { font-family: 'IBM Plex Mono', monospace; font-size: 0.66rem; color: var(--ink-soft); margin-top: 0.25rem; white-space: nowrap; }
.infographic-cstruct2 .facts { text-align: center; font-family: 'IBM Plex Mono', monospace; font-size: 0.8rem; color: var(--ink-soft); margin: 0.9rem 0 1.1rem; }
.infographic-cstruct2 .facts strong { color: var(--ink); }
.infographic-cstruct2 .access-panel {
background: var(--paper-alt);
border-radius: 8px;
padding: 0.8rem 1rem;
}
.infographic-cstruct2 .access-line {
font-family: 'IBM Plex Mono', monospace;
font-size: 0.82rem;
display: flex;
justify-content: space-between;
gap: 0.5rem;
padding: 0.2rem 0;
}
.infographic-cstruct2 .access-line span:first-child { color: var(--ink-soft); }
.infographic-cstruct2 .access-line strong { color: var(--accent); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 12rem; }
.infographic-cstruct2 .takeaway {
font-size: 0.82rem; color: var(--ink-soft);
margin-top: 1.3rem; border-top: 1px solid var(--line); padding-top: 0.9rem;
}
.infographic-cstruct2 .takeaway strong { color: var(--ink); }
</style>
<h1>๐งฑ Struct in C</h1>
<p class="subhead">One box, made of smaller named boxes.</p>
<div class="chip-row" id="td-tabs">
<button class="chip is-active" data-mode="plain">without typedef</button>
<button class="chip" data-mode="typedef">with typedef</button>
</div>
<div class="code-panel" id="cs-code"></div>
<div class="mem-row">
<div class="mem-cell">
<div class="mem-name">roll</div>
<div class="mem-box w-roll"><input type="number" id="cs-roll" value="7"></div>
<div class="mem-bytes">4 bytes</div>
</div>
<div class="mem-cell">
<div class="mem-name">name</div>
<div class="mem-box w-name"><input type="text" id="cs-name" value="Alice"></div>
<div class="mem-bytes">50 bytes</div>
</div>
<div class="mem-cell">
<div class="mem-name">percentage</div>
<div class="mem-box w-pct"><input type="number" id="cs-pct" value="88.5" step="0.1"></div>
<div class="mem-bytes">4 bytes</div>
</div>
</div>
<p class="facts">sizeof(struct Student) = <strong>58 bytes</strong> <span style="opacity:0.7;">(4 + 50 + 4)</span></p>
<div class="access-panel">
<div class="access-line"><span>s1.roll</span><strong id="acc-dot-roll">7</strong></div>
<div class="access-line"><span>s1.name</span><strong id="acc-dot-name">Alice</strong></div>
<div class="access-line"><span>s1.percentage</span><strong id="acc-dot-pct">88.5</strong></div>
</div>
<p class="takeaway" id="cs-takeaway"></p>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-cstruct2')
|| document.querySelector('.infographic-cstruct2');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') { dark = true; }
else if (bodyAttr === 'false') { dark = false; }
else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
function $(sel) { return wrapper.querySelector(sel); }
const rollInput = $('#cs-roll'), nameInput = $('#cs-name'), pctInput = $('#cs-pct');
const codePanel = $('#cs-code');
const takeaway = $('#cs-takeaway');
const tabs = wrapper.querySelectorAll('#td-tabs .chip');
const CODE = {
plain:
'<span class="code-line"><span class="syn-keyword">struct</span> <span class="syn-func">Student</span> {</span>' +
'<span class="code-line"> <span class="syn-keyword">int</span> roll;</span>' +
'<span class="code-line"> <span class="syn-keyword">char</span> name[50];</span>' +
'<span class="code-line"> <span class="syn-keyword">float</span> percentage;</span>' +
'<span class="code-line">};</span>' +
'<span class="code-line"> </span>' +
'<span class="code-line"><span class="syn-keyword">struct</span> <span class="syn-func">Student</span> s1;</span>',
typedef:
'<span class="code-line"><span class="syn-keyword">typedef</span> <span class="syn-keyword">struct</span> {</span>' +
'<span class="code-line"> <span class="syn-keyword">int</span> roll;</span>' +
'<span class="code-line"> <span class="syn-keyword">char</span> name[50];</span>' +
'<span class="code-line"> <span class="syn-keyword">float</span> percentage;</span>' +
'<span class="code-line">} <span class="syn-func">Student</span>;</span>' +
'<span class="code-line"> </span>' +
'<span class="code-line"><span class="syn-func">Student</span> s1;</span>'
};
const TAKEAWAY = {
plain: '<strong>Without typedef</strong>, every declaration needs the <code style="font-family:\'IBM Plex Mono\',monospace;">struct</code> keyword โ <code style="font-family:\'IBM Plex Mono\',monospace;">struct Student s1;</code>',
typedef: '<strong>With typedef</strong>, <code style="font-family:\'IBM Plex Mono\',monospace;">Student</code> becomes a type name on its own โ just <code style="font-family:\'IBM Plex Mono\',monospace;">Student s1;</code>, no <code style="font-family:\'IBM Plex Mono\',monospace;">struct</code> needed.'
};
function renderMode(mode) {
codePanel.innerHTML = CODE[mode];
takeaway.innerHTML = TAKEAWAY[mode];
}
tabs.forEach(tab => {
tab.addEventListener('click', () => {
tabs.forEach(t => t.classList.remove('is-active'));
tab.classList.add('is-active');
renderMode(tab.dataset.mode);
});
});
function update() {
$('#acc-dot-roll').textContent = rollInput.value;
$('#acc-dot-name').textContent = nameInput.value;
$('#acc-dot-pct').textContent = pctInput.value;
}
rollInput.addEventListener('input', update);
nameInput.addEventListener('input', update);
pctInput.addEventListener('input', update);
update();
renderMode('plain');
})();
</script>๐ Multiple Structure Variables
You can declare structure variables at the time of definition:
#include <stdio.h>
// Structure definition with variable declarations
struct Book {
int id;
char title[100];
float price;
} book1 = {101, "C Programming", 299.99},
book2 = {102, "Data Structures", 399.50}; // Initialized at declaration
int main() {
// Printing details of book1
printf("Book 1:\\n");
printf("ID: %d\\n", book1.id);
printf("Title: %s\\n", book1.title);
printf("Price: %.2f\\n\\n", book1.price);
// Printing details of book2
printf("Book 2:\\n");
printf("ID: %d\\n", book2.id);
printf("Title: %s\\n", book2.title);
printf("Price: %.2f\\n", book2.price);
return 0;
}โ Method 1: Designated Initializers (C99+)
struct Student s1 = { .roll = 1, .name = "Amit", .percentage = 85.0 };โ Method 2: Positional Initialization
struct Student s1 = { 1, "Amit", 85.0 };Works only if the order matches the structure definition.
๐ข Array of Structures
To store multiple records:
<div class="infographic-cstruct2">
<style>
.infographic-cstruct2 {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--code-bg: #1e1e2e;
--code-text: #d4d4e0;
--syn-keyword: #c586c0;
--syn-func: #dcdcaa;
}
.infographic-cstruct2[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--code-bg: #0d0d14;
--code-text: #d4d4e0;
}
.infographic-cstruct2 {
box-sizing: border-box;
max-width: 880px;
margin: 0 auto;
background: var(--paper);
color: var(--ink);
font-family: 'Inter', -apple-system, sans-serif;
padding: 0 clamp(1rem, 4vw, 2.25rem) 1.25rem;
}
.infographic-cstruct2 *, .infographic-cstruct2 *::before, .infographic-cstruct2 *::after {
box-sizing: border-box;
}
.infographic-cstruct2 h1 {
font-family: 'Fraunces', Georgia, serif;
font-weight: 600;
font-size: clamp(1.3rem, 3.5vw, 1.7rem);
margin: 1.25rem 0 0.2rem;
text-align: center;
}
.infographic-cstruct2 .subhead { color: var(--ink-soft); font-size: 0.85rem; margin-bottom: 1.2rem; text-align: center; }
.infographic-cstruct2 .code-panel {
background: var(--code-bg);
color: var(--code-text);
border-radius: 8px;
padding: 0.65rem 0.9rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.82rem;
line-height: 1.6;
margin-bottom: 1.1rem;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
.infographic-cstruct2 .code-panel .code-line { display: block; white-space: pre; }
.infographic-cstruct2 .code-panel .code-line.is-highlight {
background: rgba(91, 141, 239, 0.22);
box-shadow: inset 3px 0 0 var(--accent);
}
.infographic-cstruct2 .syn-keyword { color: var(--syn-keyword); }
.infographic-cstruct2 .syn-func { color: var(--syn-func); }
.infographic-cstruct2 .stdin-label { font-size: 0.75rem; color: var(--ink-soft); margin-bottom: 0.5rem; }
.infographic-cstruct2 .stdin-row { display: flex; align-items: center; gap: 0.4rem; margin-bottom: 0.4rem; flex-wrap: wrap; }
.infographic-cstruct2 .stdin-row .idx { font-family: 'IBM Plex Mono', monospace; font-size: 0.75rem; color: var(--ink-soft); width: 1.6rem; flex-shrink: 0; }
.infographic-cstruct2 .stdin-row input {
font-family: 'IBM Plex Mono', monospace;
font-size: 0.8rem;
padding: 0.3rem 0.45rem;
border-radius: 6px;
border: 1px solid var(--line);
background: var(--paper);
color: var(--ink);
}
.infographic-cstruct2 .stdin-row input.f-roll { width: 3rem; }
.infographic-cstruct2 .stdin-row input.f-name { width: 6rem; }
.infographic-cstruct2 .stdin-row input.f-pct { width: 3.6rem; }
.infographic-cstruct2 .run-btn {
all: unset; cursor: pointer;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.8rem; font-weight: 600;
padding: 0.45rem 1rem;
border-radius: 999px;
background: var(--accent);
color: #fff;
margin: 0.7rem 0 1rem;
display: inline-block;
}
.infographic-cstruct2 .run-btn:hover { opacity: 0.88; }
.infographic-cstruct2 .run-btn:disabled { opacity: 0.5; cursor: default; }
.infographic-cstruct2 .students-row { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-bottom: 0.9rem; }
.infographic-cstruct2 .student-card {
flex: 1;
min-width: 8.5rem;
border: 1px solid var(--line);
border-radius: 8px;
padding: 0.55rem 0.7rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.75rem;
transition: border-color 0.25s, background 0.25s;
}
.infographic-cstruct2 .student-card.is-filled { border-color: var(--accent); background: var(--accent-soft); }
.infographic-cstruct2 .student-card .sc-title { font-weight: 700; margin-bottom: 0.3rem; color: var(--ink-soft); }
.infographic-cstruct2 .student-card.is-filled .sc-title { color: var(--accent); }
.infographic-cstruct2 .student-card .sc-line { color: var(--ink-soft); }
.infographic-cstruct2 .student-card.is-filled .sc-line { color: var(--ink); }
.infographic-cstruct2 .printf-line {
font-family: 'IBM Plex Mono', monospace;
font-size: 0.85rem;
padding: 0.6rem 0.9rem;
background: var(--paper-alt);
border-radius: 8px;
}
.infographic-cstruct2 .printf-line strong { color: var(--accent); }
.infographic-cstruct2 .takeaway {
font-size: 0.82rem; color: var(--ink-soft);
margin-top: 1.3rem; border-top: 1px solid var(--line); padding-top: 0.9rem;
}
.infographic-cstruct2 .takeaway strong { color: var(--ink); }
</style>
<h1>๐งฑ Array of Structs</h1>
<p class="subhead">struct Student students[3]; โ one array, three whole records.</p>
<div class="code-panel" id="arr-code">
<span class="code-line" data-line="decl"><span class="syn-keyword">struct</span> <span class="syn-func">Student</span> students[3];</span>
<span class="code-line"> </span>
<span class="code-line" data-line="for"><span class="syn-keyword">for</span>(<span class="syn-keyword">int</span> i=0; i<3; i++) {</span>
<span class="code-line" data-line="s1"> <span class="syn-func">scanf</span>(<span class="syn-func">"%d"</span>, &students[i].roll);</span>
<span class="code-line" data-line="s2"> <span class="syn-func">scanf</span>(<span class="syn-func">"%s"</span>, students[i].name);</span>
<span class="code-line" data-line="s3"> <span class="syn-func">scanf</span>(<span class="syn-func">"%f"</span>, &students[i].percentage);</span>
<span class="code-line">}</span>
<span class="code-line"> </span>
<span class="code-line" data-line="print"><span class="syn-func">printf</span>(<span class="syn-func">"Roll: %d"</span>, students[1].roll);</span>
</div>
<div class="stdin-label">input for scanf() โ edit freely:</div>
<div id="stdin-rows"></div>
<button class="run-btn" id="run-btn">โถ Run</button>
<div class="students-row" id="students-row"></div>
<div class="printf-line" id="printf-out">Roll: <strong>โ</strong></div>
<p class="takeaway"><strong>Same struct, three times over:</strong> the loop fills one full record per iteration โ <code style="font-family:'IBM Plex Mono',monospace;">students[i].roll</code>, <code style="font-family:'IBM Plex Mono',monospace;">.name</code>, and <code style="font-family:'IBM Plex Mono',monospace;">.percentage</code> all belong to the same student.</p>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-cstruct2')
|| document.querySelector('.infographic-cstruct2');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') { dark = true; }
else if (bodyAttr === 'false') { dark = false; }
else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
function $(sel) { return wrapper.querySelector(sel); }
const stdinRows = $('#stdin-rows');
const runBtn = $('#run-btn');
const studentsRow = $('#students-row');
const printfOut = $('#printf-out');
const arrCode = $('#arr-code');
const arrLines = arrCode.querySelectorAll('.code-line[data-line]');
const DEFAULTS = [
{ roll: 1, name: 'Alice', pct: 88.5 },
{ roll: 2, name: 'Bob', pct: 75.2 },
{ roll: 3, name: 'Charlie', pct: 91.0 }
];
const stdinInputs = [];
DEFAULTS.forEach((d, i) => {
const row = document.createElement('div');
row.className = 'stdin-row';
row.innerHTML = '<span class="idx">i=' + i + '</span>';
const rollIn = document.createElement('input');
rollIn.className = 'f-roll'; rollIn.type = 'number'; rollIn.value = d.roll;
const nameIn = document.createElement('input');
nameIn.className = 'f-name'; nameIn.type = 'text'; nameIn.value = d.name;
const pctIn = document.createElement('input');
pctIn.className = 'f-pct'; pctIn.type = 'number'; pctIn.step = '0.1'; pctIn.value = d.pct;
row.appendChild(rollIn); row.appendChild(nameIn); row.appendChild(pctIn);
stdinRows.appendChild(row);
stdinInputs.push({ roll: rollIn, name: nameIn, pct: pctIn });
});
const cards = [0, 1, 2].map(i => {
const card = document.createElement('div');
card.className = 'student-card';
card.innerHTML = '<div class="sc-title">students[' + i + ']</div>' +
'<div class="sc-line">roll: โ</div><div class="sc-line">name: โ</div><div class="sc-line">percentage: โ</div>';
studentsRow.appendChild(card);
return card;
});
function highlightLine(id) {
arrLines.forEach(l => l.classList.toggle('is-highlight', l.dataset.line === id));
}
let filled = [null, null, null];
function runLoop() {
runBtn.disabled = true;
cards.forEach((c, i) => {
c.classList.remove('is-filled');
c.innerHTML = '<div class="sc-title">students[' + i + ']</div>' +
'<div class="sc-line">roll: โ</div><div class="sc-line">name: โ</div><div class="sc-line">percentage: โ</div>';
});
filled = [null, null, null];
printfOut.innerHTML = 'Roll: <strong>โ</strong>';
highlightLine('decl');
let i = 0;
function step() {
if (i >= 3) {
highlightLine('print');
const r1 = filled[1];
printfOut.innerHTML = 'Roll: <strong>' + (r1 ? r1.roll : 'โ') + '</strong>';
runBtn.disabled = false;
setTimeout(() => highlightLine(null), 900);
return;
}
highlightLine('for');
setTimeout(() => {
const data = {
roll: stdinInputs[i].roll.value,
name: stdinInputs[i].name.value,
pct: stdinInputs[i].pct.value
};
highlightLine('s1');
setTimeout(() => {
highlightLine('s2');
setTimeout(() => {
highlightLine('s3');
setTimeout(() => {
filled[i] = data;
cards[i].classList.add('is-filled');
cards[i].innerHTML = '<div class="sc-title">students[' + i + ']</div>' +
'<div class="sc-line">roll: ' + data.roll + '</div>' +
'<div class="sc-line">name: ' + data.name + '</div>' +
'<div class="sc-line">percentage: ' + data.pct + '</div>';
i++;
step();
}, 260);
}, 260);
}, 260);
}, 260);
}
step();
}
runBtn.addEventListener('click', runLoop);
})();
</script>๐งโ๐คโ๐ง Structures with Functions
๐น Passing by Value
#include <stdio.h>
// Define the structure
struct Student {
int roll;
char name[50];
float percentage;
};
// Function that accepts structure by value
void printStudent(struct Student s) {
printf("Student Details:\\n");
printf("Roll: %d\\n", s.roll);
printf("Name: %s\\n", s.name);
printf("Percentage: %.2f\\n", s.percentage);
}
int main() {
// Declare and initialize a structure variable
struct Student s1 = {101, "Rahul", 92.5};
// Function call (passing structure by value)
printStudent(s1);
return 0;
}Student Details:
Roll: 101
Name: Rahul
Percentage: 92.50โ ๏ธ Value is copied. Any changes inside the function won't affect the original.๐น Passing by Reference (Pointer)
#include <stdio.h>
// Define the structure
struct Student {
int roll;
char name[50];
float percentage;
};
// Function that modifies structure using pointer (pass by reference)
void update(struct Student *s) {
s->percentage += 5; // Increase percentage by 5
}
int main() {
// Declare and initialize structure variable
struct Student s1 = {101, "Rahul", 85.0};
// Print before update
printf("Before update:\\n");
printf("Roll: %d\\n", s1.roll);
printf("Name: %s\\n", s1.name);
printf("Percentage: %.2f\\n", s1.percentage);
// Function call (passing structure by reference)
update(&s1);
// Print after update
printf("\\nAfter update:\\n");
printf("Roll: %d\\n", s1.roll);
printf("Name: %s\\n", s1.name);
printf("Percentage: %.2f\\n", s1.percentage);
return 0;
}Before update:
Roll: 101
Name: Rahul
Percentage: 85.00
After update:
Roll: 101
Name: Rahul
Percentage: 90.00Use -> operator to access members via pointers.
<div class="infographic-cstructpass">
<style>
.infographic-cstructpass {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--code-bg: #1e1e2e;
--code-text: #d4d4e0;
--syn-keyword: #c586c0;
--syn-func: #dcdcaa;
--syn-string: #ce9178;
--syn-comment: #6a9955;
--warn: #b45309;
--warn-soft: #fdf0dc;
--ok: #1a7f4e;
--ok-soft: #e3f5ec;
}
.infographic-cstructpass[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--code-bg: #0d0d14;
--code-text: #d4d4e0;
--warn: #e0a458;
--warn-soft: #3a2c14;
--ok: #5cd6a0;
--ok-soft: #16342a;
}
.infographic-cstructpass {
box-sizing: border-box;
max-width: 880px;
margin: 0 auto;
background: var(--paper);
color: var(--ink);
font-family: 'Inter', -apple-system, sans-serif;
padding: 0 clamp(1rem, 4vw, 2.25rem) 1.25rem;
}
.infographic-cstructpass *, .infographic-cstructpass *::before, .infographic-cstructpass *::after {
box-sizing: border-box;
}
.infographic-cstructpass h1 {
font-family: 'Fraunces', Georgia, serif;
font-weight: 600;
font-size: clamp(1.3rem, 3.5vw, 1.7rem);
margin: 1.25rem 0 0.2rem;
text-align: center;
}
.infographic-cstructpass .subhead { color: var(--ink-soft); font-size: 0.85rem; margin-bottom: 1.1rem; text-align: center; }
.infographic-cstructpass .s1-card {
border: 2px solid var(--accent);
background: var(--accent-soft);
border-radius: 10px;
padding: 0.7rem 0.9rem;
margin-bottom: 1.1rem;
transition: box-shadow 0.3s, border-color 0.3s;
}
.infographic-cstructpass .s1-card.is-pulse { animation: csp-pulse 0.6s ease; }
@keyframes csp-pulse {
0% { box-shadow: 0 0 0 0 rgba(26,127,78,0.5); }
100% { box-shadow: 0 0 0 10px rgba(26,127,78,0); }
}
.infographic-cstructpass .s1-title { font-family: 'IBM Plex Mono', monospace; font-size: 0.78rem; font-weight: 700; color: var(--accent); margin-bottom: 0.5rem; }
.infographic-cstructpass .s1-fields { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.infographic-cstructpass .s1-field { display: flex; flex-direction: column; gap: 0.2rem; }
.infographic-cstructpass .s1-field label { font-family: 'IBM Plex Mono', monospace; font-size: 0.68rem; color: var(--ink-soft); }
.infographic-cstructpass .s1-field input {
font-family: 'IBM Plex Mono', monospace;
font-size: 0.85rem;
padding: 0.3rem 0.45rem;
border-radius: 6px;
border: 1px solid var(--line);
background: var(--paper);
color: var(--ink);
}
.infographic-cstructpass .s1-field input.w-roll { width: 3.4rem; }
.infographic-cstructpass .s1-field input.w-name { width: 6rem; }
.infographic-cstructpass .s1-field input.w-pct { width: 4rem; }
.infographic-cstructpass .chip-row { display: flex; justify-content: center; gap: 0.45rem; margin-bottom: 1rem; }
.infographic-cstructpass .chip {
all: unset;
cursor: pointer;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.78rem;
font-weight: 600;
padding: 0.4rem 0.85rem;
border-radius: 20px;
border: 1px solid var(--line);
color: var(--ink-soft);
transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.infographic-cstructpass .chip:hover { border-color: var(--accent); }
.infographic-cstructpass .chip.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.infographic-cstructpass .tab-panel { display: none; }
.infographic-cstructpass .tab-panel.is-active { display: block; }
.infographic-cstructpass .code-panel {
background: var(--code-bg);
color: var(--code-text);
border-radius: 8px;
padding: 0.65rem 0.9rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.78rem;
line-height: 1.55;
margin-bottom: 0.9rem;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
.infographic-cstructpass .code-panel .code-line { display: block; white-space: pre; }
.infographic-cstructpass .syn-keyword { color: var(--syn-keyword); }
.infographic-cstructpass .syn-func { color: var(--syn-func); }
.infographic-cstructpass .syn-string { color: var(--syn-string); }
.infographic-cstructpass .syn-comment { color: var(--syn-comment); }
.infographic-cstructpass .run-btn {
all: unset; cursor: pointer;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.8rem; font-weight: 600;
padding: 0.45rem 1rem;
border-radius: 999px;
background: var(--accent);
color: #fff;
margin-bottom: 0.9rem;
display: inline-block;
}
.infographic-cstructpass .run-btn:hover { opacity: 0.88; }
.infographic-cstructpass .fn-frame {
border: 1px dashed var(--line);
border-radius: 8px;
padding: 0.6rem 0.8rem;
margin-bottom: 0.9rem;
opacity: 0;
transform: translateY(-6px);
transition: opacity 0.35s ease, transform 0.35s ease;
}
.infographic-cstructpass .fn-frame.is-visible { opacity: 1; transform: translateY(0); }
.infographic-cstructpass .fn-frame .fn-title { font-family: 'IBM Plex Mono', monospace; font-size: 0.72rem; color: var(--ink-soft); margin-bottom: 0.35rem; }
.infographic-cstructpass .fn-frame .fn-body { font-family: 'IBM Plex Mono', monospace; font-size: 0.78rem; color: var(--ink); }
.infographic-cstructpass .fn-frame.copy .fn-title { color: var(--warn); }
.infographic-cstructpass .fn-frame.ref .fn-title { color: var(--accent); }
.infographic-cstructpass .console {
background: var(--code-bg);
color: var(--code-text);
border-radius: 8px;
padding: 0.7rem 0.9rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.78rem;
line-height: 1.55;
white-space: pre;
min-height: 2.4rem;
}
.infographic-cstructpass .note {
font-size: 0.8rem;
color: var(--warn);
font-weight: 600;
margin-top: 0.7rem;
text-align: center;
}
.infographic-cstructpass .note.ok { color: var(--ok); }
@media (prefers-reduced-motion: reduce) {
.infographic-cstructpass * { transition: none !important; animation: none !important; }
}
</style>
<h1>๐ Passing a Struct to a Function</h1>
<p class="subhead">Same struct, two ways to hand it to a function.</p>
<div class="s1-card" id="s1-card">
<div class="s1-title">struct Student s1</div>
<div class="s1-fields">
<div class="s1-field"><label>roll</label><input type="number" class="w-roll" id="s1-roll" value="101"></div>
<div class="s1-field"><label>name</label><input type="text" class="w-name" id="s1-name" value="Rahul"></div>
<div class="s1-field"><label>percentage</label><input type="number" class="w-pct" id="s1-pct" value="85.0" step="0.1"></div>
</div>
</div>
<div class="chip-row" id="pass-tabs">
<button class="chip is-active" data-tab="value">Passing by Value</button>
<button class="chip" data-tab="ref">Passing by Reference</button>
</div>
<!-- Passing by value -->
<div class="tab-panel is-active" data-tab="value">
<div class="code-panel">
<span class="code-line"><span class="syn-keyword">void</span> <span class="syn-func">printStudent</span>(<span class="syn-keyword">struct</span> Student s) {</span>
<span class="code-line"> <span class="syn-func">printf</span>(<span class="syn-string">"Roll: %d\n"</span>, s.roll);</span>
<span class="code-line"> <span class="syn-func">printf</span>(<span class="syn-string">"Name: %s\n"</span>, s.name);</span>
<span class="code-line"> <span class="syn-func">printf</span>(<span class="syn-string">"Percentage: %.2f\n"</span>, s.percentage);</span>
<span class="code-line">}</span>
<span class="code-line"> </span>
<span class="code-line"><span class="syn-func">printStudent</span>(s1); <span class="syn-comment">// s1 copied into s</span></span>
</div>
<button class="run-btn" id="value-run">โถ Run printStudent(s1)</button>
<div class="fn-frame copy" id="value-frame">
<div class="fn-title">inside printStudent() โ s is a separate copy</div>
<div class="fn-body" id="value-frame-body"></div>
</div>
<div class="console" id="value-console">Student Details:</div>
<p class="note" id="value-note"></p>
</div>
<!-- Passing by reference -->
<div class="tab-panel" data-tab="ref">
<div class="code-panel">
<span class="code-line"><span class="syn-keyword">void</span> <span class="syn-func">update</span>(<span class="syn-keyword">struct</span> Student *s) {</span>
<span class="code-line"> s->percentage += 5;</span>
<span class="code-line">}</span>
<span class="code-line"> </span>
<span class="code-line"><span class="syn-func">update</span>(&s1); <span class="syn-comment">// s points straight at s1</span></span>
</div>
<button class="run-btn" id="ref-run">โถ Run update(&s1)</button>
<div class="fn-frame ref" id="ref-frame">
<div class="fn-title">inside update() โ s โ &s1 (same memory)</div>
<div class="fn-body">s->percentage += 5;</div>
</div>
<div class="console" id="ref-console"></div>
<p class="note ok" id="ref-note"></p>
</div>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-cstructpass')
|| document.querySelector('.infographic-cstructpass');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') { dark = true; }
else if (bodyAttr === 'false') { dark = false; }
else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
function $(sel) { return wrapper.querySelector(sel); }
const rollInput = $('#s1-roll'), nameInput = $('#s1-name'), pctInput = $('#s1-pct');
const s1Card = $('#s1-card');
// ---- tabs ----
const tabs = wrapper.querySelectorAll('#pass-tabs .chip');
const panels = wrapper.querySelectorAll('.tab-panel');
tabs.forEach(tab => {
tab.addEventListener('click', () => {
tabs.forEach(t => t.classList.remove('is-active'));
tab.classList.add('is-active');
panels.forEach(p => p.classList.toggle('is-active', p.dataset.tab === tab.dataset.tab));
});
});
// ---- Passing by value ----
const valueFrame = $('#value-frame');
const valueFrameBody = $('#value-frame-body');
const valueConsole = $('#value-console');
const valueNote = $('#value-note');
$('#value-run').addEventListener('click', () => {
const roll = rollInput.value, name = nameInput.value, pct = parseFloat(pctInput.value || 0).toFixed(2);
valueFrame.classList.add('is-visible');
valueFrameBody.textContent = 's = { roll: ' + roll + ', name: "' + name + '", percentage: ' + pct + ' } (a copy)';
valueConsole.textContent = 'Student Details:\nRoll: ' + roll + '\nName: ' + name + '\nPercentage: ' + pct;
valueNote.textContent = 'โ ๏ธ s1 is untouched โ printStudent() only ever saw a copy.';
});
// ---- Passing by reference ----
const refFrame = $('#ref-frame');
const refConsole = $('#ref-console');
const refNote = $('#ref-note');
$('#ref-run').addEventListener('click', () => {
const roll = rollInput.value, name = nameInput.value;
const before = parseFloat(pctInput.value || 0);
const after = before + 5;
refFrame.classList.add('is-visible');
refConsole.textContent = 'Before update:\nRoll: ' + roll + '\nName: ' + name + '\nPercentage: ' + before.toFixed(2) +
'\n\nAfter update:\nRoll: ' + roll + '\nName: ' + name + '\nPercentage: ' + after.toFixed(2);
pctInput.value = after.toFixed(1);
s1Card.classList.remove('is-pulse');
void s1Card.offsetWidth;
s1Card.classList.add('is-pulse');
refNote.textContent = 'โ
s1.percentage really changed โ update() wrote straight through the pointer.';
});
})();
</script>