KAIHATSUGIKEN GROUP

C++ PROGRAMMING LANGUAGE



*******************************************************************************
20 ポインタ
******************
void some_function(char *string);     * をポインタという
ポインタとは特定の位置を参照したり指し示すメモリアドレスのことです。
関数はメモリ位置を参照するためにポインタ変数を使います。
ポインタ変数をインクリメントするとアドレスを自動的にインクリメントします。
*******************************************************
#include <iostream.h>

void show_string(char *string)				//文字列へのポインタ
{
	while (*string != '\0')		//string ポインタによって示された文字が
	{					//NULL文字になるまで処理を続ける
		cout << *string;
		string++;			//文字は一文字ずつ増加する
	}
}

void main(void)						//ここから始まる
{
	show_string("Rescued By C++!");	
}				//関数show_stringの引き数*stringへ値を渡す
********************
NULL文字が現れるまで文字列中をループするプログラム
********************
#include <iostream.h>

int string_length(char *string)
{
	int length = 0;

	while (*string != '\0')
	{
	length++;
	string++;
	}
	
	return(length);
}

void main(void)						//ここから始まる
{
char title[] = "Rescued By C++";
			//char型変数titleで配列を準備して文字列をtitle[]に代入
cout << title << " contains " << string_length(title) <<
	" characters";		//関数string_lengthの引き数*stringへ値を渡す
}
***********************
#include <iostream.h>

int string_length(char *string)			//titleの値を *stringに代入
{
	int length = 0;			//int型変数lengthの初期値を0にする

	while (*string != '\0')			//NULL文字が現れるまで実行
	{
	length++;					//インクリメントする
	string++;	//文字列を数えながらlengthの値がインクリメント
	}
	
	return(length);				//lengthの値を持って脱出
}

void main(void)						
{
char title[] = "Rescued By C++";			

cout << title << " contains " << string_length(title) <<
	" characters";	//lengthの値をstring_length(title)に代入して表示				
}
***********************
Rescued By C++ contains 14