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

質問編集履歴

4

修正

2020/06/05 12:47

投稿

junnnnchan
junnnnchan

スコア26

title CHANGED
File without changes
body CHANGED
@@ -143,5 +143,5 @@
143
143
 
144
144
  <質問>
145
145
  警告を消すには、どう直したらよいでしょうか。
146
- scanfとtype argument of ‘->’の解決策がわかりません。
146
+ scanfの警告の解決策がわかりません。
147
147
  よりよい解決策をご教示お願いします。

3

修正

2020/06/05 12:47

投稿

junnnnchan
junnnnchan

スコア26

title CHANGED
File without changes
body CHANGED
@@ -136,18 +136,10 @@
136
136
  }
137
137
 
138
138
  ```
139
- main.c: In function ‘main’:
140
- main.c:103:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat=]
139
+ main.c:103:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat=]
141
- scanf("%s",x.name);
142
- ^
143
- main.c:112:73: error: invalid type argument of ‘->(have ‘PhysCheckStack {aka struct })
140
+ main.c:112:41: warning: format ‘%s’ expects argument of type char *, but argument 2 has type ‘char ** [-Wformat=]
144
- printf("ポップしたデータは%s %.1f %d です。\n", x.name[s->ptr],x.body.vision,x.body.height);
145
- ^~
146
- main.c:118:73: error: invalid type argument of ‘->(have ‘PhysCheckStack {aka struct })
141
+ main.c:118:41: warning: format ‘%s’ expects argument of type char *, but argument 2 has type ‘char ** [-Wformat=]
147
- printf("ピークしたデータは%s %.1f %d です。\n", x.name[s->ptr],x.body.vision,x.body.height);
148
- ^~
149
- main.c:124:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat=]
142
+ main.c:124:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat=]
150
- scanf("%s",x.name);
151
143
 
152
144
  <質問>
153
145
  警告を消すには、どう直したらよいでしょうか。

2

修正

2020/06/05 12:44

投稿

junnnnchan
junnnnchan

スコア26

title CHANGED
File without changes
body CHANGED
@@ -24,8 +24,8 @@
24
24
  int Search(PhysCheckStack *s , PhysCheck *x){
25
25
  int count = 0;
26
26
  for(int i = 0 ; i < s->ptr ; i ++){
27
- if(strcmp(x->name,s->stk[i].name) == 0){//string compare 文字一致したら0返す
27
+ if(strcmp(x->name[s->ptr],s->stk[i].name[s->ptr]) == 0){//string compare 文字一致したら0返す
28
- printf("%s %f %d\n",s->stk[i].name,s->stk[i].body.vision,s->stk[i].body.height);
28
+ printf("%s %f %d\n",s->stk[i].name[s->ptr],s->stk[i].body.vision,s->stk[i].body.height);
29
29
  count ++;
30
30
  }
31
31
  }
@@ -45,8 +45,8 @@
45
45
  /*--- スタックにデータをプッシュ ---*/
46
46
  int Push(PhysCheckStack *s, PhysCheck *x){
47
47
  if (s->ptr >= s->max) return -1; /* スタック満杯 */
48
- strcpy(s->stk[s->ptr].name,x->name);
48
+ strcpy(s->stk[s->ptr].name[s->ptr],x->name[s->ptr]);
49
- if ((s->stk[s->ptr].name[s->ptr] = calloc(strlen(x)+1, sizeof(char))) == NULL)
49
+ if ((s->stk[s->ptr].name[s->ptr] = calloc(strlen(x->name[s->ptr])+1, sizeof(char*))) == NULL)
50
50
  /* データをコピーするための動的な文字列保存用配列を確保することに失敗 */
51
51
  return -1;
52
52
  s->stk[s->ptr].body.vision = x->body.vision;
@@ -58,16 +58,16 @@
58
58
  int Pop(PhysCheckStack *s, PhysCheck *x){
59
59
  if (s->ptr <= 0) return -1; /* スタックは空 */
60
60
  s->ptr--;
61
- strcpy(x->name,s->stk[s->ptr].name);
61
+ strcpy(x->name[s->ptr],s->stk[s->ptr].name[s->ptr]);
62
62
  x->body.vision = s->stk[s->ptr].body.vision;
63
63
  x->body.height = s->stk[s->ptr].body.height;
64
- free(s->stk[s->ptr].name);
64
+ free(s->stk[s->ptr].name[s->ptr]);
65
65
  return (0);
66
66
  }
67
67
  /*--- スタックからデータをピーク ---*/
68
68
  int Peek(PhysCheckStack *s, PhysCheck *x){
69
69
  if (s->ptr <= 0) return -1;
70
- strcpy(x->name,s->stk[s->ptr-1].name);
70
+ strcpy(x->name[s->ptr],s->stk[s->ptr-1].name[s->ptr]);
71
71
  x->body.vision = s->stk[s->ptr-1].body.vision;
72
72
  x->body.height = s->stk[s->ptr-1].body.height;
73
73
  return 0;
@@ -85,7 +85,7 @@
85
85
  int i;
86
86
 
87
87
  for(i = 0; i < s->ptr; i++)
88
- printf("%s %f %d", s->stk[i].name,s->stk[i].body.vision,s->stk[i].body.height);
88
+ printf("%s %f %d", s->stk[i].name[s->ptr],s->stk[i].body.vision,s->stk[i].body.height);
89
89
  putchar('\n');
90
90
  }
91
91
  int main(void){
@@ -111,13 +111,13 @@
111
111
  if (Pop(&s, &x) == -1)
112
112
  puts("\a エラー:ポップに失敗しました。");
113
113
  else
114
- printf("ポップしたデータは%s %.1f %d です。\n", x.name,x.body.vision,x.body.height);
114
+ printf("ポップしたデータは%s %.1f %d です。\n", x.name[s->ptr],x.body.vision,x.body.height);
115
115
  break;
116
116
  case 3: /* ピーク */
117
117
  if (Peek(&s, &x) == -1)
118
118
  puts("\a エラー:ピークに失敗しました。");
119
119
  else
120
- printf("ピークしたデータは%s %.1f %d です。\n", x.name,x.body.vision,x.body.height);
120
+ printf("ピークしたデータは%s %.1f %d です。\n", x.name[s->ptr],x.body.vision,x.body.height);
121
121
  break;
122
122
  case 4: /* 表示 */
123
123
  Print(&s);
@@ -135,41 +135,21 @@
135
135
  return 0;
136
136
  }
137
137
 
138
- ```
139
- <警告>
140
- main.c:25:13: warning: passing argument 1 of ‘strcmpfrom incompatible
141
- pointer type [-Wincompatible-pointer-types]
142
- /usr/include/string.h:144:12: note: expected ‘const char *’ but argumen
143
- t is of type ‘char **
144
- main.c:26:13: warning: format ‘%s expects argument of type ‘char *’, b
145
- ut argument 2 has type ‘char **’ [-Wformat=]
146
-
147
- /usr/include/string.h:129:14: note: expected ‘char * restrict’ but argu
148
- ment is of type ‘char **’
149
- main.c:46:29: warning: passing argument 2 of ‘strcpyfrom incompatible
150
- pointer type [-Wincompatible-pointer-types]
151
- /usr/include/string.h:129:14: note: expected ‘const char * restrict’ bu
152
- t argument is of type ‘char **’
153
- main.c:47:51: warning: passing argument 1 of ‘strlen’ from incompatible
154
- pointer type [-Wincompatible-pointer-types]
155
- /usr/include/string.h:399:15: note: expected ‘const char *’ but argumen
156
- t is of type ‘PhysCheck * {aka struct <anonymous> *}’
157
-
158
-
159
- /usr/include/string.h:129:14: note: expected ‘const char * restrict’ bu
160
- t argument is of type ‘char **’
161
- /usr/include/string.h:129:14: note: expected ‘char * restrict’ but argu
162
- ment is of type ‘char **’
163
- main.c:68:17: warning: passing argument 2 of ‘strcpy’ from incompatible
164
- pointer type [-Wincompatible-pointer-types]
165
- /usr/include/string.h:129:14: note: expected ‘const char * restrict’ bu
166
- t argument is of type ‘char **’
167
- main.c:86:12: warning: format ‘%s’ expects argument of type ‘char *’, b
168
- ut argument 2 has type ‘char * const*’ [-Wformat=]
169
- main.c:103:12: warning: format ‘%s’ expects argument of type ‘char *’,
170
- but argument 2 has type ‘char **’ [-Wformat=]
138
+ ```
139
+ main.c: In function ‘main’:
140
+ main.c:103:12: warning: format ‘%s’ expects argument of type char *, but argument 2 has type ‘char **’ [-Wformat=]
141
+ scanf("%s",x.name);
142
+ ^
143
+ main.c:112:73: error: invalid type argument of ->’ (have ‘PhysCheckStack {aka struct })
144
+ printf("ポップしたデータは%s %.1f %d です。\n", x.name[s->ptr],x.body.vision,x.body.height);
145
+ ^~
146
+ main.c:118:73: error: invalid type argument of ‘->’ (have ‘PhysCheckStack {aka struct }’)
147
+ printf("ピークしたデータは%s %.1f %d です。\n", x.name[s->ptr],x.body.vision,x.body.height);
148
+ ^~
149
+ main.c:124:12: warning: format ‘%s’ expects argument of type char *, but argument 2 has type ‘char **’ [-Wformat=]
150
+ scanf("%s",x.name);
171
151
 
172
- -----似た警告は省略させていただきます------
173
152
  <質問>
174
153
  警告を消すには、どう直したらよいでしょうか。
175
- 型が間違っている警告されているですが、一つの型を直すと、コードの多箇所に影響すると考えてます。よりよい解決策をご教示お願いし
154
+ scanftype argument of ‘->’の解決策がわかりせん
155
+ よりよい解決策をご教示お願いします。

1

追記

2020/06/05 12:24

投稿

junnnnchan
junnnnchan

スコア26

title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,4 @@
1
+ <追記>
1
2
  ```
