Couldn't find the errors initially & yet for both sets of code, so need someone else's perspective.

1) Does this function work OK in shifting an array of index entries for a new entry?

I've used printing statements for debugging & what I've found is this:Code:void indexinsert(const int& location, Index dindex[], int& size, const int& trrn, char addkey[]){ int i; dindex = (Index*) realloc (dindex, (size+1) * sizeof(Index)); if (dindex==NULL) exit (1); printf("Range of Index entries shifted: start: %d end: %d\n",location,size); for(i = size; i > location; i--){ dindex[i] = dindex[i-1]; } strcpy(dindex[location].key,addkey); dindex[location].rrn = trrn; size++; return; }

the last 3 entries of index before calling function above:

ie key -> associated data, which is the rrn.

N8500R -> 15

N87BC -> 1

N97890 -> 25

but after call to function, I get this for the end of the array:

N8500R -> 15

N87BC -> 1

N97890 -> 25

-> 0

NOTE: the new key to be inserted is not put in the right place & what I get is that weird last line with rrn of 0.

If this function is incorrect, how should I rewrite it?

2) Does this function perform insert sort correctly? It seemed to be ok when I used this function to sort an index of 26 entries but just today, when the index was greater than 26 entries, something funny happens.

If the additional entries (entries after 26th one) should go before the 1st entry (if sorting 26 entries), then the 1st entry just gets duplicated in the index from the sort, rather than placing the additional entries in the correct place. If the additional entries should go after the 1st entry it seems ok.

ie.:

1st entry is CP-188

if 27th entry is ABC123, result is that you will see CP-188 appear twice in index after sort.

if 27th entry is NBC123, sort works as intended with NBC123 being in sorted list at right place.

If this function is incorrect, how should I rewrite it?Code:int compare(const Index entry1, const Index entry2){ return strcmp(entry1.key, entry2.key); }//this function orignally for use by cstdlib's qsort function, but was abandoned so for use below. void insert_sort(Index data[], int size){ Index temp; int n = 1; int i = 0, k = 0, j = 0; for(i = 1; i < size; ++i){ temp = data[i]; j = i; for(k = i - 1; k >= 0; k--){ if(compare(data[k],temp) > 0){ data[j--] = data[k]; }else{ data[j] = temp; n = NULL; break; } } if(n != NULL) data[j] = temp; } return; }