-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructgetinfo.c
62 lines (38 loc) · 1.14 KB
/
structgetinfo.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
49
50
51
52
53
54
55
56
57
58
59
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct name{
char* firstname;
char* lastname;
int letters;
};
void getinfo(struct name* pst){
char temp[81];
printf("Please input first name: \n");
fgets(temp,80, stdin); /* Reading from stdin, which is the keyboard */
pst->firstname = (char *)malloc(strlen(temp + 1)); /* Allocating memory */
if(pst->firstname == NULL)
exit(1);
strcpy(pst->firstname,temp); /* Copy */
printf("Please input last name: \n");
fgets(temp,80, stdin); /* Reading from stdin, which is the keyboard */
pst->lastname = (char *)malloc(strlen(temp + 1)); /* Allocating memory */
if(pst->lastname == NULL)
exit(1);
strcpy(pst->lastname,temp);
}
void computelen(struct name *pst){ /* Function to compute name length */
pst->letters = strlen(pst->firstname) + strlen(pst->lastname);
}
void cleanup(struct name *pst){ /* free memory with one function */
free(pst->firstname);
free(pst->lastname);
}
int main(){
struct name x;
getinfo(&x);
computelen(&x);
printf("First name: %sLast name: %sName length: %d\n", x.firstname, x.lastname, x.letters);
cleanup(&x);
return 0;
}