回答編集履歴

2

C++コード修正

2020/01/31 03:14

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -118,7 +118,7 @@
118
118
 
119
119
  int num = 0;
120
120
 
121
- struct Staff *staff = NULL;
121
+ Staff *staff = NULL;
122
122
 
123
123
 
124
124
 
@@ -142,7 +142,7 @@
142
142
 
143
143
 
144
144
 
145
- struct StaffList stafflist;
145
+ StaffList stafflist;
146
146
 
147
147
  stafflist.add("akai");
148
148
 

1

C++の例を追記

2020/01/31 03:14

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -89,3 +89,83 @@
89
89
  }
90
90
 
91
91
  ```
92
+
93
+
94
+
95
+ ちょっと脱線しますが、C++だとこんな書き方ができます。
96
+
97
+
98
+
99
+ ```c++
100
+
101
+ #include <stdio.h>
102
+
103
+ #include <stdlib.h>
104
+
105
+ #include <string.h>
106
+
107
+
108
+
109
+ struct Staff {
110
+
111
+ char name[64];
112
+
113
+ };
114
+
115
+
116
+
117
+ struct StaffList {
118
+
119
+ int num = 0;
120
+
121
+ struct Staff *staff = NULL;
122
+
123
+
124
+
125
+ void add(const char *name) {
126
+
127
+ staff = (Staff *)reallocarray(staff, sizeof(Staff), num + 1);
128
+
129
+ strncpy(staff[num].name, name, sizeof(staff[num].name) - 1);
130
+
131
+ num++;
132
+
133
+ }
134
+
135
+ };
136
+
137
+
138
+
139
+ int main(void) {
140
+
141
+ printf("start\n");
142
+
143
+
144
+
145
+ struct StaffList stafflist;
146
+
147
+ stafflist.add("akai");
148
+
149
+ stafflist.add("aoi");
150
+
151
+ stafflist.add("hogehoge");
152
+
153
+
154
+
155
+ printf("result\n");
156
+
157
+ for(int i = 0; i < stafflist.num; i++) {
158
+
159
+ printf("No %d, %s\n", i+1, stafflist.staff[i].name);
160
+
161
+ }
162
+
163
+
164
+
165
+ printf("end\n");
166
+
167
+ return 0;
168
+
169
+ }
170
+
171
+ ```