# using an array of structs in C



## lilLB (Mar 19, 2006)

ok so we got a problem to read data from a file and it represents restaurants and some info like food type, avg meal price, and a rating 
i believe my algorithm is correct but i'm gettin tons of compilation errors and i don't understand why

here's the first major issue i have:

```
//Function Declarations
int getSize(void);
void readFile(int size, restaurant *list);
int menu (void);
int search (char *looking, restaurant *list, int num);
void sortmenu (void);
void display(int size, restaurant *list);
void alphabetize(int num, restaurant *list);
void spoonsort(int num, restaurant *list);

//Main Function:
int main(void)
{
	typedef struct{
		 char *name;
		 char *type;
		double meal;
		 int spoons;
	} restaurant;
	//find the size of list from file
	int size=getSize();
	//define a list of restaurants
	restaurant restList[size];
```
ok so i want my functions to receive an array of restaurants 
but the compiler is not recognizing restaurant as a type
i'm sure i have many other errors but if anyone can give me a clue on how to allow my functions to recieve a typedef struct?


----------



## mt2002 (Sep 14, 2005)

restaurant must be declared before it is first used. Move your restaurant struct above your function declarations.


----------



## lotuseclat79 (Sep 12, 2003)

I would also redesign the struct to have the largest element first in the order with descending sizes of defined elements in the structure and the pointers at the end - i.e. meaning {meal, spoons, name, type} per your definitions.

-- Tom


----------



## lilLB (Mar 19, 2006)

Thanks mt2002, I thought of that but I think our teacher showed us to put it in the main, so I wasn't sure.

@ lotuseclat79: Does that just make the program run more efficiently?

My next major issue is file reading 

I took this code from the group project we just did some kid tinkered with the original code to get it to work so I'm not entirely sure what he's doing but it worked for that project and I was trying to adapt it to this program but its not reading in anything.

I have 2 file reading functions, one gets size, the other gets all the elements.


```
int getSize(void){
	//Reads the size of the list from the first line of the file  

      	int size; // size of the array which will be read from the file
        FILE *infilep;  // Input file pointer

        infilep = fopen("restaurant.dat","r"); //file where all data is stored
		fscanf(infilep, "%d", &size);
		fclose(infilep);
	
   	
	return size;
	}
```
and

```
void readFile(int size, restaurant *list) {

	int x; // index variable
	FILE *inp; //Input file pointer
	
	char *n, *t;
	double avgml;
	int spn;

	/** Open the file to read data from it **/
	inp = fopen("restaurant.dat", "r"); 
		fseek(inp,3,SEEK_CUR);
	for (x = 0; x < size; x++){ //for each item 
		fscanf(inp, "%s%s%lf%d\n", &n, &t, &avgml, &spn); 
		fseek(inp,0,SEEK_CUR);

		//store in list
		list[x].name=n;
		list[x].type=t;
		list[x].meal=avgml;
		list[x].spoons=spn;
	}

	fclose(inp); //close the file*/
			
}
```
I'm not very good at file reading so if you can help point me in the right direction I'd appreciate it.
Thanks.


----------



## mt2002 (Sep 14, 2005)

> Thanks mt2002, I thought of that but I think our teacher showed us to put it in the main, so I wasn't sure.


You can put the structure inside of main, but it would have limited scope. However, you might be able to forward declare restaurant to get rid of the error:


```
//Function Declarations
struct restaurant;

int getSize(void);
void readFile(int size, restaurant *list);
int menu (void);
int search (char *looking, restaurant *list, int num);
void sortmenu (void);
void display(int size, restaurant *list);
void alphabetize(int num, restaurant *list);
void spoonsort(int num, restaurant *list);
```


----------



## lilLB (Mar 19, 2006)

thanks for the help
just stumped on file reading any good guides out there?


----------



## mt2002 (Sep 14, 2005)

Clicky

If you dont understand something or need help with anything, feel free to ask


----------



## lilLB (Mar 19, 2006)

thanks mt2002, you're a life saver ill give that a read later, right now i have to go to work


----------



## lotuseclat79 (Sep 12, 2003)

lilLB said:


> Thanks mt2002, I thought of that but I think our teacher showed us to put it in the main, so I wasn't sure.
> 
> @ lotuseclat79: Does that just make the program run more efficiently?
> 
> ...


Hi lilLB,

It helps the compiler to allocate memory for the data structure more efficiently when you compile your code with the compiler. Learn to use a debugger to execute your program so you can step through the program statement by statement and check to see the value of variables as you program executes. Its fun! Ask how to do it. What compiler are you using on what OS environment?

-- Tom


----------



## lilLB (Mar 19, 2006)

lotuseclat79: I am using Fedora and compiling with gcc by command line
not sure how to debug, these are school computers so i can't download anything applications to them. However if you have any suggestions for windows xp which i have at home I could possibly use that to work on some programs.


----------



## lotuseclat79 (Sep 12, 2003)

How to debug at school:
$ gdb program
gdb> step

Read doc:
$ man gdb

-- Tom


----------

