ポインタ応用
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
~/******************************
~
~ 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)だ...
~ if (isalnum(c)) {/* isalnumはcが数字か文字な...
~ m = m->next = (struct node*)malloc(siz...
~ 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],&t...
~ printf(" test[1] = %d\n&test[1] = %p\n",test[1],&t...
~ 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)だ...
~ if (isalnum(c)) {/* isalnumはcが数字か文字な...
~ m = m->next = (struct node*)malloc(siz...
~ 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],&t...
~ printf(" test[1] = %d\n&test[1] = %p\n",test[1],&t...
~ 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) と表示されます */
~
~}
ページ名: