0

I have a function sortbyName that returns a recursive data structure sortedList. sortList itself contains a pointer to another recursive data structure stud_type and they are all defined as below.

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;
    }
}

Now I would like to assign a variable in the main() function to hold the return value from my sortbyName function so I can iterate through it with a while loop and print out the results. How do I then define this variable? I tried sortList curTEST; and sortList * curTEST; to no avail. Or is there something wrong with my definition of the sortbyName function?

EDIT: I've tried compiling it and corrected most of the trivial and not so trivial errors/warnings until it came to this current error report which doesn't make too much sense to me.

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

In my main function, I defined curTEST like this: sortList * curTEST;

Jon Gan
  • 867
  • 1
  • 11
  • 22
  • 1
    Read your type names again. `sortList * curTest = ...`. And in the definition, `struct sort_List *next;`, you need to spell the `struct` tag correctly. – Daniel Fischer May 04 '13 at 12:23
  • `sortedList` -> shouldn't that be `sortList`? – stijn May 04 '13 at 12:24
  • `sortList *` not `sortedList *` – suspectus May 04 '13 at 12:25
  • sorry an oversight on spelling there while typing it in. I defined it correctly in my program so it's not because of that.. – Jon Gan May 04 '13 at 12:26
  • a) copy and paste the code, don't retype it to avoid such mistakes. b) what exactly is the problem you face? If the compiler is complaining about dereferencing a pointer to an incomplete type, it's the typo in the definition of `sortList` I mentioned above. – Daniel Fischer May 04 '13 at 12:28
  • I'd recommend having the `struct` and its `typedef` have the same name - `typedef struct sortList { ... } sortList;`. – Bernhard Barker May 04 '13 at 12:37
  • Compiles just fine [here](http://ideone.com/X0akGr) (with some minor seemingly unrelated changes to get it to compile). Please construct a minimum test case by at least removing the code where the `...`'s are on your side and try to compile again. If it doesn't work, please copy and paste **the entire source file as is** into the question. – Bernhard Barker May 04 '13 at 12:49

1 Answers1

1

Are you defining your function in a different file... or after you define main()? If it is defined elsewhere and you do not have a function prototype before main(), then you will get the warnings and linker error.

I did the following all in a single file (main.c) and it compiled without any issues.

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;
}

Note that I only made two changes to your file. I changed your structure for sortList when defining the pointer to next. I changed it from struct sortList *next to struct sort_List *next. And I defined and initialized curTEST as sortList * curTEST = sortbyName().

embedded_guy
  • 1,939
  • 3
  • 24
  • 39
  • yea thanks i think I got it to work.. at least this problem's solved. I defined it previously in another file and didn't have a function prototype. – Jon Gan May 05 '13 at 12:15