Skip to content
LAM
Read Home Blog
Make Projects HTML Tools Games
Touch grass Notes Resume Links
Home Blog HTML Projects
Tools Games Notes Resume Links
Back CSE240 Module 3: Pointers, Arrays & Structures Programming
Download Open
Show description 1,974 chars · Programming

CSE240 Module 3: Pointers, Arrays & Structures

CSE240 Module 3: Pointers, Arrays & Structures





CSE240 Module 3: Complete Study Guide

Pointers, Arrays, Strings & Structures in C








Quick Navigation


LO1: Pointer Basics

LO2: Arrays and Strings

LO3: Defining New Types

LO4: Contact Manager Demo

Practice Problems






LO1: Develop C Programs with Pointers


LO1.1: The Basic Concept of a Pointer

A pointer is a variable that stores the memory address of another variable. Think of it like a house address that tells you where to find something.



int x = 42; // regular variable storing value 42
int *ptr = &x; // pointer storing address of x

// Two key operators:
// & (address-of): gets the address of a variable
// * (dereference): accesses the value at an address





xValue: 42Addr: 0x1000

←
ptrValue: 0x1000Addr: 0x2000







Pointer declaration: type *name; where 'type' is what the pointer points to. The * in declaration is different from * in expressions (dereference).



LO1.2: Pointer Operators and Tracing


int a = 10, b = 20;
int *p1 = &a;
int *p2 = &b;

// Trace these operations:
*p1 = 30; // a becomes 30
p1 = p2; // p1 now points to b
*p1 = 40; // b becomes 40
a = *p2; // a becomes 40





Operation

a

b

p1 points to

p2 points to




Initial

10

20

a

b




*p1 = 30

30

20

a

b




p1 = p2

30

20

b

b




*p1 = 40

30

40

b

b




a = *p2

40

40

b

b





LO1.3: Pointers with Strings


char str[] = "Hello"; // array of characters
char *ptr = "World"; // pointer to string literal

// Key differences:
// str[] is modifiable, stored in stack
// *ptr points to read-only memory

str[0] = 'J'; // OK: now "Jello"
// ptr[0] = 'B'; // ERROR: can't modify literal

// String traversal with pointers:
char *p = str;
while (*p != '\0') {
printf("%c ", *p);
p++; // move to next character
}




String literals are stored in read-only memory.…

CSE240 Module 3: Pointers, Arrays & Structures

