lecutre 05

这一讲中讲字符串的指针讲得很好Lecture05.pdflive_session.cmemory.cmemory_errors.cverify_password.cverify_password_soln.c
Side note: use strlen to get the length of a string. Don’t use sizeof!
image.png
image.png
image.png
image.png

Arrays of string

image.png
image.png
image.png
image.png
image.png
image.png
image.png
We create strings as char[], pass them around as char *
image.png
image.png
Q: Is there a way to check in code whether a string’s characters are modifiable?
A: No. This is something you can only tell by looking at the code itself and how the string was created.
Q: So then if I am writing a string function that modifies a string, how can I tell if the string passed in is modifiable?
A: You can’t! This is something you instead state as an assumption in your function documentation. If someone calls your function with a read-only string, it will crash, but that’s not your function’s fault :-)
image.png

一些程序例子

字符数组的负索引

  1. #include <stdio.h>
  2. #include <string.h>
  3. void func(char *str) {
  4. str[0] = 'S';
  5. str++;
  6. *str = 'u';
  7. str = str + 3;
  8. str[-2] = 'm';
  9. }
  10. int main(int argc, const char *argv[]) {
  11. char buf[] = "Monday";
  12. printf("before func: %s\n", buf);
  13. func(buf);
  14. printf("after func: %s\n", buf);
  15. return 0;
  16. }

输出image.png,表明str[-2]是在当前str的位置向左查找了2个字符位置,从a到了m的位置。

char* vs char[] exercises

image.png

Bonus: Tricky addresses

  1. void tricky_addresses() {
  2. char buf[] = "Local";
  3. char *ptr1 = buf;
  4. char **double_ptr = &ptr1;
  5. printf("double_ptr value: %p\n", double_ptr);
  6. printf("buf's address: %p\n", &buf);
  7. printf("ptr1's value: %s\n", ptr1);
  8. printf("ptr1’s deref: %c\n", *ptr1);
  9. printf("ptr1’s address: %p\n", &ptr1);
  10. printf("ptr1's value address: %p\n", ptr1);
  11. char *ptr2 = &buf;
  12. printf("ptr2's value: %s\n", ptr2);
  13. printf("ptr2’s deref: %c\n", *ptr2);
  14. printf("ptr2’s address: %p\n", &ptr2);
  15. printf("ptr2's value address: %p\n", ptr2);
  16. }

image.png

lecture 06

这一讲很好Lecture06.pdflive_session.cskip_spaces.cskip_spaces_soln.c

Pointers & C Parameters

image.png
image.png
image.png
image.png
image.png
image.png
image.png

Pointers Summary

• If you are performing an operation with some input and do not care about any changes to the input, pass the data type itself.
• If you are modifying a specific instance of some value, pass the location of what you would like to modify.
• If a function takes an address (pointer) as a parameter, it can go to that address if it needs the actual value.
image.png

Double Pointers

image.png
image.png
image.png
image.png

Arrays in Memory

image.png
image.png
image.png
image.png
image.png
image.png

Arrays of Pointers

image.png
image.png

Pointer Arithmetic

image.png

const

image.png
image.png
image.png

struct

image.png
image.png
image.png
image.png
image.png
image.png
image.png

ternary

image.png

Live Session Slides

image.png
image.png
image.png
image.png
image.png