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;
}