37,983 bytes · HTML source
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSE240 Module 3: Pointers, Arrays & Structures</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #0a0a0a 0%, #1a0f0f 100%);
            color: #e0e0e0;
            line-height: 1.6;
            min-height: 100vh;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        header {
            background: linear-gradient(135deg, #ff6b35 0%, #f72b1c 100%);
            padding: 30px 0;
            margin-bottom: 40px;
            box-shadow: 0 5px 20px rgba(255, 107, 53, 0.3);
        }

        h1 {
            text-align: center;
            font-size: 2.5em;
            color: #fff;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
        }

        .subtitle {
            text-align: center;
            color: #ffd4c4;
            margin-top: 10px;
        }

        nav {
            background: #1a1a1a;
            padding: 20px;
            border-radius: 10px;
            margin-bottom: 30px;
            border: 1px solid #ff6b35;
        }

        nav h2 {
            color: #ff6b35;
            margin-bottom: 15px;
        }

        nav ul {
            list-style: none;
            padding-left: 20apterecursivelyperience;
        }

        nav li {
            margin: 8px 0;
        }

        nav a {
            color: #ffa500;
            text-decoration: none;
            transition: all 0.3s;
        }

        nav a:hover {
            color: #ff6b35;
            padding-left: 5px;
        }

        .section {
            background: #1a1a1a;
            padding: 30px;
            margin-bottom: 30px;
            border-radius: 10px;
            border-left: 4px solid #ff6b35;
        }

        h2 {
            color: #ff6b35;
            margin-bottom: 20px;
            font-size: 1.8em;
            border-bottom: 2px solid #333;
            padding-bottom: 10px;
        }

        h3 {
            color: #ffa500;
            margin: 20px 0 15px 0;
            font-size: 1.3em;
        }

        h4 {
            color: #ff8c42;
            margin: 15px 0 10px 0;
        }

        .code-block {
            background: #0a0a0a;
            border: 1px solid #ff6b35;
            border-radius: 5px;
            padding: 15px;
            margin: 15px 0;
            overflow-x: auto;
            position: relative;
        }

        .code-block pre {
            color: #f0f0f0;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.5;
        }

        .code-block .comment {
            color: #7a7a7a;
        }

        .code-block .keyword {
            color: #ff6b35;
            font-weight: bold;
        }

        .code-block .string {
            color: #98c379;
        }

        .code-block .number {
            color: #d19a66;
        }

        .code-block .function {
            color: #61afef;
        }

        .important-box {
            background: linear-gradient(135deg, #2a1515 0%, #1a0a0a 100%);
            border: 2px solid #ff6b35;
            border-radius: 8px;
            padding: 20px;
            margin: 20px 0;
        }

        .important-box::before {
            content: "⚠️ Important: ";
            color: #ff6b35;
            font-weight: bold;
        }

        .example-box {
            background: #0f0f0f;
            border-left: 4px solid #ffa500;
            padding: 15px;
            margin: 20px 0;
        }

        .memory-diagram {
            background: #0a0a0a;
            border: 2px solid #ff6b35;
            border-radius: 5px;
            padding: 20px;
            margin: 20px 0;
            font-family: monospace;
        }

        .memory-cell {
            display: inline-block;
            background: #1a1a1a;
            border: 1px solid #ffa500;
            padding: 10px 15px;
            margin: 5px;
            min-width: 80px;
            text-align: center;
        }

        .pointer-arrow {
            color: #ff6b35;
            font-size: 1.5em;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }

        th, td {
            padding: 12px;
            text-align: left;
            border: 1px solid #333;
        }

        th {
            background: #ff6b35;
            color: #fff;
        }

        td {
            background: #0a0a0a;
        }

        .quiz-question {
            background: #1a0f0f;
            border: 1px solid #ff6b35;
            padding: 15px;
            margin: 20px 0;
            border-radius: 5px;
        }

        .quiz-answer {
            display: none;
            background: #0a1a0a;
            padding: 10px;
            margin-top: 10px;
            border-left: 3px solid #4caf50;
        }

        .reveal-btn {
            background: #ff6b35;
            color: #fff;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 10px;
            transition: background 0.3s;
        }

        .reveal-btn:hover {
            background: #ff8c42;
        }

        .tip {
            background: linear-gradient(135deg, #1a1a0a 0%, #0a0a0a 100%);
            border-left: 4px solid #4caf50;
            padding: 15px;
            margin: 20px 0;
        }

        .tip::before {
            content: "💡 Tip: ";
            color: #4caf50;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <header>
        <div class="container">
            <h1>CSE240 Module 3: Complete Study Guide</h1>
            <div class="subtitle">Pointers, Arrays, Strings & Structures in C</div>
        </div>
    </header>

    <div class="container">
        <nav>
            <h2>Quick Navigation</h2>
            <ul>
                <li><a href="#pointer-basics">LO1: Pointer Basics</a></li>
                <li><a href="#arrays-strings">LO2: Arrays and Strings</a></li>
                <li><a href="#new-types">LO3: Defining New Types</a></li>
                <li><a href="#contact-manager">LO4: Contact Manager Demo</a></li>
                <li><a href="#practice">Practice Problems</a></li>
            </ul>
        </nav>

        <!-- LO1: Pointer Basics -->
        <section id="pointer-basics" class="section">
            <h2>LO1: Develop C Programs with Pointers</h2>
            
            <h3>LO1.1: The Basic Concept of a Pointer</h3>
            <p>A pointer is a variable that stores the memory address of another variable. Think of it like a house address that tells you where to find something.</p>
            
            <div class="code-block">
                <pre><span class="keyword">int</span> x = <span class="number">42</span>;      <span class="comment">// regular variable storing value 42</span>
<span class="keyword">int</span> *ptr = &x;  <span class="comment">// pointer storing address of x</span>

<span class="comment">// Two key operators:</span>
<span class="comment">// & (address-of): gets the address of a variable</span>
<span class="comment">// * (dereference): accesses the value at an address</span></pre>
            </div>

            <div class="memory-diagram">
                <div style="display: flex; align-items: center;">
                    <div class="memory-cell">x<br>Value: 42<br>Addr: 0x1000</div>
                    <span class="pointer-arrow">←</span>
                    <div class="memory-cell">ptr<br>Value: 0x1000<br>Addr: 0x2000</div>
                </div>
            </div>

            <div class="important-box">
                Pointer declaration: type *name; where 'type' is what the pointer points to. The * in declaration is different from * in expressions (dereference).
            </div>

            <h3>LO1.2: Pointer Operators and Tracing</h3>
            <div class="code-block">
                <pre><span class="keyword">int</span> a = <span class="number">10</span>, b = <span class="number">20</span>;
<span class="keyword">int</span> *p1 = &a;
<span class="keyword">int</span> *p2 = &b;

<span class="comment">// Trace these operations:</span>
*p1 = <span class="number">30</span>;        <span class="comment">// a becomes 30</span>
p1 = p2;         <span class="comment">// p1 now points to b</span>
*p1 = <span class="number">40</span>;        <span class="comment">// b becomes 40</span>
a = *p2;         <span class="comment">// a becomes 40</span></pre>
            </div>

            <table>
                <tr>
                    <th>Operation</th>
                    <th>a</th>
                    <th>b</th>
                    <th>p1 points to</th>
                    <th>p2 points to</th>
                </tr>
                <tr>
                    <td>Initial</td>
                    <td>10</td>
                    <td>20</td>
                    <td>a</td>
                    <td>b</td>
                </tr>
                <tr>
                    <td>*p1 = 30</td>
                    <td>30</td>
                    <td>20</td>
                    <td>a</td>
                    <td>b</td>
                </tr>
                <tr>
                    <td>p1 = p2</td>
                    <td>30</td>
                    <td>20</td>
                    <td>b</td>
                    <td>b</td>
                </tr>
                <tr>
                    <td>*p1 = 40</td>
                    <td>30</td>
                    <td>40</td>
                    <td>b</td>
                    <td>b</td>
                </tr>
                <tr>
                    <td>a = *p2</td>
                    <td>40</td>
                    <td>40</td>
                    <td>b</td>
                    <td>b</td>
                </tr>
            </table>

            <h3>LO1.3: Pointers with Strings</h3>
            <div class="code-block">
                <pre><span class="keyword">char</span> str[] = <span class="string">"Hello"</span>;     <span class="comment">// array of characters</span>
<span class="keyword">char</span> *ptr = <span class="string">"World"</span>;      <span class="comment">// pointer to string literal</span>

<span class="comment">// Key differences:</span>
<span class="comment">// str[] is modifiable, stored in stack</span>
<span class="comment">// *ptr points to read-only memory</span>

str[<span class="number">0</span>] = <span class="string">'J'</span>;              <span class="comment">// OK: now "Jello"</span>
<span class="comment">// ptr[0] = 'B';          // ERROR: can't modify literal</span>

<span class="comment">// String traversal with pointers:</span>
<span class="keyword">char</span> *p = str;
<span class="keyword">while</span> (*p != <span class="string">'\0'</span>) {
    printf(<span class="string">"%c "</span>, *p);
    p++;                   <span class="comment">// move to next character</span>
}</pre>
            </div>

            <div class="tip">
                String literals are stored in read-only memory. Always use char arrays if you need to modify the string!
            </div>

            <h3>LO1.4: Pointers with Multi-Dimensional Arrays</h3>
            <div class="code-block">
                <pre><span class="keyword">int</span> matrix[<span class="number">3</span>][<span class="number">4</span>] = {
    {<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>},
    {<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>},
    {<span class="number">9</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>}
};

<span class="keyword">int</span> (*ptr)[<span class="number">4</span>] = matrix;   <span class="comment">// pointer to array of 4 ints</span>

<span class="comment">// Accessing elements:</span>
<span class="comment">// matrix[i][j] == *(*(matrix + i) + j)</span>
<span class="comment">// ptr[i][j]    == *(*(ptr + i) + j)</span>

printf(<span class="string">"%d\n"</span>, ptr[<span class="number">1</span>][<span class="number">2</span>]);      <span class="comment">// prints 7</span>
printf(<span class="string">"%d\n"</span>, *(*(ptr+<span class="number">1</span>)+<span class="number">2</span>)); <span class="comment">// also prints 7</span></pre>
            </div>

            <div class="important-box">
                Array name is a constant pointer to the first element. For 2D arrays, it's a pointer to the first row (which is itself an array).
            </div>

            <h3>LO1.5: Pointers vs Unsigned Integers</h3>
            <div class="code-block">
                <pre><span class="comment">// Pointers can be cast to/from integers, but it's dangerous!</span>
<span class="keyword">int</span> x = <span class="number">100</span>;
<span class="keyword">int</span> *ptr = &x;
<span class="keyword">unsigned int</span> addr = (<span class="keyword">unsigned int</span>)ptr;  <span class="comment">// store address as number</span>

<span class="comment">// Key differences:</span>
<span class="comment">// 1. Pointers have type information</span>
<span class="comment">// 2. Pointer arithmetic uses sizeof(type)</span>
<span class="comment">// 3. Pointers can be dereferenced</span>
<span class="comment">// 4. Size varies (32-bit vs 64-bit systems)</span>

<span class="keyword">int</span> arr[] = {<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>};
<span class="keyword">int</span> *p = arr;
p++;        <span class="comment">// moves by sizeof(int) bytes, not 1</span>
            <span class="comment">// now p points to arr[1]</span></pre>
            </div>
        </section>

        <!-- LO2: Arrays and Strings -->
        <section id="arrays-strings" class="section">
            <h2>LO2: Arrays of Strings</h2>
            
            <h3>LO2.1: String Literal Limitations</h3>
            <div class="code-block">
                <pre><span class="comment">// String literals are READ-ONLY!</span>
<span class="keyword">char</span> *str1 = <span class="string">"Hello"</span>;     <span class="comment">// points to read-only memory</span>
<span class="keyword">char</span> str2[] = <span class="string">"Hello"</span>;    <span class="comment">// creates modifiable copy</span>

<span class="comment">// str1[0] = 'J';         // CRASH! Segmentation fault</span>
str2[<span class="number">0</span>] = <span class="string">'J'</span>;            <span class="comment">// OK! str2 is now "Jello"</span>

<span class="comment">// Limitations:</span>
<span class="comment">// 1. Can't modify string literals</span>
<span class="comment">// 2. Multiple identical literals may share memory</span>
<span class="comment">// 3. Size is fixed at compile time</span></pre>
            </div>

            <h3>LO2.2: Array of Strings Implementation</h3>
            <div class="code-block">
                <pre><span class="comment">// Method 1: 2D char array (fixed size for each string)</span>
<span class="keyword">char</span> names1[<span class="number">5</span>][<span class="number">20</span>] = {
    <span class="string">"Alice"</span>,
    <span class="string">"Bob"</span>,
    <span class="string">"Charlie"</span>,
    <span class="string">"David"</span>,
    <span class="string">"Eve"</span>
};

<span class="comment">// Method 2: Array of char pointers (variable sizes)</span>
<span class="keyword">char</span> *names2[<span class="number">5</span>] = {
    <span class="string">"Alice"</span>,
    <span class="string">"Bob"</span>,
    <span class="string">"Charlie"</span>,
    <span class="string">"David"</span>,
    <span class="string">"Eve"</span>
};

<span class="comment">// Accessing strings:</span>
printf(<span class="string">"%s\n"</span>, names1[<span class="number">2</span>]);    <span class="comment">// prints "Charlie"</span>
printf(<span class="string">"%c\n"</span>, names2[<span class="number">1</span>][<span class="number">0</span>]); <span class="comment">// prints 'B'</span>

<span class="comment">// Modifying:</span>
strcpy(names1[<span class="number">0</span>], <span class="string">"Alan"</span>);   <span class="comment">// OK for 2D array</span>
names2[<span class="number">0</span>] = <span class="string">"Alan"</span>;          <span class="comment">// OK: change pointer</span>
<span class="comment">// names2[0][0] = 'X';       // ERROR: literal is read-only</span></pre>
            </div>

            <div class="example-box">
                <strong>Memory Layout Comparison:</strong><br>
                2D Array: Contiguous memory, each string gets 20 bytes<br>
                Pointer Array: Pointers in array, strings scattered in memory<br>
                <br>
                2D Array wastes space but allows modification<br>
                Pointer Array saves space but strings are read-only
            </div>
        </section>

        <!-- LO3: Defining New Types -->
        <section id="new-types" class="section">
            <h2>LO3: Defining New Types in C</h2>
            
            <h3>LO3.1: Constants with #define and const</h3>
            <div class="code-block">
                <pre><span class="comment">// Preprocessor macros (compile-time substitution)</span>
<span class="keyword">#define</span> MAX_SIZE <span class="number">100</span>
<span class="keyword">#define</span> PI <span class="number">3.14159</span>
<span class="keyword">#define</span> SQUARE(x) ((x) * (x))   <span class="comment">// macro with parameter</span>

<span class="comment">// const keyword (runtime constant)</span>
<span class="keyword">const int</span> max_items = <span class="number">50</span>;
<span class="keyword">const double</span> pi = <span class="number">3.14159</span>;

<span class="comment">// Key differences:</span>
<span class="comment">// #define: no type checking, just text replacement</span>
<span class="comment">// const: type-safe, occupies memory, can't be modified</span>

<span class="keyword">int</span> array[MAX_SIZE];        <span class="comment">// OK with #define</span>
<span class="comment">// int array2[max_items];   // ERROR in C (OK in C++)</span></pre>
            </div>

            <div class="important-box">
                Always use parentheses in macros! SQUARE(x+1) without parentheses would expand to x+1*x+1, not (x+1)*(x+1)
            </div>

            <h3>LO3.2: Typedef and Enumerations</h3>
            <div class="code-block">
                <pre><span class="comment">// typedef: create alias for existing type</span>
<span class="keyword">typedef unsigned long</span> ulong;
<span class="keyword">typedef int</span> bool;
<span class="keyword">typedef char</span> String[<span class="number">100</span>];    <span class="comment">// array type</span>

ulong bigNumber = <span class="number">1000000</span>;
String name;                 <span class="comment">// same as char name[100]</span>

<span class="comment">// enum: create named integer constants</span>
<span class="keyword">enum</span> Days {
    MONDAY,      <span class="comment">// 0</span>
    TUESDAY,     <span class="comment">// 1</span>
    WEDNESDAY,   <span class="comment">// 2</span>
    THURSDAY,    <span class="comment">// 3</span>
    FRIDAY = <span class="number">10</span>, <span class="comment">// 10 (custom value)</span>
    SATURDAY,    <span class="comment">// 11</span>
    SUNDAY       <span class="comment">// 12</span>
};

<span class="keyword">enum</span> Days today = MONDAY;

<span class="comment">// typedef with enum for cleaner syntax</span>
<span class="keyword">typedef enum</span> {
    FALSE,
    TRUE
} Boolean;

Boolean isReady = FALSE;</pre>
            </div>

            <h3>LO3.3: Structures (Composite Data)</h3>
            <div class="code-block">
                <pre><span class="comment">// Define a structure type</span>
<span class="keyword">struct</span> Person {
    <span class="keyword">char</span> name[<span class="number">50</span>];
    <span class="keyword">int</span> age;
    <span class="keyword">float</span> height;
    <span class="keyword">char</span> email[<span class="number">100</span>];
};

<span class="comment">// Create structure variables</span>
<span class="keyword">struct</span> Person person1;
<span class="keyword">struct</span> Person person2 = {<span class="string">"Alice"</span>, <span class="number">25</span>, <span class="number">5.6</span>, <span class="string">"alice@email.com"</span>};

<span class="comment">// Access members with dot operator</span>
person1.age = <span class="number">30</span>;
strcpy(person1.name, <span class="string">"Bob"</span>);

<span class="comment">// typedef for cleaner syntax</span>
<span class="keyword">typedef struct</span> {
    <span class="keyword">double</span> x;
    <span class="keyword">double</span> y;
} Point;

Point p1 = {<span class="number">3.5</span>, <span class="number">7.2</span>};

<span class="comment">// Structures with pointers</span>
<span class="keyword">struct</span> Person *ptr = &person1;
ptr->age = <span class="number">31</span>;              <span class="comment">// arrow operator for pointer access</span>
(*ptr).age = <span class="number">31</span>;            <span class="comment">// equivalent using dereference</span>

<span class="comment">// Nested structures</span>
<span class="keyword">typedef struct</span> {
    Point center;
    <span class="keyword">double</span> radius;
} Circle;

Circle c = {{<span class="number">0.0</span>, <span class="number">0.0</span>}, <span class="number">5.0</span>};</pre>
            </div>

            <div class="tip">
                Arrow operator (->): ptr->member is shorthand for (*ptr).member. Use it when accessing structure members through a pointer.
            </div>
        </section>

        <!-- LO4: Contact Manager Demo -->
        <section id="contact-manager" class="section">
            <h2>LO4: Complete Contact Manager Implementation</h2>
            <p>This demonstrates all concepts: flow control, arrays, pointers, structs, and I/O</p>
            
            <div class="code-block">
                <pre><span class="keyword">#include</span> <span class="string">&lt;stdio.h&gt;</span>
<span class="keyword">#include</span> <span class="string">&lt;stdlib.h&gt;</span>
<span class="keyword">#include</span> <span class="string">&lt;string.h&gt;</span>

<span class="keyword">#define</span> MAX_CONTACTS <span class="number">100</span>
<span class="keyword">#define</span> NAME_LENGTH <span class="number">50</span>
<span class="keyword">#define</span> PHONE_LENGTH <span class="number">20</span>

<span class="comment">// Structure definition</span>
<span class="keyword">typedef struct</span> {
    <span class="keyword">char</span> name[NAME_LENGTH];
    <span class="keyword">char</span> phone[PHONE_LENGTH];
    <span class="keyword">int</span> id;
} Contact;

<span class="comment">// Global array and counter</span>
Contact contacts[MAX_CONTACTS];
<span class="keyword">int</span> contactCount = <span class="number">0</span>;

<span class="comment">// Function prototypes</span>
<span class="keyword">void</span> displayMenu();
<span class="keyword">void</span> insertContact();
<span class="keyword">void</span> searchContact();
<span class="keyword">void</span> deleteContact();
<span class="keyword">void</span> displayAll();

<span class="keyword">int</span> <span class="function">main</span>() {
    <span class="keyword">int</span> choice;
    
    <span class="keyword">while</span> (<span class="number">1</span>) {
        displayMenu();
        printf(<span class="string">"Enter choice: "</span>);
        scanf(<span class="string">"%d"</span>, &choice);
        getchar(); <span class="comment">// consume newline</span>
        
        <span class="keyword">switch</span> (choice) {
            <span class="keyword">case</span> <span class="number">1</span>:
                insertContact();
                <span class="keyword">break</span>;
            <span class="keyword">case</span> <span class="number">2</span>:
                searchContact();
                <span class="keyword">break</span>;
            <span class="keyword">case</span> <span class="number">3</span>:
                deleteContact();
                <span class="keyword">break</span>;
            <span class="keyword">case</span> <span class="number">4</span>:
                displayAll();
                <span class="keyword">break</span>;
            <span class="keyword">case</span> <span class="number">5</span>:
                printf(<span class="string">"Exiting...\n"</span>);
                <span class="keyword">return</span> <span class="number">0</span>;
            <span class="keyword">default</span>:
                printf(<span class="string">"Invalid choice!\n"</span>);
        }
    }
}

<span class="keyword">void</span> <span class="function">displayMenu</span>() {
    printf(<span class="string">"\n=== Contact Manager ===\n"</span>);
    printf(<span class="string">"1. Insert Contact\n"</span>);
    printf(<span class="string">"2. Search Contact\n"</span>);
    printf(<span class="string">"3. Delete Contact\n"</span>);
    printf(<span class="string">"4. Display All\n"</span>);
    printf(<span class="string">"5. Exit\n"</span>);
}

<span class="keyword">void</span> <span class="function">insertContact</span>() {
    <span class="keyword">if</span> (contactCount >= MAX_CONTACTS) {
        printf(<span class="string">"Contact list full!\n"</span>);
        <span class="keyword">return</span>;
    }
    
    Contact *newContact = &contacts[contactCount];
    
    printf(<span class="string">"Enter name: "</span>);
    fgets(newContact->name, NAME_LENGTH, stdin);
    newContact->name[strcspn(newContact->name, <span class="string">"\n"</span>)] = <span class="string">'\0'</span>;
    
    printf(<span class="string">"Enter phone: "</span>);
    fgets(newContact->phone, PHONE_LENGTH, stdin);
    newContact->phone[strcspn(newContact->phone, <span class="string">"\n"</span>)] = <span class="string">'\0'</span>;
    
    newContact->id = contactCount + <span class="number">1</span>;
    contactCount++;
    
    printf(<span class="string">"Contact added successfully! ID: %d\n"</span>, newContact->id);
}

<span class="keyword">void</span> <span class="function">searchContact</span>() {
    <span class="keyword">char</span> searchName[NAME_LENGTH];
    printf(<span class="string">"Enter name to search: "</span>);
    fgets(searchName, NAME_LENGTH, stdin);
    searchName[strcspn(searchName, <span class="string">"\n"</span>)] = <span class="string">'\0'</span>;
    
    <span class="keyword">int</span> found = <span class="number">0</span>;
    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < contactCount; i++) {
        <span class="keyword">if</span> (strstr(contacts[i].name, searchName) != NULL) {
            printf(<span class="string">"Found: ID=%d, Name=%s, Phone=%s\n"</span>, 
                   contacts[i].id, contacts[i].name, contacts[i].phone);
            found = <span class="number">1</span>;
        }
    }
    
    <span class="keyword">if</span> (!found) {
        printf(<span class="string">"No contacts found.\n"</span>);
    }
}

<span class="keyword">void</span> <span class="function">deleteContact</span>() {
    <span class="keyword">int</span> id;
    printf(<span class="string">"Enter ID to delete: "</span>);
    scanf(<span class="string">"%d"</span>, &id);
    getchar();
    
    <span class="keyword">int</span> index = -<span class="number">1</span>;
    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < contactCount; i++) {
        <span class="keyword">if</span> (contacts[i].id == id) {
            index = i;
            <span class="keyword">break</span>;
        }
    }
    
    <span class="keyword">if</span> (index == -<span class="number">1</span>) {
        printf(<span class="string">"Contact not found.\n"</span>);
        <span class="keyword">return</span>;
    }
    
    <span class="comment">// Shift elements left to fill gap</span>
    <span class="keyword">for</span> (<span class="keyword">int</span> i = index; i < contactCount - <span class="number">1</span>; i++) {
        contacts[i] = contacts[i + <span class="number">1</span>];
    }
    contactCount--;
    
    printf(<span class="string">"Contact deleted successfully.\n"</span>);
}

<span class="keyword">void</span> <span class="function">displayAll</span>() {
    <span class="keyword">if</span> (contactCount == <span class="number">0</span>) {
        printf(<span class="string">"No contacts to display.\n"</span>);
        <span class="keyword">return</span>;
    }
    
    printf(<span class="string">"\n=== All Contacts ===\n"</span>);
    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < contactCount; i++) {
        printf(<span class="string">"ID: %d | Name: %s | Phone: %s\n"</span>,
               contacts[i].id, contacts[i].name, contacts[i].phone);
    }
}</pre>
            </div>
        </section>

        <!-- Practice Problems -->
        <section id="practice" class="section">
            <h2>Practice Problems & Test Prep</h2>
            
            <div class="quiz-question">
                <h4>Question 1: Pointer Basics</h4>
                <p>What will be the output of this code?</p>
                <div class="code-block">
                    <pre><span class="keyword">int</span> x = <span class="number">5</span>, y = <span class="number">10</span>;
<span class="keyword">int</span> *p = &x;
<span class="keyword">int</span> *q = &y;
*p = *q;
q = p;
*q = <span class="number">15</span>;
printf(<span class="string">"%d %d"</span>, x, y);</pre>
                </div>
                <button class="reveal-btn" onclick="toggleAnswer(this)">Show Answer</button>
                <div class="quiz-answer">
                    <strong>Answer: 15 10</strong><br>
                    Explanation:<br>
                    1. *p = *q → x = 10 (copy value of y to x)<br>
                    2. q = p → q now points to x<br>
                    3. *q = 15 → x = 15 (q points to x)<br>
                    4. Final: x = 15, y = 10
                </div>
            </div>

            <div class="quiz-question">
                <h4>Question 2: String Pointers</h4>
                <p>Which of these will cause a runtime error?</p>
                <div class="code-block">
                    <pre>A) <span class="keyword">char</span> str[] = <span class="string">"Hello"</span>; str[<span class="number">0</span>] = <span class="string">'J'</span>;
B) <span class="keyword">char</span> *str = <span class="string">"Hello"</span>; str = <span class="string">"World"</span>;
C) <span class="keyword">char</span> *str = <span class="string">"Hello"</span>; str[<span class="number">0</span>] = <span class="string">'J'</span>;
D) <span class="keyword">char</span> str[<span class="number">10</span>] = <span class="string">"Hello"</span>; strcpy(str, <span class="string">"World"</span>);</pre>
                </div>
                <button class="reveal-btn" onclick="toggleAnswer(this)">Show Answer</button>
                <div class="quiz-answer">
                    <strong>Answer: C</strong><br>
                    Explanation: Option C tries to modify a string literal (read-only memory), causing segmentation fault. A is OK (array is modifiable), B is OK (changing pointer, not string), D is OK (copying to array).
                </div>
            </div>

            <div class="quiz-question">
                <h4>Question 3: Structures</h4>
                <p>Fill in the blank to correctly access the x coordinate:</p>
                <div class="code-block">
                    <pre><span class="keyword">typedef struct</span> {
    <span class="keyword">float</span> x, y;
} Point;

<span class="keyword">typedef struct</span> {
    Point center;
    <span class="keyword">float</span> radius;
} Circle;

Circle c = {{<span class="number">3.0</span>, <span class="number">4.0</span>}, <span class="number">5.0</span>};
Circle *ptr = &c;

