Advertisement
xeritt

Ввод/вывод массива указателей на структуру

Nov 10th, 2016
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. /*
  2.   gcc -std=c99 -Wall -o "input" "input.c"
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. struct stud{
  7.     char name[50];
  8.     char fname[50];
  9.     int balls;
  10. };
  11. typedef struct stud st;
  12.  
  13. void readStudent(st* student){
  14.     printf("Введите Имя:");
  15.     scanf("%s", student->name);
  16.     printf("Введите Фамилию:");
  17.     scanf("%s", student->fname);
  18.     printf("Введите баллы:");
  19.     scanf("%d", &student->balls);
  20. }
  21. void readStudents(st** student, int count){
  22.     for (int i = 0; i < count ; i++){
  23.         student[i] = (st*) malloc (sizeof(st));
  24.         readStudent(student[i]);
  25.     }
  26. }
  27.  
  28. void printStudents(st** student, int count){
  29.     for (int i = 0; i < count ; i++){
  30.         printf("Имя:");
  31.         printf("%s\n", student[i]->name);
  32.         printf("Фамилия:");
  33.         printf("%s\n", student[i]->fname);
  34.         printf("баллы:");
  35.         printf("%d\n", student[i]->balls);
  36.     }
  37. }
  38.  
  39. int main(int argc, char **argv){
  40.     int count = 3;
  41.     printf("Введите кол-во:");
  42.     scanf("%d", &count);
  43.     st** mas = (st**)malloc(sizeof(st**)*count);
  44.     readStudents(mas, count);
  45.     printStudents(mas, count);
  46.     for (int i = 0; i < count; i++)
  47.     {
  48.         free(mas[i]);
  49.     }
  50.     free(mas);
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement