C中的总线错误10

时间:2015-04-06 22:05:50

标签: c macos

我是C的新生儿,我正在从USACO做任务,这是“gift1”的任务。我想解决方案是我自己的,所以如果你只是告诉我为什么我得到这个总线错误10而不是给我这个任务的答案,那将非常感激。

我使用的是OS X系统,而gcc(来自Xcode)的版本是

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix

这是我的代码(尚未完成):

/*
LANG: C
TASK: gift1
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *input_file, *output_file;

int total_people, giving_to_counter, accepting, having[9];

char giver[9][13], receiver[9][13], person;

int people_scaner, people_judger;

int main () {
    input_file=fopen("gift1.in", "r");
    output_file=fopen("gift1.out", "w+");

    fscanf(input_file, "%d\n", &total_people);

    people_scaner = 0;

    while(people_scaner<total_people){
        fscanf(input_file, "%s", giver[people_scaner]);
        people_scaner ++;
    }

    fscanf(input_file, "%s", &person);
    printf("%s\n", &person);

    people_judger = 0;

    do{
        people_judger ++;
    } while (
        strcmp(&person, giver[people_judger]) !=0
        );

    fscanf(input_file, "%d%d", &having[people_judger], &giving_to_counter);
    printf("%d\n%s\n%d\n%d\n", people_judger, giver[people_judger], having[people_judger], giving_to_counter);


    fclose(input_file);
    fclose(output_file);
    return 0;
}

我尝试使用tihs代码来读取这样的文件:

5
dave
laura
owen
vick
arm
dave  //if this line is "laura, owen, vick or arm" the code works good, but if it is "dave" I will get bus error 10?
200 3
laura
owen
vick
owen
500 1
dave
arm
150 2
vick
owen
laura
0 2
arm
vick
vick
0 0

1 个答案:

答案 0 :(得分:0)

您已宣布

char person;

&person只能容纳一个字符。它不能拥有一个名字。因此,以下行是错误的并导致未定义的行为。

fscanf(input_file, "%s", &person);
printf("%s\n", &person);