Recap: Pointer Basics
Part of the Logic & Flow section of Coddy's C journey — lesson 6 of 63.
Challenge
EasyWrite a C program that demonstrates the complete pointer workflow by working with multiple data types. Your program should:
- Declare an integer variable named
ageand initialize it with the value 25 - Declare a character variable named
gradeand initialize it with the value 'A' - Declare a float variable named
temperatureand initialize it with the value 98.6 - Declare three corresponding pointers:
- A pointer to an integer named
age_ptr - A pointer to a character named
grade_ptr - A pointer to a float named
temp_ptr
- A pointer to an integer named
- Use the address-of operator to assign the addresses of the variables to their respective pointers
- Use the dereference operator to print all three values through their pointers in the following format:
- Age: [value]
- Grade: [value]
- Temperature: [value] (with exactly 1 decimal place)
- Modify the original variables through their pointers:
- Change
ageto 30 usingage_ptr - Change
gradeto 'B' usinggrade_ptr - Change
temperatureto 99.5 usingtemp_ptr
- Change
- Print the modified values directly from the original variables (not through pointers) in the same format as step 6
Your output should display the results in the following format:
Age: 25
Grade: A
Temperature: 98.6
Age: 30
Grade: B
Temperature: 99.5This challenge tests your understanding of pointer declaration, address assignment, dereferencing for both reading and writing values, and demonstrates that pointers provide direct access to the original variables' memory locations.
Try it yourself
#include <stdio.h>
int main() {
// TODO: Write your code here
// Declare variables and pointers
// Assign addresses to pointers
// Print values through pointers
// Modify values through pointers
// Print modified values from original variables
return 0;
}All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions