Struct Array

micmac700's Avatar
Newbie Member
in my .c file i have a struct atop of the program defined as follows:
Code:
#define MAX 10
int curtab;

static struct tab {
	int count;
	int use;
} tab[MAX];
with the initial function following it like so:
Code:
int tab_create(int init_count)
{
       int i;
       for(i=0; i < MAX; i++)
	{
		if(tab[i].use != 1)
		{
			tab[i].use = 1; /* true */
			tab[i].count = init_count;
			curtab = i;
			i = MAX; /* break */
		}
	}
       return (curtab);
}
but I find I receive a Seg-Fault error at
Code:
if(tab[i].use != 1)
Why does this occur?
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
I see no reason for it to seg fault. Perhaps there's some undefined operation in "main", which you don't show?
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
I guess you are passing tab_create a value greater than some upper limit value
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Still, the array bounds are constrained by MAX in the loop. If tab_create is some goshawful unitialized number, it should still fit in an int, or the compiler would complain. Do you have a good compiler with all warnings and errors turned on? You really, at this point, should show "main", since all the code that you show is functional on my system, with my own "main", of course.

You will probably find MAX defined elsewhere in the standard library (a thangy to return the max of two values), but your #define should override.