passing arguments to functions: what'swrong with my pointer design?

This is a discussion on passing arguments to functions: what'swrong with my pointer design? within the C Programming forums, part of the General Programming Boards category; I feel a bit of an arse, but I still don't seem to get it. General concept of the attached ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    99

    passing arguments to functions: what'swrong with my pointer design?

    I feel a bit of an arse, but I still don't seem to get it.
    General concept of the attached program:
    - define a few structures
    - populate a linked list (in fillTestBook)
    - print it (printList)

    The code below prints the linked list while in the fillTestBook function, but in the main it gives a memory error. Why, why, drives me nuts.
    Your patience is much appreciated.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    struct order {
    	char dir;
    	float price;
    	int size;
    	char instr[5];
    };
    
    struct bookOrder {
    	struct bookOrder *next;
    	struct order *order;
    };
    
    void printBook(struct bookOrder *book) {
    
    	while (book != NULL) {
    		printf("Printbook %f \n",(book->order)->price);
    		book = book->next;
    	}
    }
    
    void insertSellOrder(struct bookOrder *sellBook, struct order newOrder) {
    
    	struct bookOrder *prev, *curr;
    	prev = curr = sellBook;
    }
    
    void processSellOrder() {
    }
    
    
    
    void fillTestBook(struct bookOrder **book) {
    
    	struct order order1;
    	struct order order2;
    	struct order order3;
    	struct bookOrder book1;
    	struct bookOrder book2;
    	struct bookOrder book3;
    	struct bookOrder *head;
    
    
    	//first record
    
    	order1.price = 1.1;
    	order1.size = 25;
    
    	book1.order = &order1;
    	book1.next = NULL;
    
    	//initialize the pointers
    	head = &book1;
    
    	//secnd record
    	order2.price = 1.2;
    	order2.size = 10;
    
    	book2.order = &order2;
    	book2.next = NULL;
    	book1.next = &book2;
    
    	//third record
    	order3.price = 1.6;
    	order3.size = 100;
    
    	book3.order = &order3;
    	book3.next = NULL;
    	book2.next = &book3;
    
    	*book = head;
    	printBook(*book);
    }
    
    int main(void) {
    
    	struct bookOrder *book;
    	book = malloc(sizeof(struct bookOrder));
    	fillTestBook(&book);
    	printBook(book);
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is there some reason you abandon your other thread?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Yes, in hindsight I thought that it is a different case altogether. Same topic (but quite a lot of posts on this) but different code.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,702
    Read this
    Linked Lists in C Tutorial - Cprogramming.com

    You have no idea of how to use malloc in a link list; you allocated space to hold one set of pointers;
    This is NOT enough space to hold a single book let alone several.

    This code and the other (struct bookOrder) code still is local variables; they need replaced by malloc() calls
    Code:
    struct bookOrder book1
    Tim S.
    Last edited by stahta01; 07-27-2011 at 07:35 AM.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Most new programmers have a general trend to overcomplicate the matter at hand. The cause of this is because instead of sitting down and actually thinking about the problem at hand, new programmers decide to just jump in and start coding. With this in mind, let's revisit what your thought process should have been:

    Problem: Create a program to handle book orders
    Your solution: Implement a linked list. Now unless for academia, I can tell you this isn't necessarily the best option; however, I will play along.

    Problem: What is a linked list?
    YOUR answer:
    Code:
    struct order {
    	char dir;
    	float price;
    	int size;
    	char instr[5];
    };
    struct bookOrder {
    	struct bookOrder *next;
    	struct order *order;
    };
    My Answer: A linked list is simply a group of structures (containers to be exact, you can have a series of class objects) that have at least one member that is a pointer to the next object in the list. - Notice I didn't start programming anything yet, however with my answer let's look at your structure (node definition) and see what we could come up with:

    Code:
    struct order {
    	char dir;
    	float price;
    	int size;
    	char instr[5];
    	struct order* next;
    };
    Problem: How do I implement my linked list in my program?
    YOUR answer: <more random coding>

    My answer: A linked list is a group or chain of nodes. As with all chains we need to define an anchor or starting point (which can be as simple as an instance of our node) and an ending point. You define the end of a linked list by having the last node’s next pointer set to NULL.Now in the beginning of our program to signify an initially empty list we will set firstNode to NULL..

    With that in mind let's revisit our program:
    Code:
    int main (void){
    
    	struct order *myOrder;
    
    	myOrder = NULL;
    
    	return (0);
    }
    The remaining concept to grasp the fundamentals of linked lists lies in successfully adding and removing nodes from our list. These topics revolve around dynamic memory allocation, using malloc() and free().This topic is as easy to understand however it seems to be the hardest for new programmers to grasp. The prototype for malloc() is:
    Code:
    void* malloc(size_t size);
    All malloc() does is allocate size bytes and returns a pointer of type void to that memory, or NULL if an error occured.The standard convention for using malloc is:
    Code:
    pointer = malloc(n * sizeof(*pointer));
    where n is the number of elements you need and *pointer (the dereferenced pointer, which in your case would be **pointer) is used to determine the size of the individual element.

    So let's take a look at our addNode function:
    Code:
    void addNode(struct order** bookOrder){
    	
    	struct order* newNode = malloc(sizeof(**bookOrder));
    	//if couldn't allocate memory, exit
    	if(!newNode){
    		printf("Couldn't allocate memory\n");
    		return;
    	}
    }
    Now that we understand how to allocate the memory, lets take a look at how we add the node to our list. Remember that we signify the end of our list by having the last node’s next node pointer be NULL. Thus to add a new node we simply “walk through” our linked list until we find the last node. Once we are there we set the last node’s next node pointer to our newly created node. Thus the implementation would look like:

    Code:
    //first check if the linked list is empty
    //if it is make the new node the first node
    if(*bookOrder == NULL) {
         *bookOrder = newNode;
         return;
    }
    //if not find the last node
    walker = *bookOrder;
    while(walker->next != NULL){
         walker = walker->next;
    }
    //set last node's next node to our new node
    walker->next = newNode;
    So now look at our program with this knowledge:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct order {
    	char dir;
    	float price;
    	int size;
    	char instr[5];
    	struct order* next;
    };
    
    void addNode(struct order**);
    
    int main (void){
    
    	struct order *myOrder;
    	myOrder = NULL;
    	
    	for(int i=0; i < 3; i++)
    		addNode(&myOrder);
    
    	for(int j = 0; j < 3; j++){
    		printf("Book %d is $%f\n",j, myOrder->price);
    		myOrder = myOrder->next; 
    	}
    	getchar();
    	return (0);
    }
    void addNode(struct order** bookOrder){
    	//Demonstration only
    	static float price = 1.0f;
    	//-----------------------
    	//Create our walker pointer
    	struct order* walker;
    	
    	//Allocate space for our newNode
    	struct order* newNode = malloc(sizeof(**bookOrder));
    	//if couldn't allocate memory, exit
    	if(!newNode){
    		printf("Couldn't allocate memory\n");
    		return;
    	}
    	//here we would intiliaze our values for our order
    	newNode->price = price++;
    	newNode->next = NULL;
    	//ect.....
    
    	//Check to see if new list
    	if(*bookOrder==NULL){
    		*bookOrder = newNode;
    		return;
    	}
    	//else walk to the end of our list
    	walker = *bookOrder;
    	while(walker->next != NULL){
    		walker = walker->next;
    	}
    	//add our new node
    	walker->next = newNode;
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Ok really, really insightful. I will try to abandon this thread and continue on the other one. I have a few more theoretical questions which I will post there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic program design, passing pointer to a function
    By heras in forum C Programming
    Replies: 14
    Last Post: 04-02-2008, 03:21 AM
  2. Passing arguments between functions
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 05-17-2006, 04:59 PM
  3. passing arguments to functions
    By Micko in forum C Programming
    Replies: 4
    Last Post: 07-18-2004, 10:59 AM
  4. Passing functions as arguments
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-13-2002, 10:02 PM
  5. passing arguments through functions
    By cwd in forum C Programming
    Replies: 2
    Last Post: 09-30-2001, 06:07 PM

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