📖 Scenario: Imagine you are building a program that stores names of students in a class. Sometimes the class size changes, and you don't know the number of students before running the program.
🎯 Goal: You will create a simple C program that uses dynamic memory to store student names. This will show why dynamic memory is needed when the amount of data is not fixed.
📋 What You'll Learn
Create a pointer to hold student names dynamically
Use malloc to allocate memory for 3 student names
Add a variable to store the number of students
Use a loop to assign names to the allocated memory
Print the stored student names
💡 Why This Matters
🌍 Real World
Programs often need to handle data that changes size, like user input lists or files. Dynamic memory helps manage this efficiently.
💼 Career
Understanding dynamic memory is essential for C programmers working on system software, embedded devices, or performance-critical applications.
Progress0 / 4 steps
1
Create a pointer for student names
Declare a pointer called students of type char ** to hold multiple student names.
C
Hint
Use char **students; to declare a pointer to an array of strings.
2
Set number of students and allocate memory
Create an integer variable called num_students and set it to 3. Then use malloc to allocate memory for students to hold 3 pointers to char.
C
Hint
Use malloc to allocate memory for 3 pointers to strings.
3
Allocate memory for each student name and assign names
Use a for loop with variable i from 0 to num_students - 1. Inside the loop, allocate memory for each student name to hold 20 characters using malloc. Then assign these exact names: "Anna", "Ben", "Cara" to students[0], students[1], and students[2] respectively using strcpy.
C
Hint
Use a loop to allocate memory for each name, then use strcpy to copy the names.
4
Print the student names
Use a for loop with variable i from 0 to num_students - 1. Inside the loop, print each student name stored in students[i] using printf.
C
Hint
Use a loop and printf to show each name.
Practice
(1/5)
1. Why do we need dynamic memory allocation in C programs?
easy
A. To allocate memory during program execution when size is unknown
B. To make the program run faster by using fixed memory
C. To avoid using pointers in the program
D. To store only constant values in memory
Solution
Step 1: Understand static vs dynamic memory
Static memory is fixed at compile time, so it cannot change size during execution.
Step 2: Identify the need for flexible memory
Dynamic memory allows programs to request memory as needed during runtime, useful when data size is unknown beforehand.
Final Answer:
To allocate memory during program execution when size is unknown -> Option A
Quick Check:
Dynamic memory = flexible size allocation [OK]
Hint: Dynamic memory = memory decided while program runs [OK]
Common Mistakes:
Confusing dynamic memory with faster execution
Thinking dynamic memory avoids pointers
Believing dynamic memory stores only constants
2. Which of the following is the correct syntax to allocate memory dynamically for 10 integers in C?
easy
A. int *ptr = malloc(10);
B. int *ptr = malloc(10 * sizeof(int));
C. int ptr = malloc(10 * sizeof(int));
D. int *ptr = malloc(sizeof(int));
Solution
Step 1: Recall malloc syntax
malloc requires the number of bytes to allocate, so multiply count by size of type.
Step 2: Check each option
int *ptr = malloc(10 * sizeof(int)); correctly multiplies 10 by sizeof(int) and assigns to int pointer.
Final Answer:
int *ptr = malloc(10 * sizeof(int)); -> Option B
Quick Check:
malloc size = count * sizeof(type) [OK]
Hint: Always multiply count by sizeof(type) in malloc [OK]
Common Mistakes:
Not multiplying by sizeof(int)
Assigning malloc result to non-pointer
Allocating too little memory
3. What will be the output of this C code snippet?