A selection of topics on IT and its application to finance.
Send me your comments, questions or suggestions by clicking here
Implementing dynamic arrays in C
Dynamic arrays are useful in situations where you want to read data into memory but you don't know in advance how big the data set will be. The usual way to do this kind of thing is to use linked lists but I find that dynamic arrays, although similar, are much easier to code. Below is a very simple example of reading in a code and description from an external file into memory.
static struct look *lookup = 0;
int num_codes = 0;
fn = fopen("codes.dat","r");
/* Read 'em in */
if((num_codes= load_lookup()) <= 0)
fprintf(stderr,"Error loading lookup data\n");
/* print 'em out */
static int code_count=0;
int ret_val = 0;
ret_val = fscanf(fn,"%s% %69c\n",lookup_code,lookup_desc);
if(ret_val != 2)
if((lookup=(look *)realloc(lookup,code_count*sizeof(struct look))) ==NULL)