Passing pointer to function exercise need advice

This is a discussion on Passing pointer to function exercise need advice within the C Programming forums, part of the General Programming Boards category; Here is the exercise: Write a function called insertEntry to insert a new entry into a linked list. Have the ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    163

    Passing pointer to function exercise need advice

    Here is the exercise:

    Write a function called insertEntry to insert a new entry into a linked list. Have the procedure take as arguments a pointer to the list entry to be inserted(of type struct entry) and a pointer to an element in the list AFTEr which the new entry is to be inserted.
    My preliminary work, NOT FINISHED, is resulting in no out put to the console window. Before I implement the actual function I just want to make sure I'm doing it right without using the function. And I dunny know why. *scrathes head*

    Code:
    #include <stdio.h>
    
    struct entry
    {
    	int values;
    	struct entry *next;
    };
    
    /* void insertEntry(struct entry *list_entry,struct entry *later_entry)
    {
    	list_entry=&later_entry)
    } */
    
    struct entry n1,n2,n3,n2_3;
    int i;
    
    int main(void)
    {
    	struct entry *list_pointer=&n1;
    
    	n1.values=100;
    	n1.next=&n2;
    
    	n2.values=200;
    	n2.next=&n3;
    
    	n3.values=300;
    	n3.next=(struct entry *)0;
    
    	n2_3.values=250;
    
    	while(list_pointer !=(struct entry *) 0);
    	{
    		printf("%i\n",list_pointer->values);
    		list_pointer=list_pointer->next;
    	}
    
    	list_pointer=&n1;
    	n2_3.next=n2.next;
    	n2.next=&n2_3; // Remove for function?
    
    	// insertEntry(&n2_3,&n3);
    
    	printf("\nAfter entry insertion:\n");
    
    		while(list_pointer !=(struct entry *) 0)
    	{
    		printf("%i\n",list_pointer->values);
    		list_pointer=list_pointer->next;
    	}
    
    	getchar();
    	return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    163
    haaha I got it. Syntax error. friend gave me heads up>

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    163
    ok I need some help on this. I dont know if I'm reading the problem wrong or what but am having trouble inserting the new item using a subroutine. Some reason I'm not wrapping my head around it right.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    You have the function call correct. Make sure later_entry's next pointer get's list_entry's next pointer, before you actually change list_entry's next to later_entry.

    Oh, another, you have to use the pointer reference operator:

    Code:
    later_entry->next = list_entry->next;
    I gave you the part the trips up most people doing singly linked lists for the first time. You can come up with the rest.
    Last edited by Cynic; 03-11-2012 at 09:36 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    163
    ok thank you for your help. I can get it easily without a function just getting it into the function was confusing me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c ++ passing a pointer into a function
    By Ron in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2008, 08:31 PM
  2. passing method pointer as a function pointer
    By sevcsik in forum C++ Programming
    Replies: 5
    Last Post: 12-30-2007, 05:19 AM
  3. Replies: 4
    Last Post: 11-05-2006, 01:57 PM
  4. Passing pointer into function bug
    By CodeMonkey in forum C++ Programming
    Replies: 4
    Last Post: 04-26-2005, 11:13 PM
  5. Passing Pointer to a function
    By Nishant Ghai in forum C Programming
    Replies: 4
    Last Post: 02-27-2003, 05:40 AM

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