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));
    	
	p=records;

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

Many thanks.

you can delete pointer p since its not needed

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

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

Or you could use a linked list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.