
January 3rd, 2013, 04:53 PM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 112
Time spent in forums: 1 Day 4 h 12 m 48 sec
Reputation Power: 1
|
|
|
Segmentation fault with linked list
Hi.
i'm currently taking my first steps with linked list, and i'm having a problem:
whenever i run the program it terminates and mumbles somthing about segmentaion fault.
from what i've read, it means the program tried to access an unauthorized memory block.
i just couldn't figure out what's wrong with the code, and i can really use some help.
here's the code:
Code:
struct numbers
{
int num;
struct numbers *next;
};
void Append(struct numbers **headRef, int num)
{
struct numbers *current;
struct numbers *newNum;
current=*headRef;
newNum=(struct numbers *)malloc(sizeof(struct numbers));
newNum->num=num;
newNum->next=NULL;
//case of first node
if (current==NULL)
*headRef=newNum;
else
{
while (current!=NULL)
current=current->next;
current->next=newNum; //it always fails here, at the second call to Append
}
}
int main()
{
struct numbers *head=NULL;
struct numbers *conductor;
int i;
for (i=1; i<6 ;i++)
Append(&head, i);
conductor=head;
while (conductor->next!=NULL)
printf("%d", conductor->num);
}
it always fails at the same code line (marked in red).
thanks in advanced!
|