-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS_8.c
More file actions
32 lines (26 loc) · 958 Bytes
/
Copy pathDS_8.c
File metadata and controls
32 lines (26 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//Design a structure ‘subject’ to store the details of the subject like subject name and subject code. Using structure pointer allocate memory for the structure dynamically so as to obtain details of ‘n’ subjects using for loop.
#include <stdio.h>
#include <stdlib.h>
struct course
{ //structure declaration
int code; //structure variables
char subject[25];
};
int main() {
struct course *ptr; //structure object
int ns;
printf("NUMBER OF SUBJECTS: ");
scanf("%d", &ns);
//memory allocation for number of subjects
ptr = (struct course *)malloc(ns * sizeof(struct course));
for (int i = 0; i < ns; ++i) {
printf("ENTER SUBJECT AND SUBJECT CODE:\n");
scanf("%s %d", (ptr + i)->subject, &(ptr + i)->code);
}
printf("SUBJECT SUBJECT CODE\n");
for (int i = 0; i < ns; ++i) {
printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->code);
}
free(ptr);
return 0;
}