-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.c
44 lines (32 loc) · 1.05 KB
/
struct.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
#include <stdio.h>
struct Book // alternatively can use typedef struct { ...members...} Book;
{
// member variables of the structure
int id;
char *title;
char *author;
char *subject;
float price;
};
// note use of pointers above
// alternatively can say char title, and then use strcpy() later
// this would require an extra include directive: #include <string.h>
int main(void)
{
struct Book abook; // if we use typedef, then: Book abook;
abook.id = 1;
abook.title = "Programming Is Fun";
abook.author = "J J Smith";
abook.subject = "IT";
abook.price = 22.99;
// if we were not using pointers, we would use strcpy()
// e.g. strcpy( abook.title, "Programming Is Fun" );
printf("Book Id: %d\n", abook.id);
printf("Title: %s\n", abook.title);
printf("Author: %s\n", abook.author);
printf("Subject: %s\n", abook.subject);
printf("Price: %.2f\n", abook.price);
return 0;
}
// ref:
// http://www.learn-c.org/en/Structures