⚠️ This is not the current iteration of the course! Head here for the current offering.

## How section works

We release section material via the CS 300 website (where you’re reading this now) and the cs300-sections repository (obtain a local copy with a command like `git clone git@github.com:csci0300/cs300-sections`). Section instructors will work through the section material at their own pace, stopping at exercises, questions, and discussions so students can work through the material together. It’s best if you bring your laptop.

Section works well if students ask questions, and sometimes instructors will take the section in an unexpected direction based on student requests. Please speak up! If you have questions from lecture, now’s the time to ask :-)

# Section 1: Memory Organization and Pointers

Curious as to how you can pass variables by reference in C? Here's some pointers!

## Warmup: Values and Addresses

Consider the following declaration and initialization in C:

``````int y = 420;
``````

WARMUP A. What is the value of the variable `y`? How do we access this value using C code?

WARMUP B. Where does the variable `y` live in memory? What is this called, and how might we access this location using C code?

## Question 1: Pointers and Memory Diagrams

For the following questions, we'll be using the following memory diagram, WeensyMem:

Note that each box in WeensyMem represents 4 bytes of memory!

As an example, the declaration and initializion code in the Warmup could result in this memory diagram:

Although the computer could have put `y` at many different addresses, we chose to put it at address `0x14`. It has value `420`.

QUESTION 1A. Consider the following function:

``````void problemOne() {
int x;
int* ptr = &x;
*ptr = 5;
}
``````

Starting with a blank memory diagram, how does our memory diagram change after executing the above function?

QUESTION 1B. Consider the following function:

``````void problemTwo() {
int x;
int* int_ptr;
int** int_ptr_ptr;
int_ptr_ptr = &int_ptr;
*int_ptr_ptr = &x;
*(*int_ptr_ptr) = 120;
}
``````

Starting with a blank memory diagram, how does our memory diagram change after executing the above function?

After executing this function, what would our memory diagram look like? You may assume that `x` will have address `0x14`, `int_ptr` will have address `0x20`, and `int_ptr_ptr` will have address `0x30`. You should start with a blank memory diagram.

QUESTION 1C. Consider the following function:

``````#include <stdio.h>

void problemThree() {
int x = 120;
int* int_ptr = &x;
char* char_ptr = int_ptr;
printf("%d\n", *int_ptr);
printf("%c\n", *char_ptr);
}
``````

Starting with a blank memory diagram, how does our memory diagram change after executing the above function?

What output will this function print? (Hint: a character-to-integer table (which converts characters to their corresponding ASCII code) can be found here!)

QUESTION 1D. Consider the following function:

``````#include <stdio.h>

void problemFour() {
long x;
long y = 121;
x = &y;
long* ptr = &x;
printf("%d\n", *((long*)*ptr));
}
``````

Will this code compile and run? If so, starting with a blank memory diagram, how does our memory diagram change after executing the above function?

What output will this function print? Would trying to print this value result in a runtime error?

## Question 2: Pointers and Strings in Code

Now, we're going to probe properties of pointers in real code. Consider the following C program:

``````#include <stdio.h>

int addFromArray(char* arr) {
int num1 = arr[0];
int num2 = arr[1];
return num1 + num2;
}

void mysteryFunc(int* num) {
int value = *num;
*num = value * value;
}

int main() {
char str_arr[] = "I <3 CS0300!";
printf("%s\n", str_arr); // A

char* str_ptr = str_arr;
printf("%s\n", str_ptr); // B

str_arr[9] = '3';
printf("%s\n", str_arr); // C
printf("%s\n", str_ptr); // D

printf("%d\n", added); // E

// Start of buggy code block
printf("%d\n", added); // G
// End of buggy code block
}
``````

QUESTION 2A. Will the line labeled `A` and the line labeled `B` print the same text? Why or why not?

QUESTION 2B. What will the line labeled `C` and the line labeled `D` print? And will they be the same text? Why or why not?

QUESTION 2C. What will the value printed in line `E` be? (Hint: a character-to-integer table, which converts characters to their corresponding ASCII code, can be found here!)

QUESTION 2D. Oh no, some of our code is not working as intended! In the labeled buggy code block, something has gone wrong. What is the function called `mysteryFunc` meant to do?

QUESTION 2E. How do we fix this code block such that the print on the line labeled `G` works as intended (i.e. the value of `added` is actually changed by `mysteryFunc`)?