Pointers Data Structure 20211025 1 Introduction If you

  • Slides: 53
Download presentation
Pointers Data Structure 補充單元 2021/10/25 1

Pointers Data Structure 補充單元 2021/10/25 1

Introduction • If you want to be proficient in the writing of code in

Introduction • If you want to be proficient in the writing of code in the C programming language, you must have a thorough working knowledge of how to use pointers – Unfortunately, C pointers appear to be rather unfriendly – Here, let try to introduce for the first time what pointer is all about … 2021/10/25 2

get a feeling on C’s variables … • A variable in a program is

get a feeling on C’s variables … • A variable in a program is something with a name, the value of which can vary. • When we declare a variable we inform the compiler of two things, – the name of the variable – the type of the variable. 2021/10/25 3

get a feeling on C’s variables … • The way the compiler and linker

get a feeling on C’s variables … • The way the compiler and linker handles this is that it assigns a specific block of memory within the computer to hold the value of that variable – The variable size of that block depends on the range over which the is allowed to vary 2021/10/25 4

Assigning specific blocks of memory to variables j i int i; short j; i=100;

Assigning specific blocks of memory to variables j i int i; short j; i=100; 2021/10/25 5

Size of Integer 2021/10/25 6

Size of Integer 2021/10/25 6

get a feeling on C’s variables … • For example, we declare a variable

get a feeling on C’s variables … • For example, we declare a variable of type integer with the name k by writing: – int k • On seeing the "int" part of this statement the compiler – sets aside 4 bytes of memory (on a PC) to hold the value of the integer. – It also sets up a symbol table. • In that table it adds the symbol k and the relative address in memory where those 4 bytes were set aside. 2021/10/25 7

get a feeling on C’s variables … • Int k, m; k = 2;

get a feeling on C’s variables … • Int k, m; k = 2; • m = k; • There are two "values" associated with the object k. – One is the value of the integer stored there (2 in the above example) and – the other the "value" of the memory location, i. e. , the address of k. 2021/10/25 8

get a feeling on C’s variables … • int j, k; k = 2;

get a feeling on C’s variables … • int j, k; k = 2; j = 7; <-- line 1 k = j; <-- line 2 • In line 1, j is interpreted as the “address” of j • In line 2, j is interpreted as the “value” stored in address of j 2021/10/25 9

Example • 我如何將 variable (變數) 的 address (位址) 列印出來? – Int i; printf(“%p”, &i)

Example • 我如何將 variable (變數) 的 address (位址) 列印出來? – Int i; printf(“%p”, &i) 2021/10/25 10

2021/10/25 11

2021/10/25 11

32 -bit vs 64 -bit #include <stdio. h> int main() { int i; long

32 -bit vs 64 -bit #include <stdio. h> int main() { int i; long j; printf("size of int = %dn", sizeof(i)); printf("size of long = %dn", sizeof(j)); printf(“Address of variable I = %pn", &i); printf(“Address of variable J = %pn", &j); } 2021/10/25 12

32 -bit vs 64 -bit (Free. BSD) 2021/10/25 13

32 -bit vs 64 -bit (Free. BSD) 2021/10/25 13

32 -bit vs 64 -bit %gcc -m 32 -o out 32 c. c %gcc

32 -bit vs 64 -bit %gcc -m 32 -o out 32 c. c %gcc -m 64 -o out 64 c. c 2021/10/25 14

32 -bit vs 64 -bit Free. BSD AMD 64 supports up to 1 TB

32 -bit vs 64 -bit Free. BSD AMD 64 supports up to 1 TB MM %. /out 32 size of int = 4 size of long = 4 address of variable I = 0 xffffdc 10 address of variable J = 0 xffffdc 0 c %. /out 64 size of int = 4 size of long = 8 address of variable I = 0 x 7 fffffffeaec address of variable J = 0 x 7 fffffffeae 0 2021/10/25 15

