Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 373d27c

Browse files
Merge pull request #263 from thesantoso/master
add an example of malloc
2 parents 40bda25 + 0eec857 commit 373d27c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

‎DynamicMemoryAllocation‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
struct course {
4+
int marks;
5+
char subject[30];
6+
};
7+
8+
int main() {
9+
struct course *ptr;
10+
int noOfRecords;
11+
printf("Enter the number of records: ");
12+
scanf("%d", &noOfRecords);
13+
14+
// Memory allocation for noOfRecords structures
15+
ptr = (struct course *)malloc(noOfRecords * sizeof(struct course));
16+
for (int i = 0; i < noOfRecords; ++i) {
17+
printf("Enter subject and marks:\n");
18+
scanf("%s %d", (ptr + i)->subject, &(ptr + i)->marks);
19+
}
20+
21+
printf("Displaying Information:\n");
22+
for (int i = 0; i < noOfRecords; ++i) {
23+
printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks);
24+
}
25+
26+
free(ptr);
27+
28+
return 0;
29+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /