-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice1.c
31 lines (29 loc) · 992 Bytes
/
practice1.c
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
//dynamic memory allocation sanga use to huna ko lagi
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Student
{
char name[50];
int roll;
char hobbies[50];
};
int main () {
int n;
printf("n=");
scanf("%d", &n);
struct Student *ptr;
FILE *fptr;
fptr = fopen("Student.txt", "w");
ptr = (struct Student *)malloc(n * sizeof(struct Student));
for (int i=0; i<n; ++i){
printf("Enter name, roll, hobbies of %d student : ", i+1);
scanf("%s %d %s", (ptr + i)->name, &(ptr + i)->roll, (ptr + i)->hobbies);
fprintf(fptr, "%s %d %s", ptr[i].name, ptr[i].roll, ptr[i].hobbies);
}fprintf(fptr, "\n");
for (int i=0; i<n; ++i){
printf("\n\nDetails of %d student : ", i+1);
printf("\nName=%s \nRoll=%d \nHobbies=%s", (ptr + i)->name, (ptr + i)->roll, (ptr + i)->hobbies);
}
return 0;
}