儲存 “位址” 的變數 • Yes! Such a variable is called a pointer variable –

儲存 “位址” 的變數 • Yes! Such a variable is called a pointer variable – In C when we define a pointer variable we do so by preceding its name with an asterisk (*). – In C we also give our pointer a type which, in this case, refers to the type of data stored at the address we will be storing in our pointer. • For example, consider the variable declaration: – int *ptr; • ptr : A pointer variable that point to address of an integer • *ptr : value stores in the memory pointed to by ptr 2021/10/25 17

Example • Int *ptr; int k; ptr = &k; ptr stores address of k

Example • Int *ptr; int k; ptr = &k; ptr stores address of k *ptr = 7; *ptr store its “value” • 可以這樣做類比 – ptr &k – *ptr k • 注意 : ptr 如果沒有給初值, 預設值是 null (or 0) – if (ptr == NULL) … 2021/10/25 18

Assigning specific blocks of memory to variables 1004 7 ptr k int *ptr; int

Assigning specific blocks of memory to variables 1004 7 ptr k int *ptr; int k; ptr = &k; *ptr=7; 2021/10/25 19

Example 2021/10/25 20

Example 2021/10/25 20

Void pointer? • void *ptr; Void : 空的 – 空的 pointer? – 較好的解釋是 :

Void pointer? • void *ptr; Void : 空的 – 空的 pointer? – 較好的解釋是 : generic pointer (不是某種特定 型態, 例如 integer or character, 的 pointer) • You cannot compare an integer pointer to a character pointer – But you can compare them to a void pointer 2021/10/25 22

Dynamic Allocation of Memory • There are times when it is convenient to allocate

Dynamic Allocation of Memory • There are times when it is convenient to allocate memory at run time using malloc(), calloc() • Using this approach permits – postponing the decision on the size of the memory block need to store an array, for example, until run time. – Or it permits using a section of memory for the storage of an array of integers at one point in time, and then when that memory is no longer needed it can be freed up for other uses, such as the storage of an array of structures. 2021/10/25 23

Pointers and Dynamic Allocation ( 配置) of Memory • When memory is allocated, the

Pointers and Dynamic Allocation ( 配置) of Memory • When memory is allocated, the allocating function (such as malloc(), calloc(), etc. ) returns a pointer. 2021/10/25 24

Example • int *ptr; ptr = (int *)malloc(10 * sizeof(int)); if (ptr == NULL)

Example • int *ptr; ptr = (int *)malloc(10 * sizeof(int)); if (ptr == NULL) { ………. } – 動態地 allocates 10 個 integers 的空間 – <重要>動態建立的 memory 不用時, 記得要 set it “free” • free (ptr); 2021/10/25 25

Dynamic Array 動態陣列 #include <stdio. h> #include <stdlib. h> int main() { int *ptr,

Dynamic Array 動態陣列 #include <stdio. h> #include <stdlib. h> int main() { int *ptr, size, i; printf("Please input the array size: "); scanf("%d", &size); ptr = (int *)malloc(sizeof(int)*size); for (i=0; i<size; i++) ptr[i]=i; // *(ptr+i) = i; for (i=0; i<size; i++) printf("ptr[%d]=%dn", i, ptr[i]); } 2021/10/25 26

Int maximum(); main() { int max, x[10000], y[10000]; …. max = maximum(x, y); }

