新・明解C言語 ポインタ完全攻略

Similar documents
新版明解C言語 実践編

新・明解C言語 実践編

ex14.dvi

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

BW BW

18 C ( ) hello world.c 1 #include <stdio.h> 2 3 main() 4 { 5 printf("hello World\n"); 6 } [ ] [ ] #include <stdio.h> % cc hello_world.c %./a.o

PowerPoint Presentation

Original : Hello World! (0x0xbfab85e0) Copy : Hello World! (0x0x804a050) fgets mstrcpy malloc mstrcpy (main ) mstrcpy malloc free fgets stream 1 ( \n

Microsoft Word - Cプログラミング演習(12)

: CR (0x0d) LF (0x0a) line separator CR Mac LF UNIX CR+LF MS-DOS WINDOWS Japan Advanced Institute of Science and Technology

ex12.dvi

II 3 yacc (2) 2005 : Yacc 0 ~nakai/ipp2 1 C main main 1 NULL NULL for 2 (a) Yacc 2 (b) 2 3 y

untitled

ohp03.dvi

(K&R 2.9) ~, &,, >>, << 2. (K&R 5.7) 3. (K&R 5.9) 4. (K&R 5.10) (argc argv atoi(), atof() ) 5. (K&R 7.5) (K&R 7.6) - FILE, stdin, stdout, std

新版明解C言語入門編

:30 12:00 I. I VI II. III. IV. a d V. VI


C言語によるアルゴリズムとデータ構造

r03.dvi

A/B (2018/10/19) Ver kurino/2018/soft/soft.html A/B

tuat1.dvi

プログラミング方法論 II 第 14,15 回 ( 担当 : 鈴木伸夫 ) 問題 17. x 座標と y 座標をメンバに持つ構造体 Point を作成せよ 但し座標 は double 型とする typedef struct{ (a) x; (b) y; } Point; 問題 18. 問題 17 の

卒 業 研 究 報 告.PDF

mstrcpy char *mstrcpy(const char *src); mstrcpy malloc (main free ) stdio.h fgets char *fgets(char *s, int size, FILE *stream); s size ( )

P05.ppt

P02.ppt

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裵²ó ¨¡ À©¸æ¹½Â¤¡§¾ò·ïʬ´ô ¨¡

I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裶²ó ¨¡ À©¸æ¹½Â¤¡§·«¤êÊÖ¤· ¨¡

P03.ppt

file"a" file"b" fp = fopen("a", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose(fp); fp = fopen("b", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose

main

r07.dvi

ohp07.dvi

I. Backus-Naur BNF : N N 0 N N N N N N 0, 1 BNF N N 0 11 (parse tree) 11 (1) (2) (3) (4) II. 0(0 101)* (

‚æ4›ñ

memo

3.1 stdio.h iostream List.2 using namespace std C printf ( ) %d %f %s %d C++ cout cout List.2 Hello World! cout << float a = 1.2f; int b = 3; cout <<

1 C STL(1) C C C libc C C C++ STL(Standard Template Library ) libc libc C++ C STL libc STL iostream Algorithm libc STL string vector l

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() 2 double *a[ ]; double 1 malloc() dou

1 4 2 EP) (EP) (EP)

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() malloc 2 #include <stdio.h> #include

untitled

program.dvi

Microsoft Word - C.....u.K...doc

kiso2-09.key

橡Pro PDF

/* do-while */ #include <stdio.h> #include <math.h> int main(void) double val1, val2, arith_mean, geo_mean; printf( \n ); do printf( ); scanf( %lf, &v

[ 1] 1 Hello World!! 1 #include <s t d i o. h> 2 3 int main ( ) { 4 5 p r i n t f ( H e l l o World!! \ n ) ; 6 7 return 0 ; 8 } 1:

O(N) ( ) log 2 N

double float

Minimum C Minimum C Minimum C BNF T okenseq W hite Any D

Microsoft Word - Cプログラミング演習(7)

超初心者用

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裱£²²ó ¡Ý½ÉÂꣲ¤Î²òÀ⡤±é½¬£²¡Ý

I J

ohp08.dvi

:30 12:00 I. I VI II. III. IV. a d V. VI

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

untitled

1.ppt

‚æ2›ñ C„¾„ê‡Ìš|

P06.ppt

Microsoft Word - Cプログラミング演習(10)

Prog1_6th

[1] #include<stdio.h> main() { printf("hello, world."); return 0; } (G1) int long int float ± ±

memo

Microsoft Word - no14.docx

r08.dvi

& & a a * * ptr p int a ; int *a ; int a ; int a int *a

C 2 / 21 1 y = x 1.1 lagrange.c 1 / Laglange / 2 #include <stdio.h> 3 #include <math.h> 4 int main() 5 { 6 float x[10], y[10]; 7 float xx, pn, p; 8 in

計算機プログラミング

lexex.dvi

joho07-1.ppt

PowerPoint プレゼンテーション

£Ã¥×¥í¥°¥é¥ß¥ó¥°(2018) - Âè11²ó – ½ÉÂꣲ¤Î²òÀ⡤±é½¬£² –

untitled

tuat2.dvi

スライド タイトルなし

gengo1-11


kiso2-06.key

j x j j j + 1 l j l j = x j+1 x j, n x n x 1 = n 1 l j j=1 H j j + 1 l j l j E

C言語入門

Prog1_15th

Microsoft PowerPoint - H22プログラミング第一(E)#12

C¥×¥í¥°¥é¥ß¥ó¥° ÆþÌç

Microsoft PowerPoint - C言語の復習(配布用).ppt [互換モード]

Microsoft Word - Cプログラミング演習(8)

I ASCII ( ) NUL 16 DLE SP P p 1 SOH 17 DC1! 1 A Q a q STX 2 18 DC2 " 2 B R b

Taro-ファイル処理(公開版).jtd

A/B (2010/10/08) Ver kurino/2010/soft/soft.html A/B

+ +

( ) 1 1: 1 #include <s t d i o. h> 2 #include <GL/ g l u t. h> 3 #include <math. h> 4 #include <s t d l i b. h> 5 #include <time. h>

C B

問 2 ( 型変換 ) 次のプログラムを実行しても正しい結果が得られない 何が間違いかを指摘し 正しく修正せよ ただし int サイズが 2 バイト long サイズが 4 バイトの処理系での演算を仮定する #include <stdio.h> int main( void ) { int a =

Microsoft Word - no15.docx

July prog11-02.c /*, */ /* $Id: prog11-02.c,v :48:07+09 naito Exp $ */ #include <stdio.h> #define N 10 int main(int argc, cha

pptx

Transcription:

2 1-1 1-1 /* 1-1 */ 1 int n = 100; int *p = &n; printf(" n %d\n", n); /* n int */ printf("*&n %d\n", *&n); /* *&n int */ printf(" p %p\n", p); /* p int * */ printf("&*p %p\n", &*p); /* &*p int * */ printf("sizeof(n) %u\n", (unsigned)sizeof(n)); printf("sizeof(*&n) %u\n", (unsigned)sizeof(*&n)); printf("sizeof(p) %u\n", (unsigned)sizeof(p)); printf("sizeof(&*p) %u\n", (unsigned)sizeof(&*p)); chap01/ex0101.c n 100 *&n 100 p 312 &*p 312 sizeof(n) 2 sizeof(*&n) 2 sizeof(p) 4 sizeof(&*p) 4 *&n &n n &*p p p 1-2 1-2 chap01/ex0102.c /* 1-2 */ int n; int *p; printf("sizeof*p = %u\n", (unsigned)sizeof*p); printf("sizeof&n = %u\n", (unsigned)sizeof&n); printf("sizeof-1 = %u\n", (unsigned)sizeof-1); printf("sizeof(unsigned)-1 = %u\n", (unsigned)sizeof(unsigned)-1); printf("sizeof(double)-1 = %u\n", (unsigned)sizeof(double)-1); printf("sizeof((double)-1) = %u\n", (unsigned)sizeof((double)-1)); printf("sizeof n+2 = %u\n", (unsigned)sizeof n+2); printf("sizeof(n+2) = %u\n", (unsigned)sizeof(n+2)); printf("sizeof(n+2.0) = %u\n", (unsigned)sizeof(n+2.0)); sizeof n+2 sizeof n+2 sizeofn

3 1-3 1-3 /* 1-3 */ int x = 123, y = 456; int *p1 = &x; /* p1 x */ int *p2 = &y; /* p2 y */ int *temp; temp = p1; p1 = p2; p2 = temp; printf("*p1 %d\n", *p1); /* p1 y */ printf("*p2 %d\n", *p2); /* p2 x */ chap01/ex0103.c *p1 456 *p2 123 sizeof*p sizeof&n sizeof-1 sizeof(unsigned)-1 sizeof(double)-1 sizeof((double)-1) sizeof n+2 sizeof(n+2) sizeof(n+2.0) *p sizeof(int) &n sizeof(int *) -1 int sizeof(int) unsigned 1 double 1-1 double sizeof(double) sizeof(int) 2 int + int int sizeof(int) int + double double sizeof(double) 1-4 ** 5 *p 5 * 55 275 1-4 /* 1-4 */ int x = 55; int *p = &x; printf("%d\n", 5**p); chap01/ex0104.c 275

4 summ_diff wa sa 1-5 main &sum &diff wa sum sa diff *wa *sa main sum diff 1-5 chap01/ex0105.c /* 1-5 */ /*--- x y *wa *sa ---*/ void sum_diff(int x, int y, int *wa, int *sa) n1 54 Ÿ n2 87 Ÿ *wa = x + y; /* */ n1 n2 141 *sa = (x > y)? x - y : y - x; /* */ n1 n2 33 int n1, n2; int sum, diff; /* */ printf(" n1 "); scanf("%d", &n1); printf(" n2 "); scanf("%d", &n2); sum_diff(n1, n2, &sum, &diff); printf("n1 n2 %d\n", sum); /* sum */ printf("n1 n2 %d\n", diff); /* diff */ 1-6 sort3d *x1 *x2 *x1 *x2 *x2 *x2 *x3 *x2 *x3 *x3 *x3 *x3 *x1 *x2 *x2 if *x1 *x2 *x2 *x3 *x2 *x1 swapd List 1-14 swap int * double *

5 /* 1-6 */ /*--- *x *y ---*/ d1 3.1416 Ÿ void swapd(double *x, double *y) d2 0.0 Ÿ d2 2.5 Ÿ double temp = *x; d1 d2 d3 d1 0.000 *x = *y; d2 2.500 *y = temp; d3 3.142 /*--- *x1 *x2 *x3 ---*/ void sort3d(double *x1, double *x2, double *x3) if (*x1 > *x2) swapd(x1, x2); if (*x2 > *x3) swapd(x2, x3); if (*x1 > *x2) swapd(x1, x2); double d1, d2, d3; 1-6 printf(" d1 "); scanf("%lf", &d1); printf(" d2 "); scanf("%lf", &d2); printf(" d3 "); scanf("%lf", &d3); sort3d(&d1, &d2, &d3); printf("d1 d2 d3 \n"); printf("d1 %.3f\n", d1); /* d1 */ printf("d2 %.3f\n", d2); /* d2 */ printf("d3 %.3f\n", d3); /* d3 */ chap01/ex0106.c

6 2-1 /* 2-1 */ int i; int a[5]; /* int */ int *p = &a[2]; /* p a[2] */ for (i = 0; i < 5; i++) printf("&a[%d] = %p p + (%2d) = %p\n", i, &a[i], i - 2, p + i - 2); 2-2 chap02/ex0201.c &a[0] = 100 p + (-2) = 100 &a[1] = 102 p + (-1) = 102 &a[2] = 104 p + ( 0) = 104 &a[3] = 106 p + ( 1) = 106 &a[4] = 108 p + ( 2) = 108 2 2-1 p a[2] a a[0] a[4] p - 2, p - 1, p, p + 1, p + 2 /* 2-2 */ ary_cpy a b while no 2-2 chap02/ex0202.c /*--- n b a ---*/ void ary_cpy(int a[], const int b[], int no) while (no-- > 0) *a++ = *b++; int i, no; int x[5], y[5]; int x_size = sizeof(x) / sizeof(x[0]); x[0] 54 Ÿ x[1] 28 Ÿ x[2] 89 Ÿ x[3] 18 Ÿ x[4] 77 Ÿ x y y[0] 54 y[1] 28 y[2] 89 y[3] 18 y[4] 77 for (i = 0; i < x_size; i++) printf("x[%d] ", i); scanf("%d", &x[i]); ary_cpy(y, x, x_size); printf(" x y \n"); for (i = 0; i < x_size; i++) printf("y[%d] %d\n", i, y[i]);

7 /* 2-3 */ 2-3 #define swap(type, x, y) do type temp = x; x = y; y = temp; while (0) /*--- *x[0] *x[1] *x[2] ---*/ void sort_ptr3ary(int *x[]) if (*x[0] > *x[1]) swap(int *, x[0], x[1]); if (*x[1] > *x[2]) swap(int *, x[1], x[2]); if (*x[0] > *x[1]) swap(int *, x[0], x[1]); int n1, n2, n3; int *p[3] = &n1, &n2, &n3; chap02/ex0203.c n1 5 Ÿ n2 8 Ÿ n3 6 Ÿ *p[0] 5 *p[1] 6 *p[2] 8 List 4-1 p.116 2-3 1-6 printf(" n1 "); printf(" n2 "); printf(" n3 "); scanf("%d", &n1); scanf("%d", &n2); scanf("%d", &n3); sort_ptr3ary(p); printf(" \n"); printf("*p[0] %d\n", *p[0]); /* *p[0] */ printf("*p[1] %d\n", *p[1]); /* *p[1] */ printf("*p[2] %d\n", *p[2]); /* *p[2] */ swap

8 /* 3-1 */ int i, j, k; int b[3][2][4]; 3-1 for (i = 0; i < 3; i++) for (j = 0; j < 2; j++) for (k = 0; k < 4; k++) printf("&b[%d][%d][%d] = %p\n", i, j, k, &b[i][j][k]); chap03/ex0301.c &b[0][0][0] = 1000 &b[0][0][1] = 1002 &b[0][0][2] = 1004 &b[0][0][3] = 1006 &b[0][1][0] = 1008 &b[0][1][1] = 1010 3 3-1 3-2 3-2 /* 3-2 */ int x[3][2][4]; printf(" x %d %d %d \n", (int)(sizeof(x) / sizeof(x[0])), (int)(sizeof(x[0]) / sizeof(x[0][0])), (int)(sizeof(x[0][0]) / sizeof(x[0][0][0]))); chap03/ex0302.c x 3 2 4 printf(" x %d %d %d \n", (int)(sizeof(x) / sizeof(int[2][4])), (int)(sizeof(x[0]) / sizeof(int[4])), (int)(sizeof(x[0][0]) / sizeof(int))); sizeof( ) / sizeof( )

9 /* 3-4 */ 3-3 /* 3-3 */ /*--- n 2 4 v ---*/ void fill_avalue(int a[][2][4], int n, int v) int i, j, k; for (i = 0; i < n; i++) for (j = 0; j < 2; j++) for (k = 0; k < 4; k++) a[i][j][k] = v; int i, j, k, no; int mx[3][2][4]; printf(" "); scanf("%d", &no); fill_avalue(mx, 3, no); /* mx no */ for (i = 0; i < 3; i++) for (j = 0; j < 2; j++) for (k = 0; k < 4; k++) printf("mx[%d][%d][%d] = %3d\n", i, j, k, mx[i][j][k]); chap03/ex0303.c 15 Ÿ mx[0][0][0] = 15 mx[0][0][1] = 15 mx[0][0][2] = 15 mx[0][0][3] = 15 mx[2][1][3] = 15 3-3 fill_avalue 2 4 3-4 a &a[0] *a a[0] 3-4 chap03/ex0304.c int a[3][2][4]; printf("sizeof(*a) = %u\n", (unsigned)sizeof(*a)); printf("sizeof(a[0]) = %u\n", (unsigned)sizeof(a[0])); printf("sizeof(a[0][0]) = %u\n", (unsigned)sizeof(a[0][0])); printf("sizeof(a[0][0][0]) = %u\n", (unsigned)sizeof(a[0][0][0])); sizeof(*a) = 16 sizeof(a[0]) = 16 sizeof(a[0][0]) = 8 sizeof(a[0][0][0]) = 2

10 4-1 chap04/ex0401.c /* 4-1 */ str "" char str[4]; str[0] = '\0'; str[1] = 'A'; str[2] = 'B'; str[3] = 'C'; printf(" str \"%s\" \n", str); 4-2 List 4-7 str scanf("%s", &str); &str char 15 &str[0] 4 4 4 4-1 4-3 q p q p p q "ABCD" 'A' 4-4 ptr &ptr scanf("%s", &ptr); 4 4 4 4 4 ptr str scanf ptr ptr 100 101 100 109

11 /*--- s c ---*/ 4-5 int str_chnum(const char *s, int c) int count = 0; while (*s) if (*s++ == c) count++; return count; 4-6 chap04/ex0405.c 4-6 chap04/ex0406.c /*--- s ---*/ int str_dignum(const char *s) int count = 0; while (*s) if (*s >= '0' && *s <= '9') count++; if (isdigit(*s)) *s++; <ctype.h> return count; 4-5 4-7 4-7 /*--- s1 s2 ---*/ void swap_str(char s1[], char s2[]) char *temp; while (*s1 && *s2) /* */ char t = *s1; *s1++ = *s2; *s2++ = t; if (*s1) /* s1 */ temp = s1; while (*s1) *s2++ = *s1++; /* s1 s2 */ *temp = *s2 = '\0'; else if (*s2) /* s2 */ temp = s2; while (*s2) *s1++ = *s2++; /* s2 s1 */ *temp = *s1 = '\0'; else *s1 = *s2 = '\0'; chap04/ex0407.c

12 /* 5-1 */ #include <time.h> #include <stdlib.h> #define QNO 12 /* */ #define CNO 4 /* */ #define swap(type, x, y) do type t = x; x = y; y = t; while (0) /*--- ---*/ char *jptr[] = " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", ; /*--- ---*/ char *eptr[] = "animal", "car", "flower", "house", "desk", "book", "chair", "father", "mother", "love", "peace", "magazine", ; /*--- ---*/ void print_cand(const int c[], int sw) int i; for (i = 0; i < CNO; i++) printf("(%d) %s ", i, sw? jptr[c[i]] : eptr[c[i]]); printf(" "); /*--- ---*/ int make_cand(int c[], int n) int i, j, x; c[0] = n; /* */ for (i = 1; i < CNO; i++) do x = rand() % QNO; for (j = 0; j < i; j++) if (c[j] == x) break; while (i!= j); c[i] = x; 5-1 j = rand() % CNO; if (j!= 0) swap(int, c[0], c[j]); /* */ return j; chap05/ex0501.c book (0) (1) (2) (3) 0 Ÿ 0-1- 1 Ÿ (0) love (1) house (2) car (3) desk 1 Ÿ 0-1- 0 Ÿ 5 5-1

13 int nq, pq; /* */ int na; /* */ int sw; /* 0 1 */ int retry; /* */ int cand[cno]; /* */ srand(time(null)); /* */ pq = QNO; /* */ do int no; do /* */ nq = rand() % QNO; while (nq == pq); na = make_cand(cand, nq); /* */ sw = rand() % 2; printf("%s \n", sw? eptr[nq] : jptr[nq]); do print_cand(cand, sw); /* */ scanf("%d", &no); if (no!= na) puts(" "); while (no!= na); puts(" "); pq = nq; printf(" 0-1- "); scanf("%d", &retry); while (retry == 1); 5-2 List 5-9 argc i argv 5-2 /* 5-2 */ int main(int argc, char **argv) int i = 0; while (--argc > 0) printf("argv[%d] = \"%s\"\n", ++i, *++argv); chap05/ex0502.c >ex0502 Sort BinTree Ÿ argv[1] = "Sort" argv[2] = "BinTree" 5-3 argv strtod

14 /* 5-4 */ #include <stdlib.h> /*--- src dst ---*/ void detab(file *src, FILE *dst, int width) int ch, pos = 1; 5-4 5-4 while ((ch = fgetc(src))!= EOF) int num; switch (ch) case '\t' : num = width - (pos - 1) % width; for ( ; num > 0; num--, pos++) fputc(' ', dst); break; case '\n' : fputc(ch, dst); pos=1; break; default : fputc(ch, dst); pos++; break; int main(int argc, char *argv[]) int width = 8; FILE *fp; if (argc < 2) detab(stdin, stdout, width); /* */ else while (--argc > 0) if (**(++argv) == '-') if (*++(*argv) == 't') width = atoi(++*argv); else fputs(" \n", stderr); return 1; else if ((fp = fopen(*argv, "r")) == NULL) fprintf(stderr, "\"%s\" \n", *argv); return 1; else detab(fp, stdout, width); /* fp */ fclose(fp); chap05/detab.c 5-3 /* 5-3 */ #include <errno.h> #include <stdlib.h> int main(int argc, char **argv) char str[100]; char *ptr = str; double sum = 0.0; while (--argc > 0) double x = strtod(*++argv, &ptr); if (errno!= ERANGE && ptr!= str) sum += x; printf("%f\n", sum); chap05/sum.c >sum 15 3.14 1.35E1 Ÿ 31.640000

15 /* 5-5 */ #include <stdlib.h> /*--- src dst ---*/ void entab(file *src, FILE *dst, int width) int ch, count = 0, ntab = 0, pos = 1; for ( ; (ch = fgetc(src))!= EOF; pos++) if (ch == ' ') if (pos % width!= 0) count++; else count = 0; ntab++; else for ( ; ntab > 0; ntab--) fputc('\t', dst); if (ch == '\t') count = 0; else for ( ; count > 0; count--) fputc(' ', dst); fputc(ch, dst); if (ch == '\n') pos = 0; else if (ch == '\t') pos += width - (pos - 1) % width - 1; int main(int argc, char *argv[]) int width = 8; FILE *fp; 5-5 if (argc < 2) entab(stdin, stdout, width); /* */ else while (--argc > 0) if (**(++argv) == '-') if (*++(*argv) == 't') width = atoi(++*argv); else fputs(" \n", stderr); return 1; else if ((fp = fopen(*argv, "r")) == NULL) fprintf(stderr, "\"%s\" \n", *argv); return 1; else entab(fp, stdout, width); /* fp */ fclose(fp); chap05/entab.c 5-5

16 6-1 6 6-2 /*--- x a,b ---*/ void sortxyz(struct xyz *a, struct xyz *b) if (a->x > b->x) struct xyz temp = *a; *a = *b; *b = temp; 6-3 name & chap06/ex0602.c &z.a struct xy * &z.a.x int * &z.a.y double * &z.b int * 6-2 a x b x *a *b 6-3 chap06/ex0603.c /*--- p Member ---*/ void scanmember(member *p) printf(" "); scanf("%d", &p->no); /* & */ printf(" "); scanf("%s", p->name); /* & */

17 7 7-1 List 7-1 *p = 15; *p = 0 calloc 0 7-2 0 7-2 /*--- ptr old_size size 0 --- */ void *recalloc(void *ptr, size_t size, size_t old_size) void *tmp; if (size == 0) return NULL; tmp = realloc(ptr, size); if (tmp!= NULL && size > old_size) memset((char *)tmp + old_size, 0, size - old_size); return tmp; chap07/ex0702.c 7-3 /* 7-3 */ #include <stdlib.h> #include <string.h> /*--- s ---*/ char *str_dup(const char *s) char *p = malloc(strlen(s) + 1); return (p == NULL)? NULL : strcpy(p, s); char s[128]; char *p; printf(" s "); scanf("%s", s); if ((p = str_dup(s))!= NULL) /* */ printf(" p \n"); printf("s = \"%s\"\n", s); printf("p = \"%s\"\n", p); free(p); /* */ chap07/ex0703.c

18 7-3 sprintf 7-4 /* 7-4 */ #include <stdlib.h> #define LENGTH 10 /* */ int num; /* */ char (*p)[length]; /* 10 */ printf(" "); scanf("%d", &num); p = malloc(num * LENGTH); if (p == NULL) puts(" "); else int i; char temp[100]; for (i = 0; i < num; i++) /* */ printf("p[%d] : ", i); scanf("%s", temp); sprintf(p[i], "%.9s", temp); chap07/ex0704.c 5 Ÿ p[0] : 123456789012 Ÿ p[1] : 12345678901 Ÿ p[2] : 1234567890 Ÿ p[3] : 123456789 Ÿ p[4] : 12345678 Ÿ p[0] = 123456789 p[1] = 123456789 p[2] = 123456789 p[3] = 123456789 p[4] = 12345678 for (i = 0; i < num; i++) /* */ printf("p[%d] = %s\n", i, p[i]); free(p); /* */ 7-4 argv main pt argv char ** dup_argv main char pt & char pt ptr dup_argv argv char argc argc + 1 argv[argc] NULL NULL

19 /* 7-5 */ #include <stdlib.h> #include <string.h> /*--- argv ---*/ int dup_argv(char ***ptr, int argc, char **argv) int i; 7-5 if ((*ptr = calloc(argc + 1, sizeof(char *))) == NULL) for (i = 0; i < argc + 1; i++) (*ptr)[i] = NULL; for (i = 0; i < argc; i++) if (((*ptr)[i] = malloc(strlen(argv[i]) + 1)) == NULL) strcpy((*ptr)[i], argv[i]); return 1; /*--- argv ---*/ void print_argv(int argc, char **argv) int i = 0; chap07/ex0705.c >ex0705 Sort BinTree Ÿ argv[0] = "ex0705" argv[1] = "Sort" argv[2] = "BinTree" while (argc-- > 0) printf("argv[%d] = \"%s\"\n", i++, *argv++); int main(int argc, char **argv) int i; char **pt; if (!dup_argv(&pt, argc, argv)) puts(" "); else print_argv(argc, pt); if (pt!= NULL) for (i = 0; i < argc + 1; i++) free(pt[i]); /* */ free(pt); argv[argc] NULL argv[0] argv[argc - 1] print_argv argv pt argv

20 /*--- g(x) ---*/ double g(double x) return (x * x * x) + (x * x); /*--- ---*/ double trapezoid(double w1, double w2, double h) return (w1 + w2) * h / 2.0; 8 8-1 daikei 8-1 /* 8-1 */ /*--- f(x) ---*/ double f(double x) return x * x; chap08/ex0801.c 1.0 Ÿ 5.0 Ÿ 100 Ÿ f 41.3344 g 197.3440 /*--- fp x1 x2 n ---*/ double daikei(double x1, double x2, int n, double fp(double)) int i; double s = 0.0; double step = (x2 - x1) / n; for (i = 0; i < n; i++) s += trapezoid(fp(x1 + step * i), fp(x1 + step * (i + 1)), step); return s; int n; double x1, x2; printf(" "); printf(" "); printf(" "); scanf("%lf", &x1); scanf("%lf", &x2); scanf("%d", &n); printf(" f %.4f\n", daikei(x1, x2, n, f)); printf(" g %.4f\n", daikei(x1, x2, n, g)); f g main trapezoid

21 8-2 sort_2dstr sort_ pvstr 8-2 /* 8-2 */ #include <stdlib.h> #include <string.h> /*--- n1 n2 ---*/ void sort_2dstr(char *p, int n1, int n2) qsort(p, n1, n2, (int(*)(const void *, const void *))strcmp); /*--- x y ---*/ static int pstrcmp(const void *x, const void *y) return strcmp(*(const char **)x, *(const char **)y); /*--- p ---*/ void sort_pvstr(char *p[], int n) qsort(p, n, sizeof(char *), pstrcmp); int i; char s[][7] = "LISP", "C", "Ada", "Pascal"; char *p[] = "LISP", "C", "Ada", "Pascal"; sort_2dstr(&s[0][0], 4, 7); sort_pvstr(p, 4); chap08/ex0802.c s[0] = Ada s[1] = C s[2] = LISP s[3] = Pascal p[0] = Ada p[1] = C p[2] = LISP p[3] = Pascal puts(" "); for (i = 0; i < 4; i++) printf("s[%d] = %s\n", i, s[i]); for (i = 0; i < 4; i++) printf("p[%d] = %s\n", i, p[i]); sort_2dstr List 3-6 sort_pvstr pstrcmp