測驗https://sandbox.cs50.io/ https://cs50.harvard.edu/x/2020/weeks/2/ https://www.youtube.com/watch?v=cF6YkH-8vFk 20mins 十六進制 Hex Decimal RGB FFFFFF 255,255,255
1 2 3 4 5 6 7 8 #include <stdio.h> int main(void) { int n = 50; printf("%i\n", n); // 50 }
“&”號表示地址是什麼, “*”表示前往某個地址。
1 2 3 4 5 6 7 8 9 10 // Prints an integer's address #include <stdio.h> int main(void) { int n = 50; printf("%p\n", &n); } // 0x7ffe00b3adbc
1 2 3 4 5 6 7 8 9 10 // Prints an integer via its address #include <stdio.h> int main(void) { int n = 50; printf("%i\n", *&n); // 50 }
1 2 3 4 5 6 7 8 9 10 11 // Stores and prints an integer's address #include <stdio.h> int main(void) { int n = 50; int *p = &n; printf("%p\n", p); // // 0x7ffe00b3adbc }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // Stores and prints an integer's address #include <stdio.h> int main(void) { int n = 50; int *p = &n; printf("%i\n", p); // <!-- :您將n的地址存儲在p和p中 //18:41 //是一個指針,特別是指向整數的指針。 //18:45 //換句話說,p是整數的地址。 --> }
06 / 30 Char start means s is storing the address of a character ? What must getString returing The index of the first letter. string s = EMMA; s pointer = > EMMA
E 0x123 M 0x124 M 0x125 A 0x126 \0 0x127
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #include <stdio.h> int main(void) { char *s = "EMMA"; printf("%p\n", s); printf("%p\n", &s[0]); printf("%p\n", &s[1]); printf("%p\n", &s[2]); printf("%p\n", &s[3]); // --------------------- printf("%c\n", *s); printf("%c\n", *(s+1)); printf("%c\n", *(s+2)); printf("%c\n", *(s+3)); printf("%c\n", *(s+4)); } 0x42ab52 0x42ab52 0x42ab53 0x42ab54 0x42ab55 E M M A { int n = 50; int *p = &n; printf("%p\n", &n); printf("%i\n", *&n); printf("---------------\n"); int t = 50; int *a = &t; printf("%i\n", t); printf("%p\n", &t); printf("---------------\n"); string s = "EMMA"; printf("%s\n", s); printf("%p\n", s); } $ ./address 0x7ffcb3111d9c 50 --------------- 50 0x7ffcb3111d8c --------------- EMMA 0x42aac7
compare
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <cs50.h> #include <stdio.h> int main(void) { int i = get_int("i: "); int j = get_int("j: "); if(i == j) { printf("Same\n"); } else { printf("Defferent\n"); } } $ ./compare i: 1 j: 2 Different
COMPARE 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 #include <cs50.h> #include <stdio.h> int main(void) { // int i = get_int("i: "); // int j = get_int("j: "); // string s = get_string("s: "); // string t = get_string("t: "); char *s = get_string("s: "); char *t = get_string("t: "); // Char start means s is storing the address of a character printf("%p\n", s); printf("%p\n", t); if(s == t) { printf("Same\n"); } else { printf("Defferent\n"); } }
$ ./compare s: EM t: EM 0x1ffe6a0 0x1ffe6e0 Defferent 我輸入 都是EM 為何會是DIFFERENT 原因在記憶體位置
COPY 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 // Capitalizes a string #include <cs50.h> #include <ctype.h> #include <stdio.h> #include <string.h> int main(void) { // Get a string string s = get_string("s: "); // Copy string's address string t = s; // Capitalize first letter in string if (strlen(t) > 0) { t[0] = toupper(t[0]); } // Print string twice printf("s: %s\n", s); printf("t: %s\n", t); } $ ./copy s: EMMA s: EMMA t: EMMA 我只要 將s 轉大寫 並不想要連t 一起
下面一個例子 malloc 50:00 for memory alloca
// Capitalizes a copy of a string
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 #include <cs50.h> #include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { // Get a string char *s = get_string("s: "); // Allocate memory for another string char *t = malloc(strlen(s) + 1); // Copy string into memory for (int i = 0, n = strlen(s); i <= n; i++) { t[i] = s[i]; } // Capitalize copy t[0] = toupper(t[0]); // Print strings printf("s: %s\n", s); printf("t: %s\n", t); }
strcpy , strlen
when you pass inputs to a function , you are effectively passing copies of your own values to that function
the heap is a big chunk of meomory where you can alloate memory form.
Clang
machine code ↓
1
globals ↓
2
heap ↓
3
.
4
.
5
stack ↑
6
swap 1:22:00
.
6
.
5
.
4
.
3
1a 2b swap
2
main ↑ 1x 2y
1
scanf
1 2 3 4 5 6 7 8 9 10 11 12 // Dangerously gets a string from user using scanf #include <stdio.h> int main(void) { char s[5]; printf("s: "); scanf("%s", s); printf("s: %s\n", s); }