I am trying to write a function that will add a set of data (first name, last name, score) into 3 different dynamic arrays (one 2d char array for the first name, one 2d char array for the the last name, and a float array for the score). Here is my code so far:
void add(char **firstname, char **lastname,char *newfirst,char *newlast,float newscore, float *score, int *num)
{
realloc(firstname, sizeof(newfirst));
realloc(lastname, sizeof(newlast));
realloc(score, sizeof(float)*sizeof(newscore));
*num = *num + 1;
firstname[*num] = newfirst;
lastname[*num] = newlast;
score[*num] = newscore;
}
I know that I need to reallocate memory in order to add anything to the arrays. I am trying to add 1 to num so that whenever I run the other functions (such as printing and sorting, etc.) it will run through the other loops that are in those functions the appropriate amount of times. All other aspects of the program work, it just crashes when I run it through this function. Am I allocating the memory properly, and what could be causing the crash?
Here is the case in main, in case anyone needs to look at it:
case 2: printf("Enter record of student to add: \n");
printf("Enter the first name: \n");
scanf_s("%s", addfirst, 21);
printf("Enter the last name: \n");
scanf_s("%s", addlast, 21);
printf("Enter the score:\n");
scanf_s("%f", addscore);
add(firstname, lastname, addfirst, addlast, addscore, score, num);
break;
I am 99% sure I have properly initialized all of the variables used for this function.