pointer dereference problem in sortFunction

This is a discussion on pointer dereference problem in sortFunction within the C Programming forums, part of the General Programming Boards category; Hello everyone, I am experiencing a warning of dereferencing a pointer in my sortFunction. It is occuring on line 4 ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    pointer dereference problem in sortFunction

    Hello everyone, I am experiencing a warning of dereferencing a pointer in my sortFunction. It is occuring on line 4 of the sortFunction. The compiler is also telling me "request for member 'course' in something not a structure or union" when clearly it is. It is also on the same line.
    I am using qsort on a typedef struct and not seeming to get where mmy problem is. I am hoping someone can tell me what I can do to fix it. I have included my .h , main and sortFunction files. They are not long.

    classes.h:
    Code:
    #define CLASSES_H_INCLUDED
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define BINFILE "classes.db"
    #define MAX_RECORD 10
    
    typedef enum {MW, TR} days;
    
    typedef struct {
        int hour, min;
    } Time;
    
    typedef struct {
        char Dept[5];
        int course, sect;
        days meet_days;
        Time start, end;
        char instr[20];
    } sched_record;
    
    int sortFunction(const void *p, const void *q);
    #endif // CLASSES_H_INCLUDED
    main.c
    Code:
    #include "classes.h"
    
    int main(void)
    {
        int switchInput;
        int i,temp;
        char cName[10];
        FILE *filePointer;
        sched_record data[MAX_RECORD];
    
        filePointer = fopen (BINFILE, "rb");
        if (filePointer == NULL) {
            printf("**Can't open file**");
            printf("**Check file permissions of file path**");
            exit(1);
        }
    
        printf("Enter your choice: ");
        scanf("%d", &switchInput);
    
        switch(switchInput)
        {
            case 1:
    
                fread(data, sizeof(sched_record), MAX_RECORD, filePointer);
                fclose(filePointer);
                qsort(data, MAX_RECORD, sizeof(sched_record), sortFunction);
                for (i = 0; i < MAX_RECORD; i++ ){
                printf("\n%s", data[i].course);
                }
        }
    }
    sortFunction:
    Code:
    #include "classes.h"
    
    int sortFunction(const void *p, const void *q) {
            return ((sched_record *) p)->course - ((sched_record *) q->course);
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    use
    Code:
        return ((sched_record *) p)->course - ((sched_record *) q)->course;
    Kurt

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Code:
    #include "classes.h"
     
    int sortFunction(const void *p, const void *q) {
            return ((sched_record *) p)->course - ((sched_record *) q->course);
    }
    you handled the cast of 'p' differently from the cast of 'q'.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You've put a closing parenthesis in the wrong place:
    Code:
    //This
            return ((sched_record *) p)->course - ((sched_record *) q->course);
    //should be
            return ((sched_record *) p)->course - ((sched_record *) q)->course;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by ZuK View Post
    use
    Code:
        return ((sched_record *) p)->course - ((sched_record *) q)->course;
    Kurt
    Dang! Thank You. I appreciate it. All is good except an undefined reference to 'WinMain@16' in code::blocks in the sortFunction tab and then on the main tab, I get an undefined reference to 'sortFunction.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by csharp100 View Post
    All is good except an undefined reference to 'WinMain@16' in code::blocks in the sortFunction tab and then on the main tab, I get an undefined reference to 'sortFunction.
    Sounds like you have setup a Windows Application project but you have actually written a console application. Also the file sortFunction ( is that the actual name ? it should have a .c extension) has to be added to the project.
    But then I don't know anything about code::blocks
    Kurt
    Last edited by ZuK; 04-19-2012 at 05:08 PM.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Actually I just decided to set up a window application project and made new files with the exact same coded as above, with of course the correction to the sortFunction. Now I get a
    Code:
    undefined reference to 'sortFunction'
    I checked the spelling, and I have it defined in my .h file. Wish I can figure this one out. It is pointing to line 26 of my main.c file. I pretty sure this is the way to define the parameters of the qsort function.
    Code:
    qsort(data, MAX_RECORD, sizeof(sched_record), sortFunction);

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Interesting, I just tried it on our unix system at school and works perfectly fine.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,569
    Quote Originally Posted by csharp100 View Post
    Actually I just decided to set up a window application project and made new files with the exact same coded as above, with of course the correction to the sortFunction. Now I get a
    Code:
    undefined reference to 'sortFunction'
    I checked the spelling, and I have it defined in my .h file. Wish I can figure this one out. It is pointing to line 26 of my main.c file. I pretty sure this is the way to define the parameters of the qsort function.
    Code:
    qsort(data, MAX_RECORD, sizeof(sched_record), sortFunction);
    That is a linker error. Verify the spelling and case of the function.
    Make sure you added the C source file containing sortFunction to the CB Project.
    Verify that the files containing the code is really being compiled.
    Verify that the right files are being linked in the right order.

    Turning on Full Compiler Logging should help verify the above items
    FAQ-Compiling (errors) - CodeBlocks

    Tim S.
    Last edited by stahta01; 04-19-2012 at 05:35 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  10. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Here is the compile log:
    Code:
    mingw32-gcc.exe -Wall  -g     -c "C:\Users\Clint Sharp\Documents\C Files\Server1\main.c" -o obj\Debug\main.o
    mingw32-g++.exe  -o bin\Debug\Server1.exe obj\Debug\main.o    
    obj\Debug\main.o: In function `main':
    C:/Users/Clint Sharp/Documents/C Files/Server1/main.c:26: undefined reference to `sortFunction'
    collect2: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 0 seconds)
    1 errors, 0 warnings
    Maybe someone can figure out what the heck is going on.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    2,569
    Make sure you added the C source file containing sortFunction to the CB Project.

    What files contains the sortFunction function?

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  12. #12
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by stahta01 View Post
    Make sure you added the C source file containing sortFunction to the CB Project.

    What files contains the sortFunction function?

    Tim S.
    sortFunction.c
    Like i said earlier, I created a new project called Server_ then I right clicked and added main.c and sortFunction.c under Sources and the classes.h header file is under Headers tab.
    Just looked in the obj/Debug folder and the only object file is main.o. Wonder why the other files are not compiling along with main? It is all under one project.
    Last edited by csharp100; 04-19-2012 at 06:11 PM.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    2,569
    Quote Originally Posted by csharp100 View Post
    sortFunction.c
    Like i said earlier, I created a new project called Server_ then I right clicked and added main.c and sortFunction.c under Sources and the classes.h header file is under Headers tab.
    Then you have the CB Project set up wrong!

    My link command below; I uses class.c to hold sortFunction.
    Code:
    g++.exe  -o bin\Debug\testqsort.exe obj\Debug\main.o obj\Debug\classes.o
    Again, I ask you to add the sortFunction.c file to the CB Project.
    NOTE: Have it opened in the CB Editor DOES NOT mean it is in a CB Project.

    Edit: Please re-build the whole project; is the sortFunction.c file being compiled?
    Directions to rebuild a project is: "Build" -> "Rebuild"

    What I get in build log
    Code:
    -------------- Build: Debug in testqsort ---------------
    
    gcc.exe -Wall  -g     -c H:\SourceCode\Projects\testqsort\main.c -o obj\Debug\main.o
    H:\SourceCode\Projects\testqsort\main.c: In function 'main':
    H:\SourceCode\Projects\testqsort\main.c:29:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat]
    H:\SourceCode\Projects\testqsort\main.c:7:10: warning: unused variable 'cName' [-Wunused-variable]
    H:\SourceCode\Projects\testqsort\main.c:6:11: warning: unused variable 'temp' [-Wunused-variable]
    H:\SourceCode\Projects\testqsort\main.c:32:1: warning: control reaches end of non-void function [-Wreturn-type]
    gcc.exe -Wall  -g     -c H:\SourceCode\Projects\testqsort\classes.c -o obj\Debug\classes.o
    g++.exe  -o bin\Debug\testqsort.exe obj\Debug\main.o obj\Debug\classes.o    
    Output size is 54.30 KB
    Process terminated with status 0 (0 minutes, 0 seconds)
    0 errors, 4 warnings

    Tim S.
    Last edited by stahta01; 04-19-2012 at 06:15 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    2,569
    If you right click on the file sortFunction.c under Sources project then select properties.

    What is on build tab?

    Both compile and link should be checked.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  15. #15
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by stahta01 View Post
    Then you have the CB Project set up wrong!

    My link command below; I uses class.c to hold sortFunction.
    Code:
    g++.exe  -o bin\Debug\testqsort.exe obj\Debug\main.o obj\Debug\classes.o
    Again, I ask you to add the sortFunction.c file to the CB Project.
    NOTE: Have it opened in the CB Editor DOES NOT mean it is in a CB Project.

    Edit: Please re-build the whole project; is the sortFunction.c file being compiled?
    Directions to rebuild a project is: "Build" -> "Rebuild"

    What I get in build log
    Code:
    -------------- Build: Debug in testqsort ---------------
    
    gcc.exe -Wall  -g     -c H:\SourceCode\Projects\testqsort\main.c -o obj\Debug\main.o
    H:\SourceCode\Projects\testqsort\main.c: In function 'main':
    H:\SourceCode\Projects\testqsort\main.c:29:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat]
    H:\SourceCode\Projects\testqsort\main.c:7:10: warning: unused variable 'cName' [-Wunused-variable]
    H:\SourceCode\Projects\testqsort\main.c:6:11: warning: unused variable 'temp' [-Wunused-variable]
    H:\SourceCode\Projects\testqsort\main.c:32:1: warning: control reaches end of non-void function [-Wreturn-type]
    gcc.exe -Wall  -g     -c H:\SourceCode\Projects\testqsort\classes.c -o obj\Debug\classes.o
    g++.exe  -o bin\Debug\testqsort.exe obj\Debug\main.o obj\Debug\classes.o    
    Output size is 54.30 KB
    Process terminated with status 0 (0 minutes, 0 seconds)
    0 errors, 4 warnings

    Tim S.
    Thanks Tim, got it working. I did not check the two boxes "Debug" and "Release" to add files to project. Got it working now.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with dual dereference of a struct pointer
    By dan_paul in forum C Programming
    Replies: 5
    Last Post: 11-12-2011, 08:13 PM
  2. null pointer dereference
    By qwertylurker in forum C Programming
    Replies: 3
    Last Post: 03-14-2011, 12:06 AM
  3. Suspicious dereference of pointer
    By subhashish1213 in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2009, 08:19 AM
  4. Pointer dereference
    By taurus in forum C Programming
    Replies: 1
    Last Post: 11-09-2008, 06:41 AM
  5. Dereference pointer to void pointer to member
    By phil in forum C Programming
    Replies: 5
    Last Post: 04-20-2005, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21