【问题标题】:Defining variable for recursive data structure in C在 C 中为递归数据结构定义变量
【发布时间】:2013-05-04 12:21:11
【问题描述】:

我有一个函数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 函数中,我这样定义curTESTsortList * curTEST;

【问题讨论】:

  • 再次读取您的类型名称。 sortList * curTest = ...。而在定义中,struct sort_List *next;,需要正确拼写struct标签。
  • sortedList -> 不应该是sortList吗?
  • sortList * 不是sortedList *
  • 很抱歉在输入时出现了拼写疏忽。我在程序中正确定义了它,所以不是因为那个..
  • a) 复制粘贴代码,不要重复输入,以免出现此类错误。 b) 你面临的具体问题是什么?如果编译器抱怨取消引用指向不完整类型的指针,那是我上面提到的sortList 定义中的错字。

标签: c typedef recursive-datastructures


【解决方案1】:

您是在不同的文件中定义函数...还是在定义 main() 之后?如果它在别处定义并且您在 main() 之前没有函数原型,那么您将收到警告和链接器错误。

我在一个文件 (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;
}

请注意,我只对您的文件进行了两次更改。在定义指向下一个的指针时,我更改了 sortList 的结构。我将它从struct sortList *next 更改为struct sort_List *next。我将 curTEST 定义并初始化为sortList * curTEST = sortbyName()

【讨论】:

  • 是的,谢谢,我想我让它工作了.. 至少这个问题已经解决了。我之前在另一个文件中定义过,没有函数原型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
  • 2014-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多