-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEX64.c
134 lines (105 loc) · 3.13 KB
/
EX64.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
Project 64
Struct in C - Initiate objects by Reference
Here we will need to create a method that initializes an object that guarantees
an initial condition of date object since the compiler only guarantees
allocation of the address. In the allocated address there may be garbage
in the memory and therefore we must guarantee a safe initialization
for our object.
We will create a method date_modify() with that responsibility.
Then we make a method to securely initialize each member: date_init(struct Date dt)
Then if we want to print the date we must first modify the date security by calling this function:
void date_print(const struct Date dt)
When you pass a largestruct, the compiler generates code to make a copy of that struct if you pass
it by value. This wastes CPU cycles and may create a situation when your program runs out of
stack space, especially on hardware with scarce resources, such as embedded microcontrollers.
********************
Output
Date:
Day=1
Month=11
Year=2020
Status=1
Error: Object initialization Problem:\
Please, Verify your Serial Port of your Iot Hardware.
********************
GitHub Repo:
https://github.com/giljr/c
Webpage:
https://medium.com/jungletronics/data-structure-struct-in-c-caf48ae3c1b2
Inspired by: microgenios.com.br
Edited by j3
Date: Nov, 29/2020
*/
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
struct Date
{
unsigned int day;
unsigned int month;
unsigned int year;
unsigned int status;
};
struct Date date_init(struct Date *dt)
{
dt -> day = 1;
dt -> month = 1;
dt -> year = 2020;
dt -> status = 1;
return *dt;
}
int validate()
{
// Hardware Serial initialization stake goes here!
return FALSE;
//return TRUE;
}
struct Date date_modify(struct Date *dt, int day, int month, int year)
{
dt -> day = 28;
dt -> month = 11;
dt -> year = 2020;
// or
//(*dt).day = 28;
//(*dt).month = 11;
//(*dt).year = 2020;
if(validate())
{
dt -> status = TRUE;
//(*dt).status = 1;
} else {
dt -> status = FALSE;
//(*dt).status = 0;
}
return *dt;
}
void date_print(const struct Date *dt)
{
if(dt->status)
{
//printf("Date:\nDay=%d\nMonth=%d\nYear=%d\nStatus=%d\n\n", dt.day, dt.month, dt.year,dt.status);
//printf("Date:\nDay=%d\nMonth=%d\nYear=%d\nStatus=%d", (*dt).day, (*dt).month, (*dt).year,(*dt).status);
printf("Date:\nDay=%d\nMonth=%d\nYear=%d\nStatus=%d\n\n", dt->day, dt->month, dt->year, dt->status);
}
else
{
printf("Error: Object initialization Problem:\\\nPlease, Verify your Serial Port of your Iot Hardware.\n");
}
}
main()
{
struct Date clock;
//UART_Init(9600);
clock = date_init(&clock);
date_print(&clock);
clock = date_modify(&clock, 28, 11, 2020);
//clock.day = 28;
//clock.month = 11;
//clock.year = 2020;
//clock.status = 0;
date_print(&clock);
//printf("Date:\nDay=%d\nMonth=%d\nYear=%d\nStatus=%d", dt.day, dt.month, dt.year,dt.status);
return 0;
}