advertisement

C Programming Language #9

63 %
37 %
advertisement
Information about C Programming Language #9
Education

Published on March 6, 2014

Author: aragornyeh

Source: slideshare.net

advertisement

PROGRAMMING LANGUAGE #9 aragornyeh .blogspot .com

This Semester 1. C 2. Programming Group Contest 3. Objective-C

Textbook

Grading 20% Assignment 30% Group Contest 50% Final Exam

Team Project is all about Collaboration 3 Students Build 1 C Application

Chapter 9 指 標 9.1 指標的觀念 9.2 指標變數 9.3 指標的用途 9.4 陣列與指標 9.5 於函式間傳遞陣列 9.6 應用範例:排序法

US News 2014 最佳工作 1. 2. 3. 4. 5. 軟體開發者 電腦系統分析師 牙醫 執業護士 藥師 6. 註冊護士 7. 物理治療師 8. 醫師 9. 網站開發者 10. 牙科衛生士

9.1 INTRODUCTION

Pointer Variable that stores a memory address.

In computing, memory refers to the physical devices used to store programs or data. Memory

Memory A 0 1 2 3 4 5 B C D

Memory A B C 0 1 2 3 4 5 5 2 D

Memory A B C 0 1 2 5 3 4 5 p Pointer to a Location in Memory 2 D

Memory A B C 0 1 2 5 3 4 5 p Pointer to a Location in Memory (C:3) 2 D

Address in Memory

Pointer #000XD Pointer is memory address 1234

Pointer declaration: int *ptr; • the address stored in a pointer, ptr • the value of the variable that pointers point to *ptr

Symbols Asterisk (*) • Sets a variable as a pointer *pointer Ampersand (&) • Returns the memory address of a given variable &variable

Example (1): Pointer int num = 3; Int *numptr; printf("content of num is %dn", num); printf("address of num is %pn", &num); numptr = # printf("content of numptr is %pn", numptr);

Example (2): Pointer int num = 3; int *numptr = # printf("content of num is %dn", num); printf("content pointed to by numptr is %dn", *numptr); *numptr = 99; printf("content of num is now %dn", num);

Example (3): Pointer int a = 3, b = 3; int *bptr = &b; a += 4; *bptr += 4; printf("a is %d, b is %dn", a, b); a++; (*bptr)++; printf("a is %d, b is %dn", a, b);

Example (4): Pointer int vector[] = {28, 41, 7}; int *pi = vector; // pi: 100 printf("%dn",*pi); // Displays 28 pi += 1; // pi: 104 printf("%dn",*pi); // Displays 41 pi += 1; // pi: 108 printf("%dn",*pi); // Displays 7

/* ch9 pointer.c */ #include <stdio.h> #include <stdlib.h> void func_call(void); int main() { int num = 10; char ch1 = 'A'; char ch2 = 'B'; double value1 = 3.14; double value2 = 0.782;

printf("In main() ...n"); printf(" num = %d &num = %pn", num, &num); printf(" ch1 = %c &ch1 = %pn", ch1, &ch1); printf(" ch2 = %c &ch2 = %pn", ch2, &ch2); printf(" value1 = %.2f &value1 = %pn", value1, &value1); printf(" value2 = %.2f &value2 = %pn", value2, &value2); func_call(); system("PAUSE"); return 0; } void func_call(void) { int num = 10; char ch1 = 'A'; char ch2 = 'B'; double value1 = 3.14; printf("nIn func_call() ...n"); printf(" num = %d &num = %pn", num, &num); printf(" ch1 = %c &ch1 = %pn", ch1, &ch1); printf(" ch2 = %c &ch2 = %pn", ch2, &ch2); printf(" value1 = %.2f &value1 = %pn", value1, &value1); }

Console Output In main() ... num = 10 &num = 0012FF7C ch1 = A &ch1 = 0012FF78 ch2 = B &ch2 = 0012FF74 value1 = 3.14 &value1 = 0012FF6C value2 = 0.78 &value2 = 0012FF64 In func_call() ... num = 10 &num = 0012FF0C ch1 = A &ch1 = 0012FF08 ch2 = B &ch2 = 0012FF04 value1 = 3.14 &value1 = 0012FEFC

Review Memory * &

9.2 POINTER VARIABLE

Pointer Declaration type *ptr int *ptr1 char *ptr2 float *ptr3

EXAMPLE

Pointer #include <stdio.h> int main ( ) { · int* n , *n = 20, printf ("%in" , *n ) ; return o, · }

Pointers Pointer Declaration Pointer Assignment Pointer Access

Pointers Pointer Declaration

Pointers Pointer Declaration

Pointers Pointer Declaration

Pointers Pointer Assignment

Pointers Pointer Assignment

Pointers Pointer Assignment

Pointers Pointer Access

Pointers Pointer Access

Pointers

EXCERSIE

/* ch9 ptr_var.c */ #include <stdio.h> #include <stdlib.h> int main() { int num = 10; int *ptr_num; printf(" num = %d &num = %pn", num, &num); ptr_num = &num; printf(" *ptr_num = %d ptr_num = %p &ptr_num = %pn", *ptr_num,ptr_num, &ptr_num); system("PAUSE"); return 0; }

