teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

プログラム追加

2021/05/17 03:37

投稿

pokemonta
pokemonta

スコア170

title CHANGED
File without changes
body CHANGED
@@ -20,4 +20,16 @@
20
20
 
21
21
  return 0;
22
22
  }
23
- ```
23
+ ```
24
+ 以下のプログラムに変更すると
25
+ 8
26
+ 8
27
+ numberも構造体も正しい値が出力されませn
28
+ ```C
29
+ int main(void)
30
+ {
31
+   printf("%ld\n",sizeof(&student));
32
+   printf("%ld\n",sizeof(&student.number));
33
+
34
+ return 0;
35
+ }```

1

コード変更

2021/05/17 03:36

投稿

pokemonta
pokemonta

スコア170

title CHANGED
File without changes
body CHANGED
@@ -1,15 +1,9 @@
1
1
  構造体を指し示すポインタから任意のメンバーのサイズを
2
2
  取得したいです。どのようにすれば出力できるのでしょうか
3
- 下記のエラーが発生します。
4
- Main.c:16:17: error: unexpected type name 'student': expected expression
5
- printf("%p\n",&student);
3
+ &studentを介してnumberのサイズが取得できません
6
4
 
7
-
8
5
  ```c
9
- #include <stdio.h>
10
- #include <string.h>
11
-
12
- typedef struct {
6
+ struct {
13
7
  int year; /* 学年 */
14
8
  int clas; /* クラス */
15
9
  int number; /* 出席番号 */
@@ -21,8 +15,8 @@
21
15
  int main(void)
22
16
  {
23
17
  printf("%ld\n",sizeof(student));
24
- printf("%p\n", &student);
18
+ printf("%p\n",&student);
25
-   printf("%ld\n",sizeof(student->number));
19
+   printf("%ld\n",sizeof(student.number));
26
20
 
27
21
  return 0;
28
22
  }