-
Notifications
You must be signed in to change notification settings - Fork 26
/
linkedNodeStudentData.c
48 lines (40 loc) · 1.43 KB
/
linkedNodeStudentData.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
/*
You would like to store student data (for each student, their name and age) in a linked list of students.
Your first task is to write a function createStudent() that takes as inputs a string (namely a student's name)
and an integer (their age) and that returns a pointer to a struct student which stores this information.
Your function should dynamically allocate the memory required for this struct student and then write the
student's name and age into this newly allocated memory.
*/
struct student {
char name[50];
int age;
struct student *next;
};
struct student *createStudent(char studentName[], int studentAge);
// Write other function prototypes here (if any)
int main(void) {
struct student *studptr;
int myAge;
char myName[50];
scanf("%s %d", myName, &myAge);
studptr = createStudent(myName, myAge);
printf("New student created: %s is %d years old.\n", studptr->name, studptr->age);
free(studptr);
return 0;
}
// Write your createStudent function here (and any other functions you see fit)
struct student * createStudent(char studentName[50], int studentAge) {
struct student * ptr;
int i;
ptr = (struct student *) malloc(sizeof(struct student));
//ptr->name = studentName;
ptr->age = studentAge;
i = 0;
while (studentName[i] != '\0') {
ptr->name[i] = studentName[i];
i++;
}
return ptr;
}