設定記憶體位址給指標 ptr_num = &num; 0012FF7C 0012FF78 10 0012FF7C num ptr_num

type *ptr "a pointer of type named ptr" *ptr "the value of whatever ptr is pointed at“ &thing "the address of thing" type *ptr = &thing "a pointer of type named ptr set to the address of thing" ptr++ "increment where ptr points"

Printing a Pointer #include <stdio.h> int main() 1111111111111011001000000000000 { Address [7FFD9000] = [100] int* p; Address [0023FF70] = [200] int n; *p = 100; 000000001000111111111101110000 n = 200; printf("Address [%p] = [%d]n", p, *p); printf("Address [%p] = [%d]n", &n, n); return 0; }

EXAMPLE

Address Name Value

Address Name Value 0 value 1234

Address Name Value 0 value 1234 1 n *

Address Name Value 0 value 1234 1 n * x0

Address Name Value 0 value 1234 1 n * x0

Address Name Value 0 value 1234 1 n * x0

Address Name Value 0 value 1234 1 n * x0 value = 1234

Address Name Value 0 value 1234 1 n * x0 value = 1234

Address Name Value 0 value 1234 1 n * x0 value = 1234

Address Name Value 0 value 1234 1 n * x0 value = 1234 Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net Programming in C

Address Name Value 0 value 1234 1 n * x0 value = 1234

Address Name Value 0 value 1234 1 n * x0 value = 1234 n = 1234

Address Name Value 0 value 1234 1 n * x0 Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net Programming in C

Address Name Value 0 value 1234 1 n * x0

Address Name Value 0 value 1235 1 n * x0

Address Name Value 0 value 1235 1 n * x0

Address Name Value 0 value 1235 1 n * x0 value = 1235 n = 1235

Address Name Value 0 value 1235 1 n * x0 value = 1235 n = 1235

9.3 USAGE OF POINTER

to exchange, barter, or trade, as one thing for another swap

swap function swap(a, b); void swap(int x, int y) { int temp; temp = x; x = y; y = temp; }

EXCERSIE

/* ch9 swap1.c */ #include <stdio.h> #include <stdlib.h> void swap(int, int); int main() { int x,y; x = 14; y = 62; printf("Initial ...n"); printf(" x = %d y = %dn", x, y); swap(x, y); printf("nAfter swapping ...n"); printf(" x = %d y = %dn",x,y); system("PAUSE"); return 0; } void swap(int a, int b) { int temp; temp = a; a = b; b = temp; }

(1)temp=a; (2)a=b; (3)b=temp; 14 a 14 a 62 a 62 a 62 b 62 b 62 b 14 b temp 14 temp 14 temp 14 temp

Console Output Initial ... x = 14 y = 62 After swapping ... x = 14 y = 62

Exchange of pointers a: b: px: py:

swap(&a, &b); void swap(int *px, int *py) { int temp; temp = *px *px = *py *py = temp; }

CALL BY VALUE

void swap(int, int); Int main(){ int a = 3; int b = 10; swap(a, b); printf(“a:%d b:%d”, a, b); return 0; } Void swap(int x, int y){ int temp; temp = x; x = y; y = temp; }

CALL BY ADDRESS

void swap(int, int); Int main(){ int a = 3; int b = 10; swap(&a, &b); printf(“a:%d b:%d”, a, b); return 0; } Void swap(int *x, int *y){ int temp; temp = *x; *x = *y; *y = temp; }

EXCERSIE

/* ch9 swap3.c */ #include <stdio.h> #include <stdlib.h> void swap(int *,int *); int main() { int x,y; x = 14; y = 62; } printf("Initial ...n"); printf(" x = %d y = %dn", x, y); swap(&x, &y); printf("nAfter swapping ...n"); printf(" x = %d y = %dn", x, y); system("PAUSE"); return 0; void swap(int *a,int *b) { int temp; printf("nIn swap() ...n"); printf(" *a = %d*b = %dn",*a,*b); temp = *a; *a = *b; *b = temp; printf("End swap() ...n"); printf(" *a = %d*b = %dn",*a,*b); }

Console Ouput Initial ... x = 14 y = 62 In swap() ... *a = 14 *b = 62 End swap() ... *a = 62 *b = 14 After swapping ... x = 62 y = 14

9.4 ARRAY & POINTER

to exchange, barter, or trade, as one thing for another array

Array

Array

Array

Array a[0] a[1] a[9]

Array & Pointer a[i] *(a+i)

