Like Tree2Likes
  • 2 Post By quzah

Modifying multiple link lists (from main) in a function

This is a discussion on Modifying multiple link lists (from main) in a function within the C Programming forums, part of the General Programming Boards category; Is it possible to modify a link list created in the main function in another function without using a return. ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    6

    Modifying multiple link lists (from main) in a function

    Is it possible to modify a link list created in the main function in another function without using a return. I'm asking because in my assignment I want to modify 3 link lists in the function and as far as I know it's only possible to return one modified link list. I figured out how to modify a simple int variable that way and a char array, but can't seem to figure out the proper syntax for the link lists.

    For the int variable:
    Code:
    #include <stdio.h>
    
    void changeVar(int *x);
    
    int main(void){
    
       int x=0;
       
       changeVar(&x);
       
       printf("x = %d\n\n", x);
    
       system("PAUSE");
       return 0;
    }
    
    void changeVar(int *x){
          
       *x=2;
    }
    For the array:
    Code:
    void changeArray(char *arr);
    
    int main(void){
    
       char array[10];
       
       array[0]='d';
       changeArray(array);
       
       printf("array[0] = %c\n\n", array[0]);
    
       system("PAUSE");
       return 0;
    }
    
    void changeArray(char *arr){
          
       arr[0]='a';
    }
    Here is what I have for the link list (I've tried different *, **, and & configs) :
    Code:
    // Link List Struct
    typedef struct KnightsMartRestockProduct {
       int itemNum;
       struct KnightsMartRestockProduct *next;
    } KMRestockProduct;
    
    // Function Header
    void customer(KMRestockProduct *restockList);
    
    
    int main(void){
    
       KMRestockProduct* KMRestockList=(KMRestockProduct *)malloc(sizeof(KMRestockProduct));
    
          
       customer(KMRestockList);      // Call to function
       
       printf("itemNum = %d\n\n", KMRestockList->itemNum);
    
       system("PAUSE");
       return 0;
    }
    
    void customer(KMRestockProduct *restockList){
       
       KMRestockProduct* help_ptr=restockList;
       KMRestockProduct* pNewRestock=(KMRestockProduct *)malloc(sizeof(KMRestockProduct));
       
       pNewRestock->itemNum = 2;
       pNewRestock->next = NULL;
       
       if(restockList==NULL){
          pNewRestock->next=restockList;
          restockList=pNewRestock;
       }
       
       while(help_ptr->next != NULL && help_ptr->next->itemNum < pNewRestock->itemNum)
             help_ptr=help_ptr->next;
       
       pNewRestock->next=help_ptr->next;
       help_ptr->next=pNewRestock;
    }
    I'm just trying to modify the itemNum field of the link list from inside the function without having to use return.

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Hmmmm...sorry about the lack of white space in the customer function

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Everything in C is passed as a value. If you want to change the value of something passed to a function, you need to be passing a pointer to that type of object, rather than the value the object contains. Thus, if you want to modify a linked list's pointer (say to the first node for example), then you need to pass a pointer to that type of object:
    Code:
    struct node { ... } *list = NULL;
    ...
    void editlist( struct node **p );
    ...
    editlist( &list );

    Quzah.
    whiteflags and darkbrew7 like this.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Thanks quzah!
    Last edited by darkbrew7; 06-07-2011 at 08:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm confused about link lists (again)
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 06-13-2008, 08:13 PM
  2. Win Main link error
    By KoshiB in forum Windows Programming
    Replies: 2
    Last Post: 03-20-2006, 08:56 AM
  3. Link Lists
    By Loki in forum C Programming
    Replies: 2
    Last Post: 06-07-2003, 09:21 PM
  4. MFC :: Modifying string table for multiple file extensions
    By SyntaxBubble in forum Windows Programming
    Replies: 0
    Last Post: 06-06-2003, 05:18 PM
  5. Link lists
    By thedumbmutt in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2002, 01:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21