본문 바로가기

카테고리 없음

C언어 재귀함수로 구조체 출력하기

#include <stdio.h>
#include <string.h>

typedef struct userdata
{
	char username[32];
	char userphone[32];

	struct userdata* Pnext;
}userdata;

void recursion(userdata* pUser) {
	if (pUser == NULL) {
		return;
	}
	printf("[%p], %s, %s, Pnext : %p\n", pUser, pUser->username, pUser->userphone, pUser->Pnext);
	recursion(pUser->Pnext);
	return;
	
}

int main()
{
	userdata userlist[4] = {
		{"김두식", "010-2425-5353",NULL},
		{"박찬호", "010-4256-2176",NULL},
		{"김하성", "010-4583-1445",NULL},
		{"이정후", "010-0641-5214",NULL}
	};

	userdata* pUser = NULL;

	userlist[0].Pnext = &userlist[1];
	userlist[1].Pnext = &userlist[2];
	userlist[2].Pnext = &userlist[3];
	userlist[3].Pnext = NULL;

	recursion(&userlist[0]);
	/*pUser = &userlist[0];
	while (pUser != NULL) {
		printf("%s, %s\n", pUser->username, pUser->userphone);
		pUser = pUser->Pnext;
	}*/

	return 0;
}