Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Struct Array

Status
Not open for further replies.

pmccafferty

Programmer
Aug 21, 2006
3
US
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?
 
You're defining a type named tab as well as a variable named tab. I've never tried that, but I would have thought the compiler would complain about that?
Try this and see if it helps:
Code:
typedef struct {
    int count;
    int use;
} tab_t;

static tab_t tab[MAX];
 
A C compiler won't get confused as the structure will be called struct tab while the variable will be called tab. Typical example is the structure called stat and the function called stat, both in stdlib.

Can't really see anything wrong with the program. It could be caused by the routine called before tab_create is called. The point at which it falls over could be just a side effect.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top