Link list function call

This is a discussion on Link list function call within the C Programming forums, part of the General Programming Boards category; hello im having some trouble in my function call here.. what i am trying to accomplish is to build a ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    43

    Link list function call

    hello im having some trouble in my function call here.. what i am trying to accomplish is to build a linked list in a function and send the link back to main. Can anyone sort out my problem?(i think it might be syntax with how i am calling the function)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    struct info_node
    {
        char fname[20] ;
        char lname[20] ;
        int rank ;
        float score ;
        struct info_node * next ;
    } ;
    
    int build_list(struct info_node*, int)
    
    int main()
    {
    	FILE* infile ;
    	FILE* infile2 ;
    	struct info_node* link1, * link2 ;
     	struct info_node* top, * top2 ;	
    	struct info_node* move_pntr = NULL, * move_pntr2 = NULL ;
    	struct info_node* pre_pntr ;
    	struct info_node* current_pntr ;
    	int num = 0 ;
    	
    
        infile2 = fopen("p3data.txt", "r") ;
    	infile = fopen("p3purge_data.txt", "r") ;
    
    	build_list(&link1, &top) ;
    
    
    
    
    fclose(infile);
    fclose(infile2);
    return (0) ;
    
    } //end of func main()
    int build_list(info_node * lk1, int tp)
    {
    	int pre_pntr = NULL ;
    	int current_pntr = NULL ;
    
    	while( !feof(infile) )
        {
    
    		lk1 = (info_node *)malloc(sizeof(info_node) ) ;
    		fscanf( infile, "%s%s%d%f", lk1->fname , lk1->lname, &lk1->rank, &lk1->score ) ;
    
    		lk1->next = NULL ;
    		pre_pntr = lk1 ;
    		current_pntr = lk1 ;
    		tp = lk1 ;
    
    		lk1->next = tp ;
    		tp = lk1 ;
    	}
    	return(*tp) ;
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    "&#37;s %s %d %f"

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    kk i think i get what you mean there but lets say i declare the FILE* infile inside of my function would that still be a problem?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What happens in the function, stays in the function. Namely the value of lk1 can never change (what it points to can, but not the variable itself). That is to say, the malloc into lk1 is not going to be seen in the main function. Either make the first parameter a **, or don't pass it in at all but return a pointer to the head node.

    Also, it would appear that you don't make a linked list, so much as a bunch of disconnected nodes that are linked to themselves (i.e., every time through the list you set lk1->next equal to lk1, instead of waiting to connect it to the next one).

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    so basically send a pointer to malloc from main into my function and work from there? Will the infile scan work inside of the function if i do that?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You're sending a pointer. You need to send a pointer to a pointer (you can only change what is pointed to, and you need to change a pointer, so you need to point to a pointer).

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    im still having trouble understanding how to get this function to work properly, so instead of passing a pointer to a pointer i can remove the struct info node pointer, build the list and return a pointer to the head, or top in this case

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    That ought to work. Either/or.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    right. i get the idea but how am i supposed to build the list if i dont have a pointer to the structure?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You are allowed to declare variables inside a function.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    so basically just shove this into my function and that works?
    Code:
    struct info_node
    {
        char fname[20] ;
        char lname[20] ;
        int rank ;
        float score ;
        struct info_node * next ;
    } ;

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by killmequick View Post
    so basically just shove this into my function and that works?
    Code:
    struct info_node
    {
        char fname[20] ;
        char lname[20] ;
        int rank ;
        float score ;
        struct info_node * next ;
    } ;
    No, you don't need to move that.

    You can simply declare some struct info_node * more_pointers;

    Considering that your code is horribly broken at the moment anyway since tp is of type int, I'd say you need a few more pointers!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Sometimes its far easier to write your own code than it is to copy and paste someone else's then alter it to your specifications.

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    well so far this is the addition ive made im just using test code to see if i can get it to print out before i implement my official list. Can someone give me a hint as to how i can return the value of the node to main

    Code:
    int build_list(struct info_node* pnode)
    {
    
    	FILE* infile ;
    	struct info_node* link1 ;
    	struct info_node* top ;
    	int result ;
    
    
    		link1 = (info_node *)malloc(sizeof(info_node) ) ;
    		fscanf( infile, "%s%s%d%f", link1->fname , link1->lname, &link1->rank, &link1->score ) ;
    
    		top = link1 ;
    	
    	return(top) ;
    }

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I would do it in much the same way as you have it there, except for the "reading from a file you didn't open" part.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM

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