Wont make a new thread- will ask here instead. I have a new task. This time about structures and accesing them. So my task was to write a small database of students (program asks how many students will be entered, then user inputs all the info needed, and last- program shows what was entered). I wrote a basic program...
Code:
#include <stdio.h>
#include <stdlib.h>
struct data {
char name[20];
char surname[20];
int course;
char group[10];
};
int my_count();
int main(){
struct data *ptr;
int k,i;
char name;
k = my_count();
ptr = (struct data*)malloc(k*sizeof(struct data));
for (i=0; i<k; ++i)
{
printf("Enter students name:\n");
scanf("%s", &(ptr+i)->name);
printf("Enter students surname:\n");
scanf("%s", &(ptr+i)->surname);
printf("Enter students course:\n");
scanf("%d", &(ptr+i)->course);
printf("Enter students group:\n");
scanf("%s", &(ptr+i)->group);
}
for (i=0; i<k; ++i)
{
printf("\n\n...Info about student: %d...\n", i+1);
printf("Name-->\t\t |%s|\n",(ptr+i)->name);
printf("Surname-->\t |%s|\n",(ptr+i)->surname);
printf("Course-->\t |%d|\n",(ptr+i)->course);
printf("Group-->\t |%s|\n",(ptr+i)->group);
}
free(ptr);
return 0;
}
int my_count(){
static int k;
printf("How much students do you want to enter?\n");
scanf("%d",&k);
return k;
}
However, I need to make user-defiened functions for both- information input and ouput. Tricky part here is that I can use only one input function, to get everything (name,surname,course,group).
Now I am a bit confused. I tried to use something like "ptr->x" to make function, where "x" should vary, but it isnt correct