Below is the Program to add a new node to the ascending order linked list.
There was bug in below program which causing segmentation Fault
I figured out the cause for segmentation Fault using gdb
.
But i did not get what is reason
for segmentation Fault. Can anyone tell reason for segmentation Fault.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data ;
struct node *link ;
} ;
void add ( struct node **, int ) ;
void display ( struct node * ) ;
int main( )
{
struct node *p ;
p = NULL ;
add ( &p, 5 ) ;
add ( &p, 1 ) ;
add ( &p, 6 ) ;
add ( &p, 4 ) ;
add ( &p, 7 ) ;
display ( p ) ;
}
void add ( struct node **q, int num )
{
struct node *r, *temp = *q ;
r = ( struct node *) malloc ( sizeof ( struct node ) ) ;
r -> data = num ;
if ( *q == NULL || ( *q ) -> data > num )
{
*q = r ;
( *q ) -> link = temp ;
}
else
{
while ( temp != NULL )
{
if ( temp -> data <= num && ( temp -> link -> data > num ||
temp -> link == NULL ))
{
r -> link = temp -> link ;
temp -> link = r ;
return ;
}
temp = temp -> link ;
}
}
}
void display ( struct node *q )
{
printf ( "\n" ) ;
while ( q != NULL )
{
printf ( "%d ", q -> data ) ;
q = q -> link ;
}
}
I changed the order in this line from
if ( temp -> data <= num && ( temp -> link -> data > num || temp -> link == NULL ))
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
to
if ( temp -> data <= num && ( temp -> link == NULL || temp -> link -> data > num ))
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The above changed work like a charm But i dint get reason behind this changes.
So help me to figure out the reason
**Updated
If short-circuit evalution is real cause then why this below program works
int main()
{
int a=10,b=1,c=0;
int d;
d = (a && (c || b));
printf("%d",d);
getchar();
}