0

sortbyName再帰的なデータ構造を返す関数がありますsortedListsortListそれ自体が別の再帰的データ構造へのポインタを含み、stud_typeそれらはすべて以下のように定義されています。

typedef struct stud_type_ {
    int    matricnum;                 
    char   name[20];          

    struct stud_type_ *next_student;
} stud_type;

typedef struct sort_List {
    stud_type *cur;
    struct sortList *next;
} sortList;

stud_type listofStudents; // Assume that it is not NULL

sortList * sortbyName() {
    sortList *sList;

    //sort algorithm here
    return sList;
}
...
...

int main() {
//trying to define curTEST = sortbyName() here
    while (curTEST!=NULL) {
        printf("Name: %s\n", curTEST->cur->name);
        curTEST = curTEST->next;
    }
}

main()ここで、関数からの戻り値を保持する変数を関数に割り当ててsortbyName、while ループで繰り返し処理し、結果を出力できるようにします。次に、この変数をどのように定義しますか? 私は試しsortList curTEST;てみsortList * curTEST;ましたが、役に立ちませんでした。sortbyNameまたは、関数の定義に何か問題がありますか?

編集: 私はそれをコンパイルして、ほとんどの些細なエラー/警告を修正しましたが、この現在のエラーレポートに到達するまで、あまり意味がありません。

u2_4.c:208:15: warning: implicit declaration of function 'sortbyName' is invalid in C99
      [-Wimplicit-function-declaration]
    curTEST = sortbyName(); 
              ^
u2_4.c:208:13: warning: incompatible integer to pointer conversion assigning to 'sortList *'
    (aka 'struct sort_List *') from 'int' [-Wint-conversion]
    curTEST = sortbyName(); 
            ^ ~~~~~~~~~~~~~~~~~~
2 warnings generated.
Undefined symbols for architecture x86_64:
  "_sortbyName", referenced from:
      _main in u2_4-Szd3la.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [u2_4] Error 1

私のmain関数では、次curTESTのように定義しました。sortList * curTEST;

4

1 に答える 1

1

関数を別のファイルで定義していますか...または main() を定義した後ですか? 他の場所で定義されていて、main() の前に関数プロトタイプがない場合、警告とリンカー エラーが発生します。

以下のすべてを 1 つのファイル (main.c) で実行したところ、問題なくコンパイルされました。

typedef struct stud_type_ {
    int    matricnum;
    char   name[20];

    struct stud_type_ *next_student;
} stud_type;

typedef struct sort_List {
    stud_type *cur;
    struct sort_List *next;
} sortList;

stud_type listofStudents; // Assume that it is not NULL

sortList * sortbyName() {
    sortList *sList;

    //sort algorithm here
    return sList;
}

int main() {
    sortList * curTEST = sortbyName();

    while (curTEST!=NULL) {
        printf("Name: %s\n", curTEST->cur->name);
        curTEST = curTEST->next;
    }
    return 0;
}

ファイルに 2 つの変更を加えただけであることに注意してください。next へのポインターを定義するときに、sortList の構造を変更しました。から に変更しましstruct sortList *nextstruct sort_List *next。そして、curTEST を として定義し、初期化しましたsortList * curTEST = sortbyName()

于 2013-05-04T14:58:00.047 に答える