Advertisement
rafikamal

Pointer to Structure

Jan 31st, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define SIZE 20
  5. #define MAX 100
  6.  
  7. void input(struct StudentInfo *student);
  8. void print(struct StudentInfo *student);
  9.  
  10. struct StudentInfo
  11. {
  12.         char name[SIZE];
  13.         int n;
  14. };
  15.  
  16. int main()
  17. {
  18.     struct StudentInfo *s;
  19.     int n, i, max, maxPosition;
  20.    
  21.     printf("How many students? ");
  22.     scanf("%d", &n);
  23.     s = (struct StudentInfo *) malloc(n * sizeof (struct StudentInfo));
  24.     getchar();
  25.    
  26.     for(i = 0; i < n; i++)
  27.     {
  28.           input(&s[i]);
  29.     }
  30.    
  31.     for(i = 0; i < n; i++)
  32.     {
  33.           print(&s[i]);
  34.     }
  35.    
  36.     free(s);
  37.    
  38.     return 0;
  39. }
  40.  
  41. void input(struct StudentInfo *student)
  42. {
  43.     printf("Enter the name of stduent: ");
  44.     gets(student->name);
  45.     printf("Enter the number of student: ");
  46.     scanf("%d", &student->n);
  47.     getchar();
  48. }
  49.  
  50. void print(struct StudentInfo *student)
  51. {
  52.     printf("%s = %d\n", student->name, student->n);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement