質問編集履歴

2

解答を参考に書き直したため

2021/06/15 11:59

投稿

tamintya
tamintya

スコア34

test CHANGED
File without changes
test CHANGED
@@ -187,3 +187,157 @@
187
187
  in ../sysdeps/x86_64/multiarch/strcmp.S
188
188
 
189
189
  ```
190
+
191
+
192
+
193
+ <追記>
194
+
195
+ セグメンテーション違反が発生したコード
196
+
197
+
198
+
199
+ ```
200
+
201
+ #include<stdio.h>
202
+
203
+ #include<stdlib.h>
204
+
205
+ #include<string.h>
206
+
207
+
208
+
209
+ struct node{
210
+
211
+ char animal[20];
212
+
213
+ struct node *left;
214
+
215
+ struct node *right;
216
+
217
+ };
218
+
219
+ typedef struct node node;
220
+
221
+
222
+
223
+ int member_tree(char *namae , struct node *p){
224
+
225
+ int diff = strcmp(namae , p->animal);
226
+
227
+ if(p == NULL) return 0;
228
+
229
+ if(diff > 0) return member_tree(namae , p->right);
230
+
231
+ if(diff < 0) return member_tree(namae , p->left);
232
+
233
+ return 1;
234
+
235
+ }
236
+
237
+
238
+
239
+ int main(void){
240
+
241
+ char name[20];
242
+
243
+ char *p;
244
+
245
+ node *root;
246
+
247
+ node *elephant,*cat,*dog,*rat,*hourse,*bat,*hamster;
248
+
249
+ char animals[][20] = {"elephant","cat","dog","rat","horse","bat","hamster"};
250
+
251
+ root = (node*)malloc(sizeof(node));
252
+
253
+ elephant = (node*)malloc(sizeof(node));
254
+
255
+ cat = (node*)malloc(sizeof(node));
256
+
257
+ dog = (node*)malloc(sizeof(node));
258
+
259
+ rat = (node*)malloc(sizeof(node));
260
+
261
+ hourse = (node*)malloc(sizeof(node));
262
+
263
+ bat = (node*)malloc(sizeof(node));
264
+
265
+ hamster = (node*)malloc(sizeof(node));
266
+
267
+ strcpy(elephant->animal,animals[0]);
268
+
269
+ strcpy(cat->animal,animals[1]);
270
+
271
+ strcpy(dog->animal,animals[2]);
272
+
273
+ strcpy(rat->animal,animals[3]);
274
+
275
+ strcpy(hourse->animal,animals[4]);
276
+
277
+ strcpy(bat->animal,animals[5]);
278
+
279
+ strcpy(hamster->animal,animals[6]);
280
+
281
+
282
+
283
+ root = elephant;
284
+
285
+ root->left = cat;
286
+
287
+ root->left->left = bat;
288
+
289
+ root->left->right = dog;
290
+
291
+ root->right = rat;
292
+
293
+ root->right->left = hourse;
294
+
295
+ root->right->left->left = hamster;
296
+
297
+
298
+
299
+ while(1){
300
+
301
+ printf("動物名を英語で入力してください(00で終了)-->");
302
+
303
+ scanf("%s" , name);
304
+
305
+ // p = (node*)malloc(sizeof(node));
306
+
307
+ // strcpy(*p,name);
308
+
309
+ p = &(name[0]);
310
+
311
+ if(strcmp(name,"00") == 0){
312
+
313
+ printf("終了します\n");
314
+
315
+ break;
316
+
317
+ }
318
+
319
+ else{
320
+
321
+ if(member_tree(p,root) == 0){
322
+
323
+ printf("%sは存在しません.\n" , name);
324
+
325
+ }
326
+
327
+ else{
328
+
329
+ printf("%sは存在します.\n" , name);
330
+
331
+ }
332
+
333
+ }
334
+
335
+ }
336
+
337
+
338
+
339
+ return 0;
340
+
341
+ }
342
+
343
+ ```

1

質問の追記

2021/06/15 11:59

投稿

tamintya
tamintya

スコア34

test CHANGED
File without changes
test CHANGED
@@ -169,3 +169,21 @@
169
169
 
170
170
 
171
171
  ```
172
+
173
+ <追記>
174
+
175
+ gdbで実行した場合の表示
176
+
177
+
178
+
179
+ ```
180
+
181
+ Program received signal SIGSEGV, Segmentation fault
182
+
183
+ __strcmp_see42() at../sysdeps/x86_64/multiarch/strcmp.S:130
184
+
185
+ 130 ../sysdeps/x86_64/multiarch/strcmp.S: そのようなファイルやディレクトリはありません.
186
+
187
+ in ../sysdeps/x86_64/multiarch/strcmp.S
188
+
189
+ ```