Menu
Coddy logo textTech

Recap: Pointer Basics

Part of the Logic & Flow section of Coddy's C journey — lesson 6 of 63.

challenge icon

Challenge

Easy

Write a C program that demonstrates the complete pointer workflow by working with multiple data types. Your program should:

  1. Declare an integer variable named age and initialize it with the value 25
  2. Declare a character variable named grade and initialize it with the value 'A'
  3. Declare a float variable named temperature and initialize it with the value 98.6
  4. 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
  5. Use the address-of operator to assign the addresses of the variables to their respective pointers
  6. 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)
  7. Modify the original variables through their pointers:
    • Change age to 30 using age_ptr
    • Change grade to 'B' using grade_ptr
    • Change temperature to 99.5 using temp_ptr
  8. 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.5

This 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