I have a static array and in a function I create a new struct in a loop and assign it to each index in an array. In the function I can see the values but in a different function I see junk for the array values. Do I have to use malloc for something like this?
struct file_types
{
char * typename;
char * MIMEtype;
};
static struct file_types *file_type_table; //Table of parameters
static int file_type_table_num=0;
int add_to_filetype_table(char *param, int param_len, char *value, int val_len, char* value2)
{ if ((param == NULL) || (value==NULL) || (value2 == NULL))
return 0;
if ((strcmp(param,"type") != 0) || (strcmp(value,"") == 0) || (strcmp(value2,"") == 0))
return 0;
if (file_type_table==NULL)
file_type_table = emalloc(sizeof(struct file_types));
else
file_type_table = erealloc(file_type_table, (file_type_table_num*sizeof(struct file_types)+ sizeof(struct file_types)));
file_type_table_num += 1;
int index = file_type_table_num -1;
struct file_types new_struct;
new_struct.typename = value;
new_struct.MIMEtype = value2;
file_type_table[index] = new_struct;
return 1;
}
Problem is in accessing the structs here:
char* get_table_value(char * key)
{ logg("In get_table_value");
int i;
char* value;
for (i=0;i<file_type_table_num;i++)
{
if (strcmp(((file_type_table)[i]).typename, key) == 0)
{
return (file_type_table[i]).MIMEtype;
}
}
return value;
}