2
3
  #include <stdio.h>
3
4
  #include <string.h>
@@ -34,7 +35,7 @@
34
35
  /*--- スタックの初期化 ---*/
35
36
  int Initialize(PhysCheckStack *s, int max){
36
37
  s->ptr = 0;
37
- if ((s->stk[s->ptr].name = calloc(max, sizeof(char))) == NULL) {
38
+ if ((s->stk[s->ptr].name[s->ptr] = calloc(max, sizeof(char*))) == NULL) {
38
39
  s->max = 0; /* char* の配列の確保に失敗 */
39
40
  return -1;
40
41
  }
@@ -45,7 +46,7 @@
45
46
  int Push(PhysCheckStack *s, PhysCheck *x){
46
47
  if (s->ptr >= s->max) return -1; /* スタック満杯 */
47
48
  strcpy(s->stk[s->ptr].name,x->name);
48
- if ((s->stk[s->ptr].name = calloc(strlen(x)+1, sizeof(char))) == NULL)
49
+ if ((s->stk[s->ptr].name[s->ptr] = calloc(strlen(x)+1, sizeof(char))) == NULL)
49
50
  /* データをコピーするための動的な文字列保存用配列を確保することに失敗 */
50
51
  return -1;
51
52
  s->stk[s->ptr].body.vision = x->body.vision;
@@ -135,10 +136,40 @@
135
136
  }
136
137
 
137
138
  ```
138
- エラー文
139
- main.c:47:27: error: assignment to expression with array type
140
- if ((s->stk[s->ptr].name = calloc(strlen(x)+1, sizeof(char))) == NULL)
141
- main.c:36:27: error: assignment to expression with array type
142
- if ((s->stk[s->ptr].name = calloc(max, sizeof(char ))) == NULL) {
143
- ^
144
- と出てるのですが、配列と型が違うという意味らしいですが、理解できません。なぜ違うのか知りたいです。よろしくお願いします。
139
+ <警告>
140
+ main.c:25:13: warning: passing argument 1 of ‘strcmp’ from incompatible
141
+ pointer type [-Wincompatible-pointer-types]
142
+ /usr/include/string.h:144:12: note: expected ‘const char *’ but argumen
143
+ t is of type char **’
144
+ main.c:26:13: warning: format ‘%s’ expects argument of type ‘char *’, b
145
+ ut argument 2 has type ‘char **’ [-Wformat=]
146
+
147
+ /usr/include/string.h:129:14: note: expected ‘char * restrict’ but argu
148
+ ment is of type ‘char **’
149
+ main.c:46:29: warning: passing argument 2 of ‘strcpy’ from incompatible
150
+ pointer type [-Wincompatible-pointer-types]
151
+ /usr/include/string.h:129:14: note: expected ‘const char * restrict’ bu
152
+ t argument is of type ‘char **’
153
+ main.c:47:51: warning: passing argument 1 of ‘strlen’ from incompatible
154
+ pointer type [-Wincompatible-pointer-types]
155
+ /usr/include/string.h:399:15: note: expected ‘const char *’ but argumen
156
+ t is of type ‘PhysCheck * {aka struct <anonymous> *}’
157
+
158
+
159
+ /usr/include/string.h:129:14: note: expected ‘const char * restrict’ bu
160
+ t argument is of type ‘char **’
161
+ /usr/include/string.h:129:14: note: expected ‘char * restrict’ but argu
162
+ ment is of type ‘char **’
163
+ main.c:68:17: warning: passing argument 2 of ‘strcpy’ from incompatible
164
+ pointer type [-Wincompatible-pointer-types]
165
+ /usr/include/string.h:129:14: note: expected ‘const char * restrict’ bu
166
+ t argument is of type ‘char **’
167
+ main.c:86:12: warning: format ‘%s’ expects argument of type ‘char *’, b
168
+ ut argument 2 has type ‘char * const*’ [-Wformat=]
169
+ main.c:103:12: warning: format ‘%s’ expects argument of type ‘char *’,
170
+ but argument 2 has type ‘char **’ [-Wformat=]
171
+
172
+ -----似た警告は省略させていただきます------
173
+ <質問>
174
+ 警告を消すには、どう直したらよいでしょうか。
175
+ 型が間違っていると警告されているのですが、一つの型を直すと、コードの多箇所に影響すると考えてます。よりよい解決策をご教示お願いします。