-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintro_pointers.c
More file actions
41 lines (38 loc) · 1.48 KB
/
Copy pathintro_pointers.c
File metadata and controls
41 lines (38 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Filename: intro_pointers.c
// Author: Sam Mansouri
// Date: 10/15/2025
// Description: Demonstrates basic pointer usage in C.
// ===== Includes =====
#include <stdio.h>
#include <string.h>
#include "hwlib.h"
// Function: run_intro_pointers
// Description: Demonstrates basic pointer usage
void run_intro_pointers() {
// Step 1
// =================> TODO: Declare an int a and set it to 42
int a = 42;
// =================> TODO: Create a pointer p to a
int* p = &a;
// =================> TODO: Use p to reassign a to the value of 100
*p = 100;
// =================> TODO: Print a and *p, and the address of a
printf("This is the value of a: %d\nThis is the value of *p: %d\nThis is the address of a: %p\n", a, *p, (void*)&a);
// Step 2
// =================> TODO: Create another pointer q and set it equal to p
int* q = p;
// =================> TODO: Use q to change value of a
*q = 314;
// =================> TODO: Print a, *p, and *q
printf("This is the value of a: %d\nThis is the value of *p: %d\nThis is the value of *q: %d\n", a, *p, *q);
// Step 3
// =================> TODO: Declare b = 200
int b = 200;
printf("Original b = %d\n", b);
// =================> TODO: Reassign p to point at b
p = &b;
// =================> TODO: Use p to reassign b to be the value 200
*p = 200;
// =================> TODO: Print a and b
printf("This is the value of a: %d\nThis is the value of b: %d\n", a, b);
}