<c언어> malloc이 처음에만 돌아가고 나중에는 안돌아가네요ㅠㅠ

이상민@Naver의 이미지

텍스트파일을 입력받아서 연결리스트 만들려고 하는데
malloc함수가 두번째까지는 돌아가고 그다음부터는 안돌아가요ㅠㅠ 왜이러는지 아시는 능력자분 혹시 계신가요??

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <stdlib.h>
typedef struct test* ptest;
typedef struct test {
	char name;
	int data;
	ptest link;
}test;
FILE *fp;
void input(ptest*, ptest*);
 
int main()
{
	char str[20];
	ptest head, curr;
	int i = 0;
	fp = fopen("test.txt", "r");
	fscanf(fp, "%s\n", str);
	//head = (ptest)malloc(sizeof(ptest));
	//head->link = NULL;
 
	while (!feof(fp))
	{
		input(&head, &curr);
		i++;
	}
	return 0;
}
 
void input(ptest *head, ptest *curr)
{
	static int i = 0;
	ptest temp;
	temp=(ptest)malloc(sizeof(ptest));
	temp->link = NULL;
	fscanf(fp, "%c %d\n", &(temp->name), &(temp->data));
 
	if (i==0)
	{
		*head = temp;
		*curr = temp;
		i++;
	}
	else
	{
		(*curr)->link = temp;
		*curr = (*curr)->link;
	}
}
익명 사용자의 이미지

이런 짓을 했기 때문이지요:

temp=(ptest)malloc(sizeof(ptest)); // (ptest)malloc(sizeof(test)) 이겠지요...
 
// int *p = (int *)malloc(sizeof(int));를 생각해보세요.

이런 실수를 저지르는 사람을 한두 번 본 게 아닙니다. 프로그래머에게 비슷한 걸 두 번 이상 타이핑하게 만드는 모든 언어적 요소는 거의 반드시라고 해도 좋을 정도로 문제를 일으키는 것 같군요.