Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code, it can run. But when i run it it always says "There is segment fault". I run it on Ubuntu Linux. I don't know why it happens, if you know please tell me, thanks. :)

/*
   Copyright( c ), 2010, @ linux laboratory in xiyou.
   All rights reserved.

   File name: front_list.c.
   Programmer's name: Harry . wei.
   Start time: 5, 31.
   Finish time: 5,31.

   Programme's introduction: use front list to init the list.

   Modify time:
   Modify programmer:

   Modify introduction:

   */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef	struct Mystruct_Node
{
	int	data;

	struct Mystruct_Node	* next;
}*Linklist, List;

int main( void )
{
	printf( "111111" );
	Linklist	l;
	Linklist	s;
	char 	c;
//	int	flag = 1;

	l -> next = NULL;

	printf( "Please enter characters:" );
	printf( "11111\n" );
	while( true )
	{
		c = getchar();

		if( c != '$' )
		{
			s = ( Linklist )malloc( sizeof( List ) );
			s -> data = c;
			s -> next = l -> next;               // First, s->next get the NULL value from l->next.
			l -> next = s;
		}
		else
		{
			break;
		}
	}

	l = l -> next;

	while( l -> next != NULL )
	{
		printf( "%c", l -> data );

		l = l -> next;
	}

	return( false );
	



}
Posted
Updated 30-May-10 22:53pm
v3
Comments
Moak 31-May-10 4:54am    
Updated subject and tags, hope this is what you are looking for.

Linklist l; //Uninitilazed pointer to List structure
Linklist l = (Linklist)malloc(sizeof(List));//Initialized pointer to List structure (heap)
List     l; //Initialized List structure (stack)
 
Share this answer
 
v2
Comments
harish85 2-Jul-11 23:35pm    
yes, l->next access on your code @ s->next= l->next line would bust.
my 5.
Hai actually Segmentation Fault is comming to Memory fault .. ur parametesr are not allocate memmory thet time its error is comming ..pls once ckeck ur code..
 
Share this answer
 
harryxiyou wrote:
l -> next = NULL;


You must NOT do that since l is uninitialized, that is, points to garbage.
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900