Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Struct Array (http://www.go4expert.com/forums/struct-array-t2232/)

micmac700 13Dec2006 22:03

Struct Array
 
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?

DaWei 13Dec2006 22:22

Re: Struct Array
 
I see no reason for it to seg fault. Perhaps there's some undefined operation in "main", which you don't show?

shabbir 14Dec2006 06:19

Re: Struct Array
 
I guess you are passing tab_create a value greater than some upper limit value

DaWei 14Dec2006 08:57

Re: Struct Array
 
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.


All times are GMT +5.5. The time now is 10:17.