The trouble is now you are modifying *head too much! It's fine to
change it above, but to add to the tail you need to find the last node
without changing *head. Just introduce a local pointer and use that
instead:
Hey Ben.. I understood the concept I think

, thanks, I even implemented
the list_add() using assignment, the one you mention as (a) in some
earlier post, it works great:
/* This C implementation of singly linked list implements 3 operations: add, remove and insert elements at some specific position in
* the list.
*
* assignment VERISON 0.0
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* How about always keeping track of tail of list (end of list I mean) and then pass that to list_add(), that way, list_add() will always
be O(1), rather than O(n)
*/
struct my_struct* list_add( struct my_struct *, const int);
void list_print( const struct my_struct* );
void list_print_element(const struct my_struct *const );
struct my_struct
{
int num;
struct my_struct* next;
};
int main(void)
{
struct my_struct* list_head = NULL;
printf("List default\n");
list_print(list_head);
list_head = list_add(list_head, 1);
list_head = list_add(list_head, 2);
list_head = list_add(list_head, 3);
list_print(list_head);
return 0;
}
struct my_struct* list_add(struct my_struct* head, const int i)
{
struct my_struct* s = head;
struct my_struct* p = malloc( 1 * sizeof(*p) );
if( NULL == p )
{
fprintf(stderr, "IN %s, %s: malloc() failed\n", __FILE__, "list_add");
return NULL;
}
p->num = i;
p->next = NULL;
if( NULL == s )
{
/* printf("Base Node was NULL, so adding current maloc()ed struct to base: %d\n\n", p->num); */
return head = p;
}
else
{/*
printf("list_head->num = %d\n", (*head)->num );
printf("Base node is non-NULL, so adding it to the end of the list: %d\n", p->num); */
for( ; NULL != head->next; head = head->next ) ;
head->next = p;
}
return s;
}
void list_print( const struct my_struct *const sp )
{
const struct my_struct* p = sp;
for( ; NULL != p; p = p->next )
{
list_print_element(p);
}
printf("\n------------------\n");
}
void list_print_element(const struct my_struct *const p )
{
if( p )
{
printf("Num = %d\n", p->num);
}
else
{
printf("Can not print NULL struct \n");
}
}
======================= OUTPUT =======================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra singly-LL.c
[arnuld@dune programs]$ ./a.out
List default