回答編集履歴

4

誤植修正

2018/03/16 01:36

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -32,7 +32,7 @@
32
32
 
33
33
 
34
34
 
35
- `data[i]`の型は`student`であって、`student&`ではありません。
35
+ `data[i]`の型は`student`であって、`student*`ではありません。
36
36
 
37
37
  非ポインタの構造体変数 の要素を参照するときには、ドット演算子を使わねばなりません。
38
38
 

3

訂正

2018/03/16 01:36

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -60,7 +60,25 @@
60
60
 
61
61
 
62
62
 
63
- どちらも同じです。ただアロー演算子を使った方が素直でしょう。
63
+ ~~どちらも同じです。ただアロー演算子を使った方が素直でしょう。~~
64
+
65
+ **訂正: 間接参照演算子`*`よりドット演算子`.`の方が優先順位が高いため、等価ではありません。**
66
+
67
+
68
+
69
+ **追記:**
70
+
71
+ 構造体ポインタのメンバにアクセスする、等価な方法は次の二つです。
72
+
73
+ - (*構造体ポインタ変数名).要素名
74
+
75
+ - 構造体ポインタ変数名->要素名
76
+
77
+
78
+
79
+ **追記終わり**
80
+
81
+
64
82
 
65
83
 
66
84
 

2

追記

2018/03/16 01:35

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -70,7 +70,11 @@
70
70
 
71
71
  `student *data`でも`student data[]`でも同じです。
72
72
 
73
- ただ個人的には、後者の方が配列の存在を認識できる分好みです。
73
+ しかし個人的には、後者の方が配列の存在を感じられる分好みです。
74
+
75
+
76
+
77
+ (ポインタが配列を指さないことも充分あり得る。)
74
78
 
75
79
 
76
80
 
@@ -78,8 +82,6 @@
78
82
 
79
83
  ---
80
84
 
81
-
82
-
83
85
  **ドット演算子を使うべき場面で、アロー演算子を使っていること。**
84
86
 
85
87
 
@@ -104,11 +106,11 @@
104
106
 
105
107
  0. scanfの第二引数以降は、格納先のアドレスです。
106
108
 
107
- data[i].nameはポインタを返すので問題ないですが、yearとsexの読み取り誤りです。
109
+ data[i].nameはポインタを返すので問題ないですが、yearとsexは非ポインタです。
108
-
110
+
109
- 0. また、文字は`%c`で受け取る必要があります。
111
+ 0. 文字は`%c`で受け取る必要があります。
110
-
112
+
111
- 0. さらに必要に応じて**バッファのゴミを回収せねばなりません**。
113
+ 0. 必要に応じて**バッファのゴミを回収せねばなりません**。
112
114
 
113
115
 
114
116
 
@@ -355,3 +357,7 @@
355
357
  }
356
358
 
357
359
  ```
360
+
361
+
362
+
363
+ まだまだ改良の余地があるとは思いますが。

1

追記

2018/03/15 18:59

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -34,7 +34,7 @@
34
34
 
35
35
  `data[i]`の型は`student`であって、`student&`ではありません。
36
36
 
37
- 非ポインタの構造体変数の要素を参照するときには、ドット演算子を使わねばなりません。
37
+ 非ポインタの構造体変数 の要素を参照するときには、ドット演算子を使わねばなりません。
38
38
 
39
39
 
40
40
 
@@ -132,7 +132,17 @@
132
132
 
133
133
 
134
134
 
135
+ あるいは、一気に読んでしまうのもアリです。
136
+
137
+ ```C
138
+
139
+ scanf("%s\n%d\n%c\n", data[i].name, &data[i].year, &data[i].sex);
140
+
141
+ ```
142
+
143
+
144
+
135
- 本当はエラーチェックもすべきところですが、ここでは省きます。
145
+ 本当はエラーチェックもすべきところですが、ここでは省きます。(c.f. サンプル)
136
146
 
137
147
 
138
148
 
@@ -243,3 +253,105 @@
243
253
  }
244
254
 
245
255
  ```
256
+
257
+
258
+
259
+ 気が向いたので改造もしてみました。[Wandbox](https://wandbox.org/permlink/Z1Pv4LGyCVde5AjL)
260
+
261
+ ```C
262
+
263
+ #include <stdio.h>
264
+
265
+ #include <stdlib.h>
266
+
267
+ #define ARRAY_LEN(arr) (sizeof(arr) / sizeof(*arr))
268
+
269
+
270
+
271
+ typedef struct {
272
+
273
+ char name[64];
274
+
275
+ int year;
276
+
277
+ char sex; // is_man にする余地あり
278
+
279
+ } Student;
280
+
281
+
282
+
283
+ int scan_student(Student *dst);
284
+
285
+ int print_student(const Student *src);
286
+
287
+
288
+
289
+ int main(void) {
290
+
291
+ Student data[3];
292
+
293
+ for(size_t i = 0; i < ARRAY_LEN(data); ++i) {
294
+
295
+ scan_student(&data[i]);
296
+
297
+ print_student(&data[i]);
298
+
299
+ }
300
+
301
+
302
+
303
+ return 0;
304
+
305
+ }
306
+
307
+
308
+
309
+ int scan_student_without_check(Student *dst) {
310
+
311
+ return scanf(
312
+
313
+ "%s\n%d\n%c\n",
314
+
315
+ dst->name, &dst->year, &dst->sex
316
+
317
+ );
318
+
319
+ }
320
+
321
+ int scan_student(Student *dst) {
322
+
323
+ int ret = scan_student_without_check(dst);
324
+
325
+ if(ret != 3) {
326
+
327
+ fprintf(stderr, "Cannot read student data.\n");
328
+
329
+ exit(1);
330
+
331
+ }
332
+
333
+
334
+
335
+ return ret;
336
+
337
+ }
338
+
339
+
340
+
341
+ int print_student(const Student *src) {
342
+
343
+ return printf(
344
+
345
+ "[名前]\t%s\n"
346
+
347
+ "[年齢]\t%d\n"
348
+
349
+ "[性別]\t%c\n",
350
+
351
+ src->name, src->year, src->sex
352
+
353
+ );
354
+
355
+ }
356
+
357
+ ```