This code is taken from: page 20-21 of this lecture notes pdf from ocw.
struct node∗ nalloc ( int data )
{
struct node∗ p=( struct node ∗) malloc ( sizeof (node )) ;
if ( p!=NULL) {
p−>data=data ;
p−>next=NULL;
}
return p;
}
struct node∗ addfront ( struct node∗ head , int data )
{
struct node∗ p= nalloc (data );
if ( p==NULL) return head ;
p−>next=head;
return p;
}
I think the code is wrong because the pointer p is local to nalloc() and using it in addfront() would yield undefined behavior. I have seen the answer to this question and believe I am correct but can someone verify?