Int maximum(); main() { int max, x[10000], y[10000]; …. max = maximum(x, y); } Int maximum(int x, int y) { int max; if (x>= y) max = x; else max=y; 2021/10/25 return (max); 28

But, what is a structure? • Let’s see array first … To represent somebody’s

But, what is a structure? • Let’s see array first … To represent somebody’s name no more than 20 characters … – char name[20]; • How about representing 50 students’ name in a class? – char name[50][20]; 2021/10/25 29

Now, what about representing more of 50 students’ …? • Include name, ID, gender,

Now, what about representing more of 50 students’ …? • Include name, ID, gender, address, etc – char name[50][20]; – char ID[50][10]; – int gender[50]; – char address[50][100]; • A bit awkward, right? 2021/10/25 30

How about this … • Define a new data types, like “student_type”, which aggregate

How about this … • Define a new data types, like “student_type”, which aggregate name, ID, gender & address together? This is what structure does • For example: struct student_type { char name[20]; char ID[10]; int gender char address[100]; } 2021/10/25 31

Then … • In 宣告: – struct student_type student[50]; • To access: – student[0]->name

Then … • In 宣告: – struct student_type student[50]; • To access: – student[0]->name – student[10]->ID – student[49]->gender – etc 2021/10/25 32

Structure Data Type 2021/10/25 33

Structure Data Type 2021/10/25 33

#include <stdlib. h> #include <stdio. h> typedef struct node { int data; struct node

#include <stdlib. h> #include <stdio. h> typedef struct node { int data; struct node * next; } NODE; NODE *next; void main() { NODE * curr, * head; int i; head = NULL; for(i=1; i<=10; i++) { curr = (NODE *)malloc(sizeof(NODE)); curr->data = i; curr->next = head; head = curr; } curr = head; data while(curr) { printf("%dn", curr->data); curr = curr->next ; } NODE *next } 2021/10/25 34

2021/10/25 35

2021/10/25 35

Sample Code & Explanation 定義一個新的 data type, 名稱為 NODE, 包含一整數及一指標 2021/10/25 37

Sample Code & Explanation 定義一個新的 data type, 名稱為 NODE, 包含一整數及一指標 2021/10/25 37

程式執行前 2021/10/25 38

程式執行前 2021/10/25 38

NODE 宣告完後 NO Change 2021/10/25 40

NODE 宣告完後 NO Change 2021/10/25 40

NODE *curr, *head 宣告完後 curr head 佔用空間多大? Why? 2021/10/25 42

NODE *curr, *head 宣告完後 curr head 佔用空間多大? Why? 2021/10/25 42

如果是這樣宣告 … NODE curr, head; curr head 佔用空間多大? Why? 2021/10/25 43

如果是這樣宣告 … NODE curr, head; curr head 佔用空間多大? Why? 2021/10/25 43

malloc 宣告完後 NODE 的大小(佔的記憶 空間) 1016 = 4 bytes (int) + 4 bytes (address)

malloc 宣告完後 NODE 的大小(佔的記憶 空間) 1016 = 4 bytes (int) + 4 bytes (address) 2021/10/25 45

Sample Code & Explanation For loop 之 i=0 iteration 執行完後 … 2021/10/25 46

Sample Code & Explanation For loop 之 i=0 iteration 執行完後 … 2021/10/25 46

For loop 之 i=0 執行完後 … curr 1016 head 0 2021/10/25 48

For loop 之 i=0 執行完後 … curr 1016 head 0 2021/10/25 48

For loop 之 i=0 執行完後 … Address curr 1016 head 0 2021/10/25 49

For loop 之 i=0 執行完後 … Address curr 1016 head 0 2021/10/25 49

Sample Code & Explanation For loop 之 i=1 iteration 執行過程 … head = curr

Sample Code & Explanation For loop 之 i=1 iteration 執行過程 … head = curr 執行前 2021/10/25 50

For loop 之 i=1 執行過程 … 2004 1016 curr head 0 1 1016 2021/10/25

For loop 之 i=1 執行過程 … 2004 1016 curr head 0 1 1016 2021/10/25 51

For loop 之 i=1 執行完後 … 2004 curr head 0 1 1016 2021/10/25 52

For loop 之 i=1 執行完後 … 2004 curr head 0 1 1016 2021/10/25 52

用熟悉的表示方式 … (i=1) curr 0 head 1 2021/10/25 53

用熟悉的表示方式 … (i=1) curr 0 head 1 2021/10/25 53