-2

I am writing code to create a proper link lists. please tell the errors in this programerrors while compiling

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    typedef void* cadtpointer;
    struct cadtlist 
    {
      cadtpointer data;
      struct cadtlist* next;
     }; /* structure*/

     struct cadtlist* cadt_list_init( )
     {
        struct cadtlist * temp, * head,* list;
        int num;
        char *p, s[100];
        printf(" enter the number of nodes to be created");
        while (fgets(s, sizeof(s), stdin)) 
        {
          num = strtol(s, &p, 10);
          if (p == s || *p != '\n') 
            {
                printf("Please enter an integer: ");
            }
          else break;
        } 
        while (num != 0)
        {
         if(NULL != list )
         {
           list->next = cadt_create_list(temp);
            }
         else
        {
          list = cadt_create_list(temp);
          head = list;
        }
        num--;
      }


    }
   struct cadtlist* cadt_create_list(struct cadtlist * list)
   {
    int n;
    char * data;
    struct cadtlist * newnode;
    newnode = ( struct cadtlist *) malloc( sizeof(struct cadtlist));
    if( NULL != newnode )
    {
        printf(" enter the data to be added");
        scanf("%s", data);
        n= cadt_add_list(data,newnode);
        if( 1 == n)
        return newnode; 
    }
    else
    {
        printf(" error while allocating memory");
        exit(1);
    }
   }
   struct cadtlist* cadt_add_list( char* item,struct cadtlist * list) 
  {
   list->data = item;
   if(NULL == list->data)
    {
        return list;
    }
    else
    {
        printf(" error while adding data");
        exit(1);
    }
   } 

 int main()
 {
    struct cadtlist* list1;
    list1 = cadt_list_init();

    return 0;
  }
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Don't post pictures of text, post the text. With copy/paste you know... – Jabberwocky Apr 29 '16 at 06:57
  • To start with , you need a forward declaration of `cadt_create_list()` function... – Sourav Ghosh Apr 29 '16 at 07:00
  • Welcome to Stack Overflow! [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Apr 29 '16 at 07:00
  • [warning : Implicit declaration error](https://stackoverflow.com/questions/30692183/warning-implicit-declaration-error) and [warning: implicit declaration of function](https://stackoverflow.com/questions/8440816a/warning-implicit-declaration-of-function) – kaylum Apr 29 '16 at 07:00

1 Answers1

0

1) - Use prototypes for those forward declarations

2) - You are passing the address of a local variable

char * data; /* Local variable */
struct cadtlist * newnode;
newnode = ( struct cadtlist *) malloc( sizeof(struct cadtlist));
if( NULL != newnode )
{
    printf(" enter the data to be added");
    scanf("%s", data);
    n= cadt_add_list(data,newnode); /* Passing local variable */

and assignigning the address to another variable

struct cadtlist* cadt_add_list( char* item,struct cadtlist * list) 
{
    list->data = item;

Notice that when you exit from cadt_create_list(), data is no longer available (may contain garbage)

Change to

struct cadtlist* cadt_add_list( char* item,struct cadtlist * list) 
{
    list->data = strdup(item); /* Reserve space for the string */
David Ranieri
  • 39,972
  • 7
  • 52
  • 94