<span class="comment">// Access center's x coordinate through ptr:</span>
<span class="keyword">float</span> xCoord = _________;</pre>
                </div>
                <button class="reveal-btn" onclick="toggleAnswer(this)">Show Answer</button>
                <div class="quiz-answer">
                    <strong>Answer: ptr->center.x</strong><br>
                    Explanation: Use arrow operator for pointer, then dot for nested struct. Alternatives: (*ptr).center.x or c.center.x
                </div>
            </div>

            <div class="quiz-question">
                <h4>Question 4: Array of Strings</h4>
                <p>What's the key difference between these declarations?</p>
                <div class="code-block">
                    <pre><span class="keyword">char</span> names1[<span class="number">3</span>][<span class="number">10</span>];
<span class="keyword">char</span> *names2[<span class="number">3</span>];</pre>
                </div>
                <button class="reveal-btn" onclick="toggleAnswer(this)">Show Answer</button>
                <div class="quiz-answer">
                    <strong>Answer:</strong><br>
                    names1: 2D array, 30 bytes total, all strings modifiable, fixed size<br>
                    names2: Array of pointers, variable string sizes, literals are read-only<br>
                    Memory: names1 is contiguous, names2 has pointers pointing to scattered strings
                </div>
            </div>

            <div class="quiz-question">
                <h4>Question 5: Pointer Arithmetic</h4>
                <p>Given an int array, what does ptr+3 mean?</p>
                <div class="code-block">
                    <pre><span class="keyword">int</span> arr[] = {<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>};
<span class="keyword">int</span> *ptr = arr;</pre>
                </div>
                <button class="reveal-btn" onclick="toggleAnswer(this)">Show Answer</button>
                <div class="quiz-answer">
                    <strong>Answer:</strong> ptr+3 points to arr[3] (value 40)<br>
                    Explanation: Pointer arithmetic scales by sizeof(type). Since int is typically 4 bytes, ptr+3 advances by 3*4=12 bytes from the start of the array, reaching the 4th element.
                </div>
            </div>

            <h3>Key Concepts to Remember</h3>
            <div class="important-box">
                <ul style="list-style: none; padding: 0;">
                    <li>✓ & gets address, * dereferences (accesses value)</li>
                    <li>✓ Arrays decay to pointers when passed to functions</li>
                    <li>✓ String literals are read-only, use char arrays for modification</li>
                    <li>✓ Arrow operator (->) for pointer to struct member access</li>
                    <li>✓ Pointer arithmetic scales by sizeof(pointed type)</li>
                    <li>✓ typedef creates type aliases, enum creates named constants</li>
                    <li>✓ #define is preprocessor substitution, const is runtime constant</li>
                </ul>
            </div>

            <h3>Common Pitfalls to Avoid</h3>
            <div class="tip">
                1. Never modify string literals (char *str = "hello"; str[0] = 'H'; // CRASH!)<br>
                2. Always check array bounds - C doesn't do it for you<br>
                3. Initialize pointers before use (avoid wild pointers)<br>
                4. Remember strcpy needs destination with enough space<br>
                5. Use parentheses in macros: #define SQUARE(x) ((x)*(x))<br>
                6. Don't return pointers to local variables from functions
            </div>
        </section>
    </div>

    <script>
        function toggleAnswer(button) {
            const answer = button.nextElementSibling;
            if (answer.style.display === 'none' || answer.style.display === '') {
                answer.style.display = 'block';
                button.textContent = 'Hide Answer';
            } else {
                answer.style.display = 'none';
                button.textContent = 'Show Answer';
            }
        }
    </script>
</body>
</html>