Hi, this program I wrote is about creating a notebook using linked list as a final requirement. A notebook with a page number, line number and also a text on each line number.

Here is my insert note function where I will enter the Page#, Line#, and the Text and the inputs will just be passed to another functions where the nodes will be inserted.

When calling this function for the first time, there is no problem, the node will be inserted. but on the second call of this function, the program will stop on the statement which allocates memory for variable txt which is a char pointer.

I can't figure out what is the problem with the code. I tried inserting the code "free(txt);" at the bottom of the function before the "return;" statement but it will cause an error even on the first call of this function.

Can anyone help me? Thanks in advance.
Here is my code below:

Code:
void InsertNote(){
int pn,ln,i;
char *txt,c; struct notebook *temp; temp=note; txt = (char *) malloc(sizeof(char)); printf("\n\nINSERT NOTE"); printf("\nWhat Page Number? "); scanf("%d",&pn); printf("What Line Number? "); scanf("%d",&ln); fflush(stdin); printf("Enter Your Note: "); gets(txt); if(temp==NULL){
AddFirst(txt,pn,ln);
} else{
while(temp!=NULL){
if(temp->pagenum <= pn){
if(temp->linenum < ln){
c++;
}
}
temp=temp->next;
}
if(c==0){
AddFirst(txt,pn,ln);
} else if(c<count()){
AddAfter(txt,pn,ln,++c);
} else{
Append(txt,pn,ln);
}
} //free(txt); return;
}