// Copyright 2000 - Yuki(hir@suki.net) // マクロ定義を使用した文字列をコピーする定義関数のサンプルプログラムです。 // コンパイルする場合はファイル名を "strcopy.cpp" に変更して下さい。 #include #define DEBUG 0 // マクロ定義 char *StrCpy2(char *,char *); // サブルーチンのプロトタイプ宣言。 void main(){ char str00[]="abcd"; // str00 は "abcd"。 char str01[256],*sp; // str01 はポインタ配列。 for(int i=0;i<256;i++){ // str01 を初期化する。 str01[i]=' '; } #if defined DEBUG // デバッグが定義されていれば、以下の文を実行。 printf("%d\n",str00); printf("%d\n",str01); #endif // ここまで。 sp=StrCpy2(str01,str00); // サブルーチンを呼ぶ。 #if defined DEBUG printf("%d\n",sp); // 定義されていれば spを数値で表示。 #endif puts(sp); // spを表示。 printf("\n"); printf("%s\n",str01); // str01 のみ表示する。 } char *StrCpy2(char *s1,char *s2){ // サブルーチン。 #if defined DEBUG puts("DEBUG"); printf("%d\n",s1); printf("%d\n",s2); int count=0; #endif #if !defined DEBUG // デバッグが定義されていなければ "NORMAL" と表示。 puts("NORMAL"); #endif char *rp=s1; do{ *s1=*s2; #if defined DEBUG putchar(*s1); putchar(*s2); printf("%d\n",count++); // s1 に s2 をコピーし、カウントを増やして表示。 #endif s1++; s2++; }while(*s2); #if defined DEBUG printf("%d\n",rp); #endif return(rp); }