-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.c
More file actions
53 lines (43 loc) · 1.16 KB
/
Copy pathexample.c
File metadata and controls
53 lines (43 loc) · 1.16 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure definition
typedef struct {
char name[50];
int id;
float score;
} Student;
// Function prototypes
void printStudent(Student* student);
Student* createStudent(const char* name, int id, float score);
// Global variable example
const int MAX_STUDENTS = 100;
int main() {
// Array allocation
Student* students[2];
// Create students
students[0] = createStudent("John Doe", 1, 85.5);
students[1] = createStudent("Jane Smith", 2, 92.0);
// Print students
for (int i = 0; i < 2; i++) {
printStudent(students[i]);
}
// Memory cleanup
for (int i = 0; i < 2; i++) {
free(students[i]);
}
return 0;
}
Student* createStudent(const char* name, int id, float score) {
Student* student = (Student*)malloc(sizeof(Student));
strncpy(student->name, name, 49);
student->name[49] = '\0';
student->id = id;
student->score = score;
return student;
}
void printStudent(Student* student) {
printf("Student ID: %d\n", student->id);
printf("Name: %s\n", student->name);
printf("Score: %.2f\n\n", student->score);
}