Thanks for all the insight, i'm still not sure if I can accomplish what I want by but here's some code. Basically, two seperate table variables, im not sure if I set it up right or if im modifieing variables accordingly.
Code:
int main()
{
table *t1;
table *t2;
t1 = new(4, 1);
t2 = new(2, 3);
delete( t1 );
delete( t2 );
typedef struct table
{
/*Innitialize all values incase we use accidentally*/
int *array = NULL; /*Default for pointers (points to nothing)*/
int count = 0; /*Default for counter (empty array)*/
int size;
};
/*Constructor - Innitialize variables and dynamically create integer array*/
table *new( int length, int fill )
{
int i;
table mytable;
/*Innitialize variables*/
mytable.size = length; /*Size of the array (used for rfl function call)*/
mytable.rem = (length*fill); /*Remaining count of rfl table*/
mytable->array = (int*)malloc(length * sizeof (int)); /*Allocate memroy*/
if (mytable->array == NULL) /*Check progress of memory allocation*/
printf("ERROR! Could not allocate memory for table\n");
for(i=0;i<length;i++)
mytable->array[i] = fill;
return mytable; /*All is well*/
}
/*Remove the specified index, adjusting the count accordingly*/
int remove( table *mytable, int index )
{
mytable.count -= table->array[index];
mytable->array[index] = 0;
}
/*Increment existing values in array, increasing the count accordingly*/
int increment( table *mytable )
{
int i;
for(i=0;i<(mytable.size);i++)
{
if(mytable->array[i])
mytable->array[i] += 1;
}
}
/*Destructor - Free allocated memory and set pointer to null*/
void delete( table *mytable )
{
free(mytable->array); /*Free allocated memory*/
mytable->array = NULL; /*Assign pointer to null*/
mytable.count = 0;
mytable.size = -1;
if( mytable->array != NULL )
printf("ERROR! Could not de-allocate memory for table\n");
}
}
If I want some other function to modify the contents of the structs "count" variable, can I just pass it like so: "&(mytable.count)", than have the function im passing it to modify it?
Also, can I just use integers in the struct than modify them at will, or do I need them to be pointers and modify them another way?