C Programming Lecture no 7 Pointers Strings and

  • Slides: 93
Download presentation
C Programming Lecture no. 7: Pointers, Strings and Arrays

C Programming Lecture no. 7: Pointers, Strings and Arrays

ตวชและอารเรย (Pointer and Array) Department of Computer Science 2 310322 C Programming

ตวชและอารเรย (Pointer and Array) Department of Computer Science 2 310322 C Programming

1. ตวชกบแอดเดรส (Pointers and Address) int i; i = 10; int i; i i

1. ตวชกบแอดเดรส (Pointers and Address) int i; i = 10; int i; i i = 10; 400 402 404 i 10 รปท แสดงการแทนขอมลในหนวยความจำของตวแปรประเภทพนฐาน Department of Computer Science 3 400 402 404 1. 1 310322 C Programming

x 1 400 y 2 402 ip 500 iq 502 Department of Computer Science

x 1 400 y 2 402 ip 500 iq 502 Department of Computer Science int x = 1, y = 2; int *ip, *iq; 9 310322 C Programming

x 1 400 y 2 402 ip 400 500 iq Department of Computer Science

x 1 400 y 2 402 ip 400 500 iq Department of Computer Science ip = &x; 502 10 310322 C Programming

x 1 400 y 1 402 ip 400 500 iq Department of Computer Science

x 1 400 y 1 402 ip 400 500 iq Department of Computer Science y = *ip; 502 11 310322 C Programming

x 0 400 y 1 402 ip 400 500 iq 502 Department of Computer

x 0 400 y 1 402 ip 400 500 iq 502 Department of Computer Science *ip = 0; 12 310322 C Programming

x 0 400 y 5 402 ip 400 500 iq Department of Computer Science

x 0 400 y 5 402 ip 400 500 iq Department of Computer Science y = 5; 502 13 310322 C Programming

x 0 400 y 5 402 ip 402 500 iq Department of Computer Science

x 0 400 y 5 402 ip 402 500 iq Department of Computer Science ip = &y; 502 14 310322 C Programming

x 0 400 y 3 402 ip 402 500 iq Department of Computer Science

x 0 400 y 3 402 ip 402 500 iq Department of Computer Science *ip = 3; 502 15 310322 C Programming

x 0 400 y 3 402 ip 402 500 iq 402 502 Department of

x 0 400 y 3 402 ip 402 500 iq 402 502 Department of Computer Science 16 iq = ip; 310322 C Programming

ตวอยาง 4. 1 (3) void main ( ) { int x = 5, y

ตวอยาง 4. 1 (3) void main ( ) { int x = 5, y = 10; printf(“Before swap : x = %d”, x, “, y = %dn”, y); swap ( &x, &y); printf(“After swap : x = %d”, x, “, y = %dn”, y); } Department of Computer Science 20 310322 C Programming

void swap (int *px, int *py) { int temp; temp = *px; *px =

void swap (int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; } Department of Computer Science 21 ตวอยาง 4. 1 (3) 310322 C Programming

table[0] table[1] table[2] รปท 5. 1 แสดงภาพจำลองของอารเรยขนาดสมาชก Department of Computer Science 26 table[9] 10

table[0] table[1] table[2] รปท 5. 1 แสดงภาพจำลองของอารเรยขนาดสมาชก Department of Computer Science 26 table[9] 10 ตว 310322 C Programming

�������� #define MAX 10 int x[MAX], y[MAX * 2], z[MAX * 3 + 5];

�������� #define MAX 10 int x[MAX], y[MAX * 2], z[MAX * 3 + 5]; int v[4], w[4]; char s 1[2], s 2[1]; float f[10]; Department of Computer Science 27 310322 C Programming

ตวอยางท 5. 1 sum. Third = table[0] + table[1] + table[2]; table[0] = 5;

ตวอยางท 5. 1 sum. Third = table[0] + table[1] + table[2]; table[0] = 5; if ( a[0] > a[9] ) printf (“First is greater than lastn” ); Department of Computer Science 31 310322 C Programming

ตวอยางท 5. 2 สมมตให i, j, k เปนตวแปรประเภท int for (int k = 0;

ตวอยางท 5. 2 สมมตให i, j, k เปนตวแปรประเภท int for (int k = 0; k < 9; k++) printf (“Value at %d = %dn”, k+1, table[k]); table[i + j] = 0; table[7 – table[j]] = j; Department of Computer Science 33 310322 C Programming

ใหอานคาของจำนวนเตม และแสดงผลในลำดบทกลบกน # include <stdio. h> 5 # define SIZE 5 main ( )

ใหอานคาของจำนวนเตม และแสดงผลในลำดบทกลบกน # include <stdio. h> 5 # define SIZE 5 main ( ) { int k; int table[SIZE]; for (k = 0; k < SIZE; k++) scanf (“%d”, &table[k]); for (k = SIZE-1; k >= 0; k--) printf (“%dn”, table[k]); Department of Computer Science 35 ตวอยางท 5. 3 จำนวนจากคยบอรด } 310322 C Programming

สมาชกของอารเรย สมาช กของอาร เรย อาจเป นประเภทข อม ลพ น ฐานใด ๆ ก ได หร

สมาชกของอารเรย สมาช กของอาร เรย อาจเป นประเภทข อม ลพ น ฐานใด ๆ ก ได หร ออาจเป นข อม ลประเภท E n u m e r a t i o n เ ช น #define TSIZE 10 #define NAMESIZE 20 #define ADDRSIZE 30 enum month { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } Department of Computer Science 36 310322 C Programming

สมาชกของอารเรย (2) typedef enum month Month; int age[TSIZE]; float size[TSIZE+1]; Month date[8]; char name[NAMESIZE],

สมาชกของอารเรย (2) typedef enum month Month; int age[TSIZE]; float size[TSIZE+1]; Month date[8]; char name[NAMESIZE], address[ADDRSIZE]; Department of Computer Science 37 310322 C Programming

รปท 6. 2 แสดงการอางถงตำแหนงในอารเรยผานตว pa ช pa+1 pa+2 a a[0] Department of Computer Science

รปท 6. 2 แสดงการอางถงตำแหนงในอารเรยผานตว pa ช pa+1 pa+2 a a[0] Department of Computer Science a[1] a[2] 42 a[9] 310322 C Programming

ฟงกชน strlen( ปรบปรงใหกระชบขน int strlen (char *s) { } ตวอยางท 7. 1 ) char

ฟงกชน strlen( ปรบปรงใหกระชบขน int strlen (char *s) { } ตวอยางท 7. 1 ) char *p = s; while (*p != ‘’) p++; return p-s; Department of Computer Science 53 310322 C Programming

ตวอยาง 8. 2 (2) ฟงกชน strcpy ( ) เขยนในลกษณะ พอยนเตอร void strcpy ( char

ตวอยาง 8. 2 (2) ฟงกชน strcpy ( ) เขยนในลกษณะ พอยนเตอร void strcpy ( char *s, char *t ) { while ( ( *s = *t ) != ‘’ ) { s++; t++; } } Department of Computer Science 63 310322 C Programming

ตวอยาง 8. 2 (3) ฟงกชน strcpy ( ) เขยนในลกษณะ พอยนเตอร แบบสน void strcpy (

ตวอยาง 8. 2 (3) ฟงกชน strcpy ( ) เขยนในลกษณะ พอยนเตอร แบบสน void strcpy ( char *s, char *t ) { ) ; } while ( ( *s++ = *t++ ) != ‘’ Department of Computer Science 64 310322 C Programming

แบบท 2 struct date { int day; int month; int year; } *ptrdate; Department

แบบท 2 struct date { int day; int month; int year; } *ptrdate; Department of Computer Science 73 310322 C Programming

โปรแกรมตวอยางการใชตวช #include <stdio. h> ชไปยงโครงสราง struct date {/*date template */ int day; int month;

โปรแกรมตวอยางการใชตวช #include <stdio. h> ชไปยงโครงสราง struct date {/*date template */ int day; int month; int year; }; typedef struct date Date; typedef Date *Ptr. Date; Department of Computer Science 77 ตวอยางท 9. 2. 1 (pointer) 310322 C Programming

ตวอยางท (2) 9. 2. 1 main ( ) { Date today; Ptr. Date ptrdate;

ตวอยางท (2) 9. 2. 1 main ( ) { Date today; Ptr. Date ptrdate; ptrdate = &today; ptrdate->day = 27; ptrdate->month = 9; ptrdate->year = 1985; printf ( “Today’s date is %2 d/%4 dn”, ptrdate->day, ptrdate->month, ptrdate->year ); } Department of Computer Science 78 310322 C Programming

อารเรยแบบหลายมต (2) #define NUMBER_OF_PAPERS 5 int student [ NUMBER_OF_PAPERS ]; /* int student[5]; */

อารเรยแบบหลายมต (2) #define NUMBER_OF_PAPERS 5 int student [ NUMBER_OF_PAPERS ]; /* int student[5]; */ student[0] student[1] student[2] student[3] student[4] 5. 6 8. 5 12. 6 Department of Computer Science 24. 1 16. 0 86 310322 C Programming

โปรแกรมการรบคาอารเรย #include<stdio. h> main() { float score[10][3]; int i, j; printf(“Please put scoren”); for(i=0;

โปรแกรมการรบคาอารเรย #include<stdio. h> main() { float score[10][3]; int i, j; printf(“Please put scoren”); for(i=0; i<10; i++) for(j=0; j<3; j++) scanf(“%f”, &score[i][j]); } Department of Computer Science score[0][0] score[0][1] score[0][2] score[1][0] score[1][1] score[1][2] score[9][0] score[9][1] score[9][2] 91 310322 C Programming 2

ลกษณะขอมล score[0][0] score[0][1] score[0][2] score[1][0] score[1][1] score[1][2] score[2][0] score[2][1] score[2][2] หนวยความจำ 200 202 204

ลกษณะขอมล score[0][0] score[0][1] score[0][2] score[1][0] score[1][1] score[1][2] score[2][0] score[2][1] score[2][2] หนวยความจำ 200 202 204 206 208 210 [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] 210 212 214 216 218 220 [2][0] [2][1] [2][2] [3][0] [3][1] [3][2] 220 222 224 226 228 230 [4][0] [4][1] [4][2] [5][0] [5][1] [5][2] score[9][0] score[9][1] score[9][2] Department of Computer Science 92 310322 C Programming

The end

The end