I117 3 1 School of Information Science, Japan Advanced Institute of Science and Technology
char char 1 signed char -128 127 unsigned char 0 255 ( ; single-quote 0x27) ASCII Japan Advanced Institute of Science and Technology 2008 1-2 1
ASCII man ascii SP 0 9 A Z a z Oct(8 ) 040 060 071 101 132 141 172 Hex(16 ) 20 30 39 41 5A 61 7A Dec(10 ) 20 48 57 65 90 97 122 Japan Advanced Institute of Science and Technology 2008 1-2 2
C 0 \0 ( ; double-quote 0x22) char *x="abcdef"; strcpy(dst, "foo.tar.gz"); Japan Advanced Institute of Science and Technology 2008 1-2 3
char *x="abcde"; char y[]={ A, B, C, D, E, \0 }; 4bytes A B C 0x41 0x42 0x43 D E 0x44 0x45 \0 0x0 x 65 66 67 68 69 0 6bytes y 65 66 67 68 69 0 1byte Japan Advanced Institute of Science and Technology 2008 1-2 4
#include <stdio.h> char *x="abcde"; char *y="abcde"; int main() { printf("&x %p x %p size %d\n", &x, x, sizeof(x)); printf("&y %p y %p size %d\n", &y, y, sizeof(y)); } Japan Advanced Institute of Science and Technology 2008 1-2 5
(cont.) % cc ls3.c %./a.out &x 20dc8 x 20dd0 size 4 &y 20dcc y 20dd8 size 4 0x20dc8 0x20dd0 x 65 66 67 68 69 0 0x20dcc 0x20dd8 y 65 66 67 68 69 0 Japan Advanced Institute of Science and Technology 2008 1-2 6
% gcc ls3.c %./a.out &x 20940 x 10798 size 4 &y 20944 y 10798 size 4 0x20940 0x10798 x 65 66 67 68 69 0 0x20944 y Japan Advanced Institute of Science and Technology 2008 1-2 7
(cont.) Japan Advanced Institute of Science and Technology 2008 1-2 8
malloc, strdup strcpy, strcat strchr, strstr free Japan Advanced Institute of Science and Technology 2008 1-2 9
char *a = "Japan"; char *b, *c;... b = strdup("ishikawa"); c = (char*)malloc(9); c[0] = A ; c[1] = \0 ; Japan Advanced Institute of Science and Technology 2008 1-2 10
strdup strdup I s h i k a w a \0 duplicate b I s h i k a w a x = "Ishikawa"; b = malloc(strlen(x)+1); strcpy(b, x); \0 Japan Advanced Institute of Science and Technology 2008 1-2 11
strdup (cont.) char *x = "Ishikawa"; char *b = strdup(x); b[4] = y ; b[6] = m ; printf("x %p, %s\n", x, x); printf("b %p, %s\n", b, b); b x x 20dd0, Ishikawa b 20df8, Ishiyama Japan Advanced Institute of Science and Technology 2008 1-2 12
strdup (cont.) 0x20dd0 x I s h i k a w a \0 b 0x20df8 I s h i k a w a \0 y m x Japan Advanced Institute of Science and Technology 2008 1-2 13
char *p=src; while(*p) { p++; } p *p++ =? ; *p = \0 ; before src p middle src p after src p J A I S T \0 J A I S T \0 J A I S T? \0 Japan Advanced Institute of Science and Technology 2008 1-2 14
(cont.) strcat strcat(src,"?"); strcpy strcpy(src,"aist?"); => "AIST?" => "AIST?" strncpy \0 strncpy(src,"aist?", 2); => "AIIST" Japan Advanced Institute of Science and Technology 2008 1-2 15
(cont.) J A I S T + A I A I I S T memcpy, memmove Japan Advanced Institute of Science and Technology 2008 1-2 16
(cont.) src char *src=strdup("jaist"); strdup() char buf[10]; char *src=buf; 9 Japan Advanced Institute of Science and Technology 2008 1-2 17
char *p=src; char *q=null; while(*p) { q = p; p++; } q q NULL if(q) *q = # ; before src p after src p J A I S T \0 J A I S T \0 q q Japan Advanced Institute of Science and Technology 2008 1-2 18
(cont.) JAIS# int w;... w = strlen(src); if(w>0) { src[w-1] = # ; } Japan Advanced Institute of Science and Technology 2008 1-2 19
\0 char *a=(char*)malloc(10); a[0] = \0 ;... a[4] = \0 ; Japan Advanced Institute of Science and Technology 2008 1-2 20
1 char *p=src; char *x=null; while(*p) { if(*p== S ) { x = p; break; } p++; } x NULL Japan Advanced Institute of Science and Technology 2008 1-2 21
char *p=src; char *x=null; while(*p) { if(*p== A && *(p+1)== I ) { x = p; break; } p++; } x NULL Japan Advanced Institute of Science and Technology 2008 1-2 22
(cont.) strchr char *x = strchr("jaist", S ); strstr char *x = strstr("jaist", "AI"); Japan Advanced Institute of Science and Technology 2008 1-2 23
strdup(), malloc() free() Japan Advanced Institute of Science and Technology 2008 1-2 24
1) 3 asahidai dai main() int showlast3(char*); 2 Japan Advanced Institute of Science and Technology 2008 1-2 25
2) int showlastn(char*,int); Japan Advanced Institute of Science and Technology 2008 1-2 26