Start New Discussion within our Software Development Community

Hi all. I wrote a small sequence to read data into a dynamic array of structures, which seems to work just fine so far as I can tell. Unfortunately a friend with somewhat more experience than I do pronounced it 'hairy,' but wasn't good enough to explain why. I'm simply inviting criticism, and hopefully an explanation of what, if anything, is wrong with the following extract:

typedef struct {
    	char fname[25];
    	char sname[25];
    	char sex[2];
    	int age;
    	int refno;
    	int hours;
    	int minutes;
    } entrant;

	FILE *fp;
	int totalcount = 0;
	int curmax = 500;
	int memincrement = 100;
        entrant *records = NULL;        
	entrant *p = NULL;

	fp = fopen("rolodex.bin", "rb");

	records = (entrant *) realloc (records,(curmax) * sizeof(entrant));

        if (fp) {
            while (p && fread(p,sizeof(entrant),1,fp)) {                
		if (totalcount+1 > curmax) { 
                    records = (entrant *) realloc (records,(curmax) * sizeof(entrant));

Many thanks.

you can delete pointer p since its not needed

while (fread(&records[totalcount],sizeof(entrant),1,fp)) {                
		if (totalcount+1 == curmax) { 
                    records = realloc (records,curmax * sizeof(entrant));

In C language, malloc() doesn't need a typecase.

Or you could use a linked list.

This article has been dead for over six months. Start a new discussion instead.