Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Function Prototypes */
void getClassData(int *);
void getStudentData(char *n[], int s[], int class);
int main(void)
{
/* Variables declared */
int i, class1;
char **n1;
int *s1;
getClassData(&class1);
n1 = malloc(sizeof(char *) * class1);
s1 = malloc(sizeof(int) * class1);
// Always make sure allocations succeed
if(!n1 || !s1)
{
fprintf(stderr, "Memory allocation failure.\n");
return 1;
}
getStudentData(n1, s1, class1);
printf("-Class 1-\n");
for(i = 0; i < class1; i++)
{
printf("n1[%d] %s s1[%d] %d\n", i, n1[i], i, s1[i]);
free(n1[i]);
}
// Always free allocated memory
free(n1);
free(s1);
return 0;
}
void getClassData(int *class1)
{
printf("Number of students in class 1? ");
scanf("%d", class1); // class1 is already a pointer. No need for &*
}
void getStudentData(char *n[], int s[], int class)
{
char name[10];
int j, score;
for(j = 0; j < class; j++)
{
printf("Enter student's name and score? ");
scanf("%s %d", name, &score);
n[j] = malloc(strlen(name) + 1);
strcpy(n[j], name);
s[j] = score;
}
}