[ create a new paste ] login | about

Link: http://codepad.org/jwQMESxZ    [ raw code | output | fork ]

C, pasted on Feb 9:
#include <stdio.h>
#include <stdlib.h>

/* 構造体型の宣言 */
typedef struct Test {
	int aaaaa;
	int bbbbb;
	struct Test *next;
} Test;

int main(void) {
	Test *prev, *now;		/* 構造体を繋ぐ時に使うポインタ */
	Test *hyouji;			/* 表示に使うポインタ */
	Test *p;			/* 解放時に使うポインタ */
	
	/* 零番目の構造体(中身は無し) */
	Test *first = (Test *)malloc(sizeof(Test));
	first->next = NULL;
	prev = first;
	
	/* 一番目の構造体 */
	now = (Test *)malloc(sizeof(Test));
	now->aaaaa = 11;
	now->bbbbb = 12;
	now->next = NULL;
	first->next = now;
	prev = now;
	
	/* 二番目の構造体 */
	now = (Test *)malloc(sizeof(Test));
	now->aaaaa = 21;
	now->bbbbb = 22;
	now->next = NULL;
	prev->next = now;
	prev = now;
	
	/* 三番目の構造体 */
	now = (Test *)malloc(sizeof(Test));
	now->aaaaa = 31;
	now->bbbbb = 32;
	now->next = NULL;
	prev->next = now;
	prev = now;
	

	printf("零番目のデータ next%p\n", first->next);
	
	hyouji = first->next;
	printf("一番目のデータ aaaaa:%d  bbbbb:%d  next%p\n", hyouji->aaaaa, hyouji->bbbbb ,hyouji->next);
	
	hyouji = hyouji->next;
	printf("二番目のデータ aaaaa:%d  bbbbb:%d  next%p\n", hyouji->aaaaa, hyouji->bbbbb ,hyouji->next);
	
	hyouji = hyouji->next;
	printf("三番目のデータ aaaaa:%d  bbbbb:%d  next%p\n", hyouji->aaaaa, hyouji->bbbbb ,hyouji->next);
	
	/* 領域の開放 */
	p = first;
	while (p != NULL) {
		now = p->next;
		free(p);
		p = now;
	}
	return 0;
}


Output:
1
2
3
4
零番目のデータ next0x804a0a8
一番目のデータ aaaaa:11  bbbbb:12  next0x804a0d8
二番目のデータ aaaaa:21  bbbbb:22  next0x804a108
三番目のデータ aaaaa:31  bbbbb:32  next(nil)


Create a new paste based on this one


Comments: