I'm trying to write a function realloc 3 arrays which are created in main using malloc, but every time I try to run the program, I get an error and program stops working.
In my attempts to debug, I tried to print the arrays after "realloc" and it looks like the realloc was done successfully but after I scan into those new records, when I to to print, I get the error.
CORRECTION 1: Corrected the mistake in scanf line as suggested. Program runs into error as soon as the first new record is input
Any inputs are appreciated!
void addRecord(char** firstName,char** lastName, float* score, int * recordSize)
{
int add,i;
printf("How many records do you want to add? ");
scanf("%d", &add);
firstName = realloc(firstName, (*recordSize+add)*sizeof(char*));
for (i=*recordSize; i<(*recordSize)+add; i++)
firstName[i]= malloc(STRSIZE*sizeof(char));
lastName = realloc(lastName, (*recordSize+add)*sizeof(char*));
for (i=*recordSize; i<(*recordSize)+add; i++)
lastName[i]= malloc(STRSIZE*sizeof(char));
score = realloc(score, (*recordSize+add)*sizeof(float));
printf("Please enter the record to be added: \n");
printf("FirstName LastName Score\n");
for (i=*recordSize; i<*recordSize+add; i++)
scanf("%s %s %f", firstName[i], lastName[i], &score[i]);
*recordSize +=add;
}
%sformat specification forscanf()expects achar *, but you're providingchar **.firstName[i]is achar *already, so you don't need to use the&operator.