※過去講義資料編集中 編集終わったら[[有機酸]]内からリンクします
/******************************

   PG班講義  ポインタ応用

******************************/ [#s89f5403]

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

int *make_array(int x){ /* 配列を作る関数です */
	int i,*test;
	test=(int*)malloc(sizeof(int)*x); /* int型のx個分のメモリを確保 */
	for(i=0;i<x;i++){
		test[i]=0;
	}
	return test; /* 配列の"ポインタ"を返します */
}


/* リスト構造という構造です 配列に似たデータ構造の一種です */

typedef char elementtype; /* typedefの意味を再確認しておいて */

struct node{
	elementtype element;
	struct node* next;
};

struct node* init_clist(const char *s)
{
	struct node *n, *m;
	char c;
	m = n = (struct node*)malloc(sizeof(struct node));
	while (c=*s++) { /* sのポインタの最後はNULL(=0)だからwhileが止まるわけ */
		if (isalnum(c)) {/* isalnumはcが数字か文字なら1を返す */
			m = m->next = (struct node*)malloc(sizeof(struct node));
			m->element = c;
		}
	}
	m->next = 0;
	return n; /* 構造体の"ポインタ"が返ります */
}

void insert(struct node* p,elementtype x){ /* リスト挿入関数 */
	struct node* n;
	n=(struct node*)malloc(sizeof(struct node));
	n->element=x;
	n->next=p->next;
	p->next=n;
}

void print_clist(struct node *list)
{
	struct node *p;
	printf("(");
	for (p=list->next; p!=NULL; p=p->next) {/* このfor文の使い方は目から鱗でした */
		printf("%c", p->element);
	}
	printf(")\n");
}



int main(void){
	int test[5]={1,2,3,4,5};
	int *t;
	struct node *list;
	char str[]="test";
	char *pstr="test";

	printf(" test[0] = %d\n&test[0] = %p\n",test[0],&test[0]);
	printf(" test[1] = %d\n&test[1] = %p\n",test[1],&test[1]);
	printf("*test    = %d\n test    = %p\n",*test,test);
	/**********実行結果**************************************
				 test[0] = 1
				&test[0] = 002CF7B0
				 test[1] = 2
				&test[1] = 002CF7B4
				*test    = 1
				 test    = 002CF7B0
	つまり、これから&test[0]とtestは同じだということがわかる
	 →testはポインタだ!
	********************************************************/
	t=test; /* 但し、test=&a などの代入はできない */
	printf("*t       = %d\n t       = %p\n",*t,t);
	printf("*(t+1)   = %d\n (t+1)   = %p\n",*(t+1),t+1);
	printf(" t[1]    = %d\n&t[1]    = %p\n",t[1],&t[1]);
	/***********実行結果**************************************
				*t       = 1
				 t       = 001BF8F0
				*(t+1)   = 2
				 (t+1)   = 001BF8F4
				 t[1]    = 2
				&t[1]    = 001BF8F4
	********************************************************/
	t=make_array(5);
	printf("%d %d %d %d %d\n",t[0],t[1],t[2],t[3],t[4]); 
	/* ちゃんと 0 0 0 0 0 が表示されます */

	printf("%s %s\n",str,pstr);
	str[0]='r';
	/* pstr[0]='r' ダメ、絶対 コンパイルはできても実行すると止まります */
	printf("%s %s\n",str,pstr);
	/* str="rest"; 今度はこっちがやっちゃダメ ちなみにこっちはコンパイルで弾かれる */
	pstr="rest";
	printf("%s %s\n",str,pstr);
	/********各実行結果は以下の通り***************************
				test test
				rest test
				rest rest
	*********************************************************/
	
	list = init_clist(str); /* listには構造体のポインタが返ってきます */
	printf("%c",list->next->element); /* 構造体のポインタから構造体のメンバを使うときはこの使い方をします */
	print_clist(list);
	/* 実行すると r(rest) と表示されます */

}

~/******************************
~
~   PG班講義  ポインタ応用
~
~******************************/
~
~#include<stdio.h>
~#include<stdlib.h>
~#include<ctype.h>
~
~int *make_array(int x){ /* 配列を作る関数です */
~   int i,*test;
~   test=(int*)malloc(sizeof(int)*x); /* int型のx個分のメモリを確保 */
~   for(i=0;i<x;i++){
~      test[i]=0;
~   }
~   return test; /* 配列の"ポインタ"を返します */
~}
~
~
~/* リスト構造という構造です 配列に似たデータ構造の一種です */
~
~typedef char elementtype; /* typedefの意味を再確認しておいて */
~
~struct node{
~   elementtype element;
~   struct node* next;
~};
~
~struct node* init_clist(const char *s)
~{
~   struct node *n, *m;
~   char c;
~   m = n = (struct node*)malloc(sizeof(struct node));
~   while (c=*s++) { /* sのポインタの最後はNULL(=0)だからwhileが止まるわけ */
~      if (isalnum(c)) {/* isalnumはcが数字か文字なら1を返す */
~         m = m->next = (struct node*)malloc(sizeof(struct node));
~         m->element = c;
~      }
~   }
~   m->next = 0;
~   return n; /* 構造体の"ポインタ"が返ります */
~}
~
~void insert(struct node* p,elementtype x){ /* リスト挿入関数 */
~   struct node* n;
~   n=(struct node*)malloc(sizeof(struct node));
~   n->element=x;
~   n->next=p->next;
~   p->next=n;
~}
~
~void print_clist(struct node *list)
~{
~   struct node *p;
~   printf("(");
~   for (p=list->next; p!=NULL; p=p->next) {/* このfor文の使い方は目から鱗でした */
~      printf("%c", p->element);
~   }
~   printf(")\n");
~}
~
~
~
~int main(void){
~   int test[5]={1,2,3,4,5};
~   int *t;
~   struct node *list;
~   char str[]="test";
~   char *pstr="test";
~
~   printf(" test[0] = %d\n&test[0] = %p\n",test[0],&test[0]);
~   printf(" test[1] = %d\n&test[1] = %p\n",test[1],&test[1]);
~   printf("*test    = %d\n test    = %p\n",*test,test);
~   /**********実行結果**************************************
~       test[0] = 1
~      &test[0] = 002CF7B0
~       test[1] = 2
~      &test[1] = 002CF7B4
~      *test    = 1
~       test    = 002CF7B0
~     つまり、これから&test[0]とtestは同じだということがわかる
~     →testはポインタだ!
~   ********************************************************/
~   t=test; /* 但し、test=&a などの代入はできない */
~   printf("*t       = %d\n t       = %p\n",*t,t);
~   printf("*(t+1)   = %d\n (t+1)   = %p\n",*(t+1),t+1);
~   printf(" t[1]    = %d\n&t[1]    = %p\n",t[1],&t[1]);
~   /***********実行結果**************************************
~      *t       = 1
~       t       = 001BF8F0
~      *(t+1)   = 2
~      (t+1)   = 001BF8F4
~       t[1]    = 2
~      &t[1]    = 001BF8F4
~   ********************************************************/
~   t=make_array(5);
~   printf("%d %d %d %d %d\n",t[0],t[1],t[2],t[3],t[4]); 
~   /* ちゃんと 0 0 0 0 0 が表示されます */
~
~   printf("%s %s\n",str,pstr);
~   str[0]='r';
~   /* pstr[0]='r' ダメ、絶対 コンパイルはできても実行すると止まります */
~   printf("%s %s\n",str,pstr);
~   /* str="rest"; 今度はこっちがやっちゃダメ ちなみにこっちはコンパイルで弾かれる */
~   pstr="rest";
~   printf("%s %s\n",str,pstr);
~   /********各実行結果は以下の通り***************************
~         test test
~         rest test
~         rest rest
~	*********************************************************/
~	
~   list = init_clist(str); /* listには構造体のポインタが返ってきます */
~   printf("%c",list->next->element); /* 構造体のポインタから構造体のメンバを使うときはこの使い方をします */
~   print_clist(list);
~   /* 実行すると r(rest) と表示されます */
~
~}

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS