I was first thinking of that kind of strategy, so interested in what you come up with.
It is a tricky problem, only because you do have to always know the lengths of all involved, and remember to free() all allocated memory. It is particularly tricky, since realloc() is apparently not available to us in LiteC. Though it may not be "perfect" here is one solution I came up with:
Code:
#include <acknex.h>
#include <default.c>
var gVal;
int *array1;
int *array2;
int *array3;
PANEL* selection1 = {
pos_x = 0; pos_y = 0;
digits(10,10,1,*,1,gVal);
flags = VISIBLE;
}
///////////////////////////////////////////////////////////
// Allocate for an int array with capacity. Sets a sentinal
// value of -999 so we can later test for the size of the array.
// (see length() below)
///////////////////////////////////////////////////////////
int* createIntArray(int* a, int capacity)
{
a = malloc(sizeof(int)*(capacity+1));
a[capacity] = -999;
return a;
}
///////////////////////////////////////////////////////////
// Counts number of array positions in array. Tests agains
// -999 to determine the end of the array.
///////////////////////////////////////////////////////////
int length(int *a)
{
int i = 0;
while(a[i] != -999)
{
++i;
}
return i;
}
///////////////////////////////////////////////////////////
// Appends contents of a2 to a1, resulting in a3. a1 and a2
// are not altered.
//
// NOTE: If you were to attempt to make a1 the new allocated
// address, then you would no longer be able to free the
// memory originally allocated to it.
///////////////////////////////////////////////////////////
int* appendIntArray(int* a1, int* a2, int* a3)
{
int i,j;
int k = 0;
int l1 = length(a1);
int l2 = length(a2);
a3 = createIntArray(a3, (l1+l2) );
for(i = 0;i < l1;i++)
{
a3[i] = a1[i];
}
for(j = i;j < l1+l2;j++)
{
a3[j] = a2[k];
k++;
}
return a3;
}
function main()
{
screen_color.blue = 150;
array1 = createIntArray(array1, 3);
array1[0] = 1;
array1[1] = 1;
array1[2] = 3;
array2 = createIntArray(array2, 3);
array2[0] = 4;
array2[1] = 5;
array2[2] = 6;
array3 = appendIntArray(array1, array2, array3);
gVal = array3[5];
// !!! make sure to clear all allocated memory !!!
free(array1);
free(array2);
free(array3);
}
I use a sentinal value of -999 to mark the end of my int arrays, and my own length() function that uses this sentinal to count the valid storage locations.
EDIT: Just found out that realloc() could be made available by using the DLL it is contained in...
http://manual.conitec.net/litec_c.htm This would allow for better array appending.