Hi all!

I am having trouble accessing dynamic information. I have used the malloc command to allocate the necessary memory for my struct. However, when I leave that function and enter a display function it says there is a problem accessing the information in the struct.

I just had a project with pointers and would like to continue practicing with them to hopefully get a handle on them. :) I'm guessing there is a scope issue going on but I'm not sure how to bypass this issue. The following is the code I have written so far.

Inventory.h

/*
Author: Dmytri Eck
Date: October 29, 2011
*/
#ifndef _Inventory
#define _Inventory

/* Error message */
#define ERROR_INVALID_SEARCH_OPTION "Invalid search option.\n"
#define ERROR_NO_BOOK_FOUND "No book fuond matching the search criterion.\n"

#define MAXIMUM_TITLE 20
#define MAXIMUM_AUTHOR 20
#define MAXIMUM_BOOKS 10

/*Structure for book information */
typedef struct Information
{
	char subject [MAXIMUM_TITLE];
	char author [MAXIMUM_AUTHOR];
	int edition;
	int year;
	float price;
} Book;

static Book *Information;

/*Structure for book search result */
typedef struct Search
{
	Book booksFound[MAXIMUM_BOOKS];
	int searchMatches;
} BookSearchResult;

/*Interface functions for this application

Function to read the book information
It returns the structure of type book*/

Book getBookInformation(int inventory);

/*Function to earch books in the inventory
It requires an array of books (inventory) which is to be searched
and the size of this array along with the search criterion.
Following are the valid options and corresponding search methods:
option = 0; Search for books published in the year value
option = 1; Search for books with price less than or equal to value
It returns the search result.*/

BookSearchResult searchBook(Book *, int size, int option, float value);

/*Function to display information of all the books in the array
It has two arguments, an array of type book and size of this array.*/

void displayResult(int);

#endif

Main.c

#include <stdio.h>
#include <stdlib.h>
#include "Inventory.h"

int main(void)
{
	//Variable Declaration
	int inventory = 0;		//Number of books in database
	int search;					//Search selection
	int year;					//Search option (line 45)
	float price;				//Upper price limit
	
	//Debugging purposes ONLY	
	int counter = 0;

	printf("How many books are in the database?\n> ");
	scanf("%d", &inventory);
	
	
	getBookInformation(inventory); //Get book information
	displayResult(inventory);
}

Inventory.c

#include <stdio.h>
#include <stdlib.h>
#include "Inventory.h"
Book getBookInformation(int book_number)
{
	int counter = 0;		//Counts the books in the database
	Book *Information = NULL;

	//Allcoate memory and Validate for Information
	Information = (Book *) malloc(book_number * sizeof(Information)); 
	
	//Validating inventory value
	if(book_number > MAXIMUM_BOOKS)
	{	
		fprintf(stderr, "You have exceeded inventory limit (10).\n");
		exit(0);
	}
	else if(book_number <= 0)
	{
		fprintf(stderr, "ERROR: Invalid number of books.");
		exit(0);
	}

	if (Information == NULL)
	{
		fprintf(stderr, "Not enough memory available. Program terminating.\n");
		exit(0);
	}
	else
		for (counter = 0; counter < book_number; counter++)
		{
			printf("The following information is required to place the book in the database.\n");
			printf("Book Subject, Author, Year, Edition, and Price\n");

			//Book information prompt
			printf("1. What is the title of the book?\n> ");
			scanf("%s", &Information[counter].subject);
	
			printf("\n\n2. Who is the author?\n> ");
			scanf("%s", &Information[counter].author);
		
			printf("\n\n3. What year was the book published?\n> ");
			scanf("%d", &Information[counter].year);
	
			printf("\n\n4. What Edition is the book?\n> ");
			scanf("%d", &Information[counter].edition);

			printf("\n\n5. How much does the book cost?\n> ");
			scanf("%f", &Information[counter].price);
		}
}
void displayResult(int totalMatch)
{
	int counter = 0;
	printf("This are the books that are in the database.\n");
	
	for (counter = 0; counter < totalMatch; counter++)
	{
		printf("> %s\n", (*Information).subject);
		printf("> %s\n", (*Information).author);
		printf("> %d\n", (*Information).edition);
		printf("> %d\n", (*Information).year);
		printf("> %f\n", (*Information).price);
	}
}

Might there be a problem with the location that I have placed my pointer? When I place the pointer declaration in the header file I get an error when I try to change it in inventory.c

Any help would be greatly appreciated. Thanks!

It's a very , very messy code.Here are few things that I saw, that could be potentially causing the problem.
1.You have named many things "Information".First the struct for book data

/*Structure for book information */
typedef struct Information
{
char subject [MAXIMUM_TITLE];
char author [MAXIMUM_AUTHOR];
int edition;
int year;
float price;
} Book;

and then this

static Book *Information;

I don't know what's that for.And finally a pointer to Book to use with malloc.

Information = (Book *) malloc(book_number * sizeof(Information));

2.you are allocating space dynamically for a pointer variable.

Information = (Book *) malloc(book_number * sizeof(Information));

should be replaced with this:

Information = (Book *) malloc(book_number * sizeof(Book));

I guess, you have to replace all the 'Information's with 'Book's in the code that follows.
3.At least the code should be syntactically correct, then it will be easy to find the bugs that aren't caught by the compiler. for e.g. see this declaration

Book getBookInformation(int inventory);//if returns nothing, return 'void'

But see the definition of the function 'getBookInformation', it returns nothing.
I hope above things will help you fix your code,and in addition don't ignore compiler warning messages, they could be very helpful while debugging your code.
P.S. may be compiling them as a single source at first, may be less tiresome and less time consuming and then you can distribute them in multiple files afterwards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.