Pointers to an Array int a[4] = int i; int *ptr = for(i = 0; { printf("%d ptr++; } {8, 14, 6, 9}; a; i < 4; i++) ", *ptr);

Array of Pointers int *data[3]; int i, x = 5, y = 89, z = 34; data[0] = &x; data[1] = &y; data[2] = &z; for(i = 0; i < 3; i++) printf("%d ", *data[i]); data

EXCERSIE

/* ch9 ary_ptr2.c */ #include <stdio.h> #include <stdlib.h> int main() { int i; int a[] = {1, 2, 3, 4, 5}; int *ptr=a; for (i=0; i<sizeof(a)/sizeof(int); i++) { printf(" a[%d] = %dn", i, a[i]); printf(" *(ptr+%d)=%dn", i, *(ptr+i)); } system("PAUSE"); return 0; }

Console Output a[0] = 1 *(ptr+0)=1 a[1] = 2 *(ptr+1)=2 a[2] = 3 *(ptr+2)=3 a[3] = 4 *(ptr+3)=4 a[4] = 5 *(ptr+4)=5

/* ch9 ary_ptr4.c */ #include <stdio.h> #include <stdlib.h> int main() { int ary[3][4] = {{1,2,3,4}, {5,6,7,8},{9,10,11,12}}; int *ptr[3] = {ary[0], ary[1], ary[2]}; int i; printf("n"); for (i=0; i<3; i++) printf("*ptr[%d]=%d, **(ptr+%d)=%dn", i, *ptr[i], i, **(ptr+i)); system("PAUSE"); return 0; }

int *ptr[3] ptr ptr[0] 1 ptr+1 ptr[1] 2 3 4 5 6 7 ptr+2 ptr[2] 8 9 10 11 12

Console Ouput *ptr[0]=1, **(ptr+0)=1 *ptr[1]=5, **(ptr+1)=5 *ptr[2]=9, **(ptr+2)=9

9.6 SELECTION SORT

/* ch9 sel_sort.c */ #include <stdio.h> #include <stdlib.h> #define N 20 void list(int *array); void swap(int *,int *); void selection_sort(int *array); int main() { int i,array[N]; for (i=0; i<N; i++) array[i] = rand() % 100 + 1; printf("nSource array ...n"); list(array); selection_sort(array); printf("nSorting ...n"); list(array); system("PAUSE"); return 0;

} void list(int *array) { int i; } for (i=0; i<N; i++) { printf("%8d",array[i]); if (((i+1) % 5) == 0) printf("n"); } printf("n"); void swap(int *i,int *j) { int temp; } temp = *i; *i = *j; *j = temp; void selection_sort(int *array) { int i,cmp,min; } for (i=0; i<N; i++) { for (cmp=min=i,cmp++; cmp<N; cmp++) if (array[cmp] < array[min]) min = cmp; swap(&array[min],&array[i]); }

Console Output Source array ... 42 68 25 79 6 46 92 96 35 59 82 43 1 63 28 28 70 65 62 37 Sorting ... 1 6 35 37 59 62 70 79 25 42 63 82 28 43 65 92 28 46 68 96

void selection_sort(int *array) { int i,cmp,min; for (i=0; i<N; i++) { for (cmp=min=i,cmp++; cmp<N; cmp++) if (array[cmp] < array[min]) min = cmp; swap(&array[min],&array[i]); } }

for loop 外層迴圈表示目前該填入適當數值的陣列位置,這個 位置將從0增加到(N-1)為止; 內層for迴圈則負責找出剩餘陣列中的最小元素,然後 拿它與目前位置的元素交換。

Homework #1 9.9 No.2 (P.34) Write or Print in A4 Paper Name & I.D.

C Programming is Easy! aragornyeh@gmail.com

Add a comment

Related presentations

Related pages

C (programming language) - Wikipedia, the free encyclopedia

C (/ ˈ s iː /, as in the letter c) is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable ...
Read more

C programming.com - Learn C and C++ Programming ...

A website designed to help you learn C or C++. Understandable C and C++ programming tutorials, compiler reviews, source code, tips and tricks.
Read more

C (Programmiersprache) – Wikipedia

1978 veröffentlichten Brian W. Kernighan und Dennis Ritchie die erste Auflage von The C Programming Language (deutsch: Programmieren in C).
Read more

The C Programming Language, Second Edition - 9p.io

The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. ISBN 0-13-110362-8 (paperback), 0-13 ...
Read more

C Programming Tutorial - 9 - I Need Arrays - YouTube

Question: What is the best programming language to learn first? - Duration: 5:36. Eli the Computer Guy Live 186,575 views
Read more

C++ - Wikipedia, the free encyclopedia

In 1985, the first edition of The C++ Programming Language was released, ... As in C, C++ supports four types of memory management: ...
Read more

C Programming Language eBook: Brian W ... - amazon.de

C Programming Language eBook: Brian W. Kernighan, Dennis Ritchie: Amazon.de: Kindle-Shop Amazon.de Prime testen Kindle-Shop. Los. Alle Kategorien. DE Hallo ...
Read more

C Programming in Plan 9 from Bell Labs - cat-v.org

C Programming in Plan 9 from Bell Labs. Pietro Gagliardi. ABSTRACT. This paper is an introduction to programming with Plan 9 from Bell Labs with the C ...
Read more

C Programming Language - Lesson 9 - YouTube

Language: Chinese, Mandarin Author: Yonggang Liu This video serves only for the purpose of study reference. Please leave your feedback in case ...
Read more

C Programming - Wikibooks, open books for an open world

C Programming is a featured book on Wikibooks because it contains substantial content, it is well-formatted, and the Wikibooks community has decided to ...
Read more