Thread: pointer assignment inside a function ???

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    91

    pointer assignment inside a function ???

    Hello,

    This program has a function alterlist() which takes a node to a list and a function check() as an argument and removes all nodes that return a non-zero value from the function check()


    The program works fine . I couldn't understand one thing in the function alterlist() when I try doing

    link y = b;

    the program gives me an error on running
    if I do that I save some condition checking inside the loop since i wouldn't have to vare about where the new list starts, but it doesn't work ??????




    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    /*  ******************************************************* */
    /*  This program does arithmetic operation on a linked list */
    /*  ******************************************************* */
    
    typedef struct node *link;
    
    struct node
    {
           int item; 
           link next;
    };
    
    link makelist(link x, int num);
    void printlist(link x);
    int check(link x);
    link alterlist(link x, int check(link));
    
    int main()
    {
        link x = NULL;
        x = makelist(x, 5);
        x = alterlist(x, check);
        printlist(x);
        return 0;
    }
    
    link makelist(link x, int num)
    {
         int i;
         if(0 == num)
         {
             x = NULL;
             return;
         }
         link a = malloc(sizeof *a);
         x = a;
         a->item = 1;
         if(1 == num)
         {
              a->next = NULL;
              return;
         }
         for(i = 2; i <= num; i++)
         {
             a->next = malloc(sizeof *a);
             a = a->next;
             a->item = i;
             if(i == num)
              {
                   a->next = NULL;
                   break;
              }
         }
         return x;
    }
    
    void printlist(link x)
    {
        link b = x;
        while(b != NULL)
        {
             printf("%d, ", b->item);
             b = b->next;   
        }
    }
    
    
    int check(link x)
    {
        if (NULL == x)
             return 0;
        else
            return (x->item %2);
    }
    
    link alterlist(link x, int check())
    {
         link a = x, b; 
         link y = NULL;
         while(a != NULL)
         {
             if(0 == check(a))
             {
                  if(a == x)
                  {
                       b = a;
                       y = b;
                       a = a->next;
                  }
                  else
                  {
                       b = a;
                       if(NULL == y)
                            y = b;
                       a = a->next;  
                  }
             }
             else
             {
                  if(a == x)
                       a = a->next;
                  else
                  {
                      b->next = b->next->next;
                      a = a->next;
                  }
             }
         }
         return y;
    }

  2. #2
    Registered User
    Join Date
    May 2005
    Posts
    4
    in the function alterlist, you haven't initialised b. it is not a global variable either. so what is it??

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    that is because the b pointer has not been initialized to NULL,

    Code:
    link a = x, b=NULL; 
     link y = b;
    hope this hepls

    s.s.harish

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    that is because the b pointer has not been initialized to NULL,


    Code:
    link a = x, b=NULL;
    link y = b;
    make those changes and remove/comment lines 91, 97 and 98 since b has already been initilaized.

    The program still doesn't work ?????

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    it works fine for me

    this is the outout i get
    Code:
    2, 4,
    which compiler are using and what error do you get

    i think is that the seg fault

    s.s.harish

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    I am using dev-c++ compiler by bloodshed which one did u try it with ?

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    no error it just doesn't give me an output when i run the exe file through cmd prompt

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I'm kinda surprised that it compiles.
    > link alterlist(link x, int check(link));

    Are you by any chance trying to pass a pointer to a function ?
    Function pointer syntax is a bit obscure, but there are quite a few examples on the board, which a search for say "function pointer" would show up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  4. Replies: 7
    Last Post: 07-04-2007, 12:46 PM
  5. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM