C Programming Lecture no 7 Pointers Strings and
- Slides: 93
C Programming Lecture no. 7: Pointers, Strings and Arrays
ตวชและอารเรย (Pointer and Array) Department of Computer Science 2 310322 C Programming
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 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 ip = &x; 502 10 310322 C Programming
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 Science *ip = 0; 12 310322 C Programming
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 ip = &y; 502 14 310322 C Programming
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 Computer Science 16 iq = ip; 310322 C Programming
ตวอยาง 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 = *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 ตว 310322 C Programming
�������� #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; 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; 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 ( ) { 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], address[ADDRSIZE]; Department of Computer Science 37 310322 C Programming
รปท 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 *p = s; while (*p != ‘ ’) p++; return p-s; Department of Computer Science 53 310322 C Programming
ตวอยาง 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 ( 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 of Computer Science 73 310322 C Programming
โปรแกรมตวอยางการใชตวช #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; 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]; */ 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; 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 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
- Pointers and strings
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- C programming lecture
- Elastic potential energy equation further maths
- Sprint tokenizer
- & vs * in c
- & vs * in c
- Perbedaan linear programming dan integer programming
- Greedy vs dynamic programming
- System programming
- Integer programming vs linear programming
- Programing adalah
- Assembly array of strings
- Strings in java
- Flexible pattern matching in strings
- Rate of energy transfer by sinusoidal waves on strings
- Compare strings python
- Kulintang ensemble
- Mason nagy
- Mrs sprockett's strange machine
- A type of cipher that uses multiple alphabetic strings.
- Stos assembly
- Things not strings
- Three masses are connected by strings
- Sequences in discrete mathematics
- Upx decompiler
- Linguagem c strings
- Vortex strings
- Muscle strings
- What are strings in c
- What is string in c
- Jmp concatenate columns
- Uniform circular motion implies
- Ottawa suzuki strings
- What are permutations and combinations
- Ida pro strings
- Ida strings
- Declare a two dimensional array of strings named chessboard
- Caedb
- Significance of pointers in c
- C array of pointers to structs
- Xp1024xp
- Which is a good idea for using skip pointers
- Skip pointer
- Which is a good idea for using skip pointers
- Explicit pointer
- Pointers in java
- Inter function communication
- Glsl pointers
- Shuffle left algorithm
- Hazard pointers
- 12 pointers
- I-need-a-few-pointers-98hpou5
- Scientific logbook example
- Science fair logbook table of contents
- Pointers are variables that contain
- What is the fundamental of pointers?
- Pointers are variables that contain as their values
- Converging pointers algorithm
- Advanced pointers in c
- Constant pointer and pointer to constant
- Pointers are variables that contain as their values
- Pingo upb
- Pointers symbol
- Skip pointers in information retrieval
- Shuffle left algorithm
- Advanced pointers in c
- File pointers
- Pointers in assembly
- Electricity and magnetism lecture notes
- Power system dynamics and stability lecture notes
- Microbial physiology and metabolism lecture notes
- Limits fits and tolerances lecture notes
- Parallel and distributed computing lecture notes
- 50f7 tolerance
- Financial institutions and markets lecture notes ppt
- Banking and finance lecture
- Extempore and lecture
- Utilities and energy lectures
- Pride and prejudice lecture
- Mechatronics notes
- Vct monitoring foetal
- Lecture on love courtship and marriage
- Power system dynamics and stability lecture notes
- Project planning and management lecture notes ppt
- Femoral triangle
- Project procurement management lecture notes
- Lecture about sport
- Healthy lifestyle wrap up lecture
- Makeup lecture meaning
- Life lecture meaning
- Randy pausch last lecture summary
- Tensorflow lecture
- Theology proper lecture notes