> Finally if you noticed, I had to use & even in printf statements because of the structure of pointers that I used.
Which basically means you got something else wrong as well.
So rather than fix the original mistake, you make a mess of it somewhere else, where there is less error checking.
Sooner or later, printf/scanf format abuse will catch up to you, and it can be hard to track down exactly what the problem is.
Get a better compiler, or wind up the diagnostics if you're using gcc.
This is what I get with your code.
Code:
$ gcc -W -Wall -ansi -pedantic -O2 foo.c
foo.c: In function ‘main’:
foo.c:30: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[99u]’
foo.c:32: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[99u]’
foo.c:34: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[99u]’
foo.c:47: warning: format ‘%10s’ expects type ‘char *’, but argument 2 has type ‘char (*)[99u]’
foo.c:47: warning: format ‘%4s’ expects type ‘char *’, but argument 3 has type ‘char (*)[99u]’
foo.c:47: warning: format ‘%7s’ expects type ‘char *’, but argument 4 has type ‘char (*)[99u]’
foo.c:53: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[99u]’
foo.c:54: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘char *’
foo.c:6: warning: unused variable ‘k’
foo.c:8: warning: ‘sname’ may be used uninitialized in this function
foo.c:8: warning: ‘name’ may be used uninitialized in this function
foo.c:8: warning: ‘id’ may be used uninitialized in this function
Try this
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ch, num = 0, i, j;
float av = 0;
char (*id)[100] = NULL, (*name)[100] = NULL, (*sname)[100] = NULL;
/*int grade[100]; */
/*!! I think you wanted num students of this as well */
int *grade = NULL;
printf("Menu:\n"
"1. Student registration (Student id, name, surname).\n"
"2. Course list (including student id, name, surname).\n"
"3. Final results entrance of each student.\n"
"4. Average of the final.\n"
"5. Exit\n");
do {
printf("enter your choice:");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("how many students: \n");
scanf("%d", &num);
id = malloc(sizeof(*id) * num);
name = malloc(sizeof(*name) * num);
sname = malloc(sizeof(*sname) * num);
grade = malloc(sizeof(*grade) * num);
for (i = 0; i < num; i++) {
printf("please enter student ID: ");
scanf("%s", id[i]);
printf("please enter student name: ");
scanf("%s", name[i]);
printf("please enter student surname: ");
scanf("%s", sname[i]);
}
break;
case 2:
printf("\n\n");
printf(" Course List:\n\n");
printf("student id\tname\tsurname\n");
for (i = 0; i < (2 * '\t' + 14); i++) {
printf("-");
}
printf("\n");
for (j = 0; j < num; j++) {
printf("%10s\t%4s\t%7s\n", id[j], name[j], sname[j]);
}
break;
case 3:
for (j = 0; j < num; j++) {
printf("%s:", id[j]);
scanf("%d", &grade[j]);
}
break;
case 4:
for (j = 0; j < num; j++) {
av += grade[j];
}
av = av / num;
printf("average of the final is: %.2f\n", av);
break;
case 5:
/*exit(0); */
break;
}
} while (ch != 5);
free(id);
free(name);
free(sname);
free(grade);
getchar();
return 0;
}