1 of 26

CSE 333

Section 1

Gitlab, Pointers, and C Strings

1

If you haven’t yet, please clone your exercise git repo to your local machine!

2 of 26

Logistics

  • Pre-quarter survey due yesterday (6/24) @ 11:59 PM
  • Exercise 1 due today (6/25) @ 11:59pm (solutions on Saturday)
    • Create ex1-submit tag in your exercise Gitlab repo before deadline
  • Exercise 2 released tomorrow (6/26), due Tuesday (6/30) @ 11:59 PM
    • Create ex2-submit tag in your exercise Gitlab repo before deadline
  • Homework 1 released on Tuesday (6/30), due ~2 weeks (7/9) ish
    • Will be in a separate homework Gitlab repo – lots of starter code to deal with

2

3 of 26

Introductions

3

4 of 26

TA Intro: FILL IN HOWEVER YOU WOULD LIKE!

4

5 of 26

Icebreaker!

Please turn to the people next to you and share:

  • Name, pronouns, year
  • What are you excited to learn in CSE 333?
  • Favorite animal or pet that you wish you had?

5

6 of 26

Computing Setup for 333: DEMO

6

7 of 26

CSE Linux Environment

  • The CSE Linux environment (i.e., CSE lab machines, attu cluster) is where all of your work will be graded, so you should do all of your development and testing here!
    • e.g., uses gcc14 (not the latest)
  • You will need to be on the Husky OnNet VPN in order to access instructional machines (i.e., attu cluster) from off campus
    • Windows/MacOS: BIG-IP Edge Client, Linux: f5vpn, iOS/Android: F5 Access
    • New this quarter!

7

8 of 26

Text Editor of Choice

  • Generally, whatever you used in CSE 351 (likely vim or emacs) will work
  • For CSE 333, we also encourage VS Code
    • ✅ Remote SSH extension allows you to directly edit files on attu
    • ✅ C/C++ extension will enable syntax highlighting
    • ⛔ Use of Copilot to generate code violates 333’s generative AI policy! 🙅

8

9 of 26

Gitlab Repositories

  • Sign-in using your CSE NetID @ https://gitlab.cs.washington.edu/
    • Non-majors should have received a temporary CSE NetID for the quarter

  • There should be a repo created for you titled: cse333-26su-ex-<netid>
    • This is your exercise repo; your homework repo will be created Friday (6/26)
    • Please let us know ASAP if you don’t have one!
  • If you haven’t already, please follow the setup instructions in our Gitlab Guide

9

10 of 26

git Tagging Demo/Follow-Along

  1. Clone your repo locally
    1. git clone git@gitlab.cs.washington.edu:cse333-26su-students/cse333-26su-ex-<netid>.git
  2. Go to the ex1 directory in the repo
    • cd cse333-26su-ex-<netid>
    • cd ex1
  3. Edit your ex1 solution file into the ex1 directory
    • Something like: vim ex1.c
  4. Add, commit, and push
    • git add ex1/ex1.c
    • git commit -m "add my ex1 solution"
    • git push
  5. Create and push tag
    • git tag ex1-submit
    • git push --tags

10

11 of 26

Git Repo Usage

11

  • Try to use the command line interface (not Gitlab’s web interface)
  • Only push files used to build your code to the repo
    • No executables, object files, backup files, etc.
    • Don’t always use git add . to add all your local files
  • Commit and push when an individual chunk of work is tested and done
    • Don’t push after every edit
    • Don’t only push once when everything is done
    • Gives you stable checkpoint backups in case something goes wrong with your working copy

12 of 26

Pointers in C

12

13 of 26

Pointers

  • Data type that stores the address of (the lowest byte of) a datum
    • Can draw an arrow in memory diagrams from pointer to pointed to data, particularly if actual value (stored address) is unknown
  • Common uses:
    • Reference to data allocated elsewhere (e.g., malloc, literals, files)
    • Iterators (e.g., data structure traversal)
    • Data abstraction (e.g., head of linked list, function pointers)

13

14 of 26

Pointer Syntax and Semantics

  • Declared as type* name; or type *name;
    • Doesn’t matter, just be consistent
  • Address-of operator & gets a variable’s address
  • Dereference operator * refers to the pointed-to datum
    • “Follows” the pointer/arrow in a memory diagram
  • Example code:

  • Example diagram:

14

int* ar = (int*) malloc(3*sizeof(int)); // reference

int* p = &ar[1]; // iterator

*p = 3;

0x1b126b0

0x1b126b4

?

3

?

ar

p

Stack

Heap

15 of 26

Output Parameters

  • Recall: The return statement in a function passes a single value back through the %rax register

15

  • An output parameter is a C idiom that emulates “returning values” through parameters:
    • An output parameter is a pointer (i.e., the address of a location in memory)
    • The function with this parameter must dereference it to change the value stored at that location
    • The new value is “returned” by persisting after the function returns
  • Output parameters are the only way in C to achieve returning multiple values

16 of 26

Output Parameter Example

  • Write a function to compute the sum of values and product of all values in an array. The function is given a pointer to the first element in an array, the length of the array, and two output parameters to return the product and sum.
  • void ProductAndSum(int* ar, int len, int* product, int* sum) {

int temp_sum = 0, temp_product = 1; // initializations

}

16

for (int i = 0; i < len; i++) { // compute product & sum

temp_sum += ar[i];

temp_product *= ar[i];

}

*sum = temp_sum; // set output params

*product = temp_product;

17 of 26

Exercise 1

  1. Which parameters are output �parameters?

  1. Draw a memory diagram of the beginning of this call to Division.

  1. Fill in the blank spaces in our calls to Division and printf.

17

void Division(int numerator,

int denominator,

int* quotient,

int* remainder) {

*quotient = numerator / denominator;

*remainder = numerator % denominator;

}

int main(int argc, char* argv[]) {

int quot, rem;

Division(22, 5, _____, _____);

printf("%d rem %d\n", _____, _____);

return EXIT_SUCCESS;

}

18 of 26

Exercise 2

  • A prefix sum over an array is the running total of all numbers in the array up to and including the current number. For example, given the array �{1, 2, 3, 4}, the prefix sum would be {1, 3, 6, 10}.
  • Write a function PrefixSum to compute the prefix sum of an array given (1) a pointer to its first element, (2) the pointer to the first element of the output array, and (3) the length both arrays (assumed to be the same).
    • void PrefixSum(int* input, int* output, int length);
    • Are there any edge cases to look out for?

18

19 of 26

C-Strings

19

20 of 26

C-Strings

  • A string in C is declared as an array of characters that is terminated by a null character '\0'
  • When allocating space for a string, remember to add an extra element for the null character

20

char str_name[size];

21 of 26

Initialization Examples

  • Code:

  • Memory:

  • Notes:
    • The size 6 is optional, as it can be inferred from the initialization.
    • Both initialize the array in the declaration scope (e.g., on the stack if a local var), though the latter can be thought of as copying the contents from the string literal into the array.

21

// list initialization

char str1[6] = {'H','e','l','l','o','\0'};

// string literal initialization

char str2[6] = "Hello";

index

0

1

2

3

4

5

value

'H'

'e'

'l'

'l'

'o'

'\0'

22 of 26

Common String Literal Error (1/2)

  • Code:

  • Memory:

  • Notes:
    • By default, using a string literal will allocate and initialize the character array in read-only memory (Literals)

22

index

0

1

2

3

4

5

value

'H'

'e'

'l'

'l'

'o'

'\0'

// pointer instead of an array

char* str3 = "Hello";

0x402037

str3

23 of 26

Common String Literal Error (2/2)

  • Code:

  • Memory:

  • Notes:
    • By default, using a string literal will allocate and initialize the character array in read-only memory (Literals)
    • What would happen if we executed str3[0] = 'J';?

23

index

0

1

2

3

4

5

value

'H'

'e'

'l'

'l'

'o'

'\0'

// pointer instead of an array

char* str3 = "Hello";

0x402037

str3

Segfault!

24 of 26

Extra: Exercise 3

  • The following code has a bug. What’s the problem, and how would you fix it?

void Bar(char ch) {

ch = '3';

}

int main(int argc, char* argv[]) {

char fav_class[] = "CSE331";

Bar(fav_class[5]);

printf("%s\n", fav_class); // should print "CSE333"

return EXIT_SUCCESS;

}

24

25 of 26

Extra: Exercise 4

  • Write strcpy, a standard library function that copies a string src into an output parameter called dest and returns a pointer to the destination string. You may assume that dest has sufficient space to store src.

char* strcpy(char* dest, char* src) {

}

25

26 of 26

Function Pointers

  • Pointers can store addresses of functions
    • Functions are just instructions in read-only memory, their names are pointers to this memory.
  • Used when performing operations for a function to use
    • Like a comparator for a sorter to use in Java
    • Reduces redundancy

26

int one() { return 1; }

int two() { return 2; }

int three() { return 3; }

int get(int (*func_name)()) {

return func_name();

}

int main(int argc, char* argv[]) {

int res1 = get(one);

int res2 = get(two);

int res3 = get(three);

printf("%d, %d, %d\n", res1, res2, res3);

return EXIT_SUCCESS;

}