Like Tree1Likes
  • 1 Post By claudiu

Linked list: function with a struct ptr parameter in header file error

This is a discussion on Linked list: function with a struct ptr parameter in header file error within the C Programming forums, part of the General Programming Boards category; Good day to whoever reads this OS: Linux Mint 10 Julia IDE: Codeblocks 10.05 with GNU GCC as compiler The ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    4

    Question Linked list: function with a struct ptr parameter in header file error

    Good day to whoever reads this
    OS: Linux Mint 10 Julia
    IDE: Codeblocks 10.05 with GNU GCC as compiler
    The program is working when in one single file but an error surfaces when it is divided into separate files namely: program.c, mylib.h and create.c
    I removed other functions so I could experiment with only one but still
    Code:
    void create(PTR_NODE *HEAD)
    and
    Code:
    void create(PTR_NODE *)
    does not do the trick in the header file..

    Small portion of the program:
    Code:
    // Code in program.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <mylib.h>      //my user-defined header
    
    
    struct node         //this is the struct of a node
    {
        int data;
        struct list *link;
    };
    
    
    typedef struct node NODE;   //NODE is declared with a format of struct node containing elements data and link
    typedef NODE *PTR_NODE;     //this declaration confuses me...
                                //the declared NODE is used as a type for another declaration but of pointer
                                //PTR_NDOE works fine in declaring the head and tail for the linked list
    int main()
    {
        PTR_NODE head = NULL; //declare and initialize head
        //Other code
        create(&head); //in order to create a node, function must know what is head
    }
    
    
    // Code in mylib.h
    void create(PTR_NODE *HEAD)     //this (pass by reference?) does not work when linked in separate files (program.c, mylib.c and create.c)
                                    //error says: expected ')' before '*' token
    // Code in create.c
    void create(PTR_NDOE *HEAD){
        PTR_NODE local_variable_head;   //declare
        local_variable_head = *HEAD;    //copy contents
        //create a node
    }
    Any response will be appreciated.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    that error usually means the compiler doesn't know what PTR_NODE is. do you have the struct defintion and typedef above the function prototype in mylib?

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Stop using typedefs in general unless you have good control over them. There is no problem with writing struct node instead of NODE. In fact, it is far more readable. There are way too many typedef happy campers out there. Ironically, you are supposed to use typedef to shorten the time it takes for you to type the code, but in many cases beginners who use it just increase their debugging and syntax correction time by a lot.

    Never typedef pointer types. That's what is confusing you.
    cas likes this.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    Quote Originally Posted by dmh2000 View Post
    that error usually means the compiler doesn't know what PTR_NODE is. do you have the struct defintion and typedef above the function prototype in mylib?
    No. Another error happens when I duplicate it or transferred that from program.c to the mylib.h

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also note there is no passing by reference in C. A reference is not the same as a pointer, although pointers are used to implement them in many languages. When you pass a pointer to a function you are still passing a value, the value being the address of whatever object it points to.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    yes. if you duplicate it, yes you'll get an error. you should have one definition of the struct and one instance of the typedefs, in your header file.
    Code:
    svnroot@sncspksvn01:~/code> cat x.c
    void create(PTR_NODE *head);
    
    void create(PTR_NODE *head) {
    }
    
    
    svnroot@sncspksvn01:~/code> gcc -c -o x x.c
    x.c:1: error: expected ) before * token
    x.c:3: error: expected ) before * token
    svnroot@sncspksvn01:~/code>
    Code:
    svnroot@sncspksvn01:~/code> cat x.c
    typedef char *PTR_NODE;
    void create(PTR_NODE *head);
    
    void create(PTR_NODE *head) {
    }
    
    
    svnroot@sncspksvn01:~/code> gcc -c -o x x.c
    svnroot@sncspksvn01:~/code>
    Last edited by dmh2000; 04-19-2012 at 09:30 AM.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    you need to cut your code down to a minimal example and post the whole thing. when you say things like 'another error happens' without showing EXACTLY what the error is, you are not really trying to solve your problem

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    Quote Originally Posted by claudiu View Post
    Stop using typedefs in general unless you have good control over them.
    Same error appears. I don't know what format must the parameters in the functions should be even if I change the typedefs. (The labels appearing when cursor is hovered above the declarations are the same so I know now that they are same). The thing I can't figure is how to make it work..

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,512
    Quote Originally Posted by D.Last.Raven View Post
    Code:
    #include <mylib.h>      //my user-defined header
    WARNING: You should use quotes around user-defined includes!

    As like this
    Code:
    #include "mylib.h"      //my user-defined header
    NOTE: You need also to have guards in the headers.
    Read http://en.wikipedia.org/wiki/Include...include_guards

    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

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    program.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "grandfather.h"
    #include "mylib.h"
    
    
    struct node NODE;
    struct NODE *PTR_NODE;
    
    
    int main()
    {
        PTR_NODE head = NULL;
        create(&head);
        return 0;
    }
    mylib.h
    Code:
    #include "grandfather.h"
    void create(PTR_NODE *HEAD);
    grandfather.h
    Code:
    #ifndef GRANDFATHER_H
    #define GRANDFATHER_H
    
    
    struct node 
    {
        int data;
        struct node *link;
    };
    
    
    #endif
    create.c
    Code:
    #include <stdio.h>
    #include "grandfather.h"
    void create(PTR_NODE *HEAD){
        printf("ok");
    }
    I haven't used #include guard so I can't make it work.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    2,512
    My best guess at what should be in mylib.h header file.
    For the rest of you code to possibly work.

    Edit: Took a few tries to get typedefs as close to correct as I can without compiling them to check.

    Tim S.

    Code:
    #ifndef MYLIB_H
    #define MYLIB_H
    
    
    struct node 
    {
        int data;
        struct node *link;
    };
    
    typedef struct node NODE; 
    typedef NODE *PTR_NODE; /* NOTE THIS IS NOT WISE TO DO */
    
    void create(PTR_NODE *HEAD);
    
    #endif /* MYLIB_H */
    How I would write the header instead

    Code:
    #ifndef MYLIB_H
    #define MYLIB_H
    
    struct node_tag 
    {
        int data;
        struct node_tag *link;
    };
    
    typedef struct node_tag node; 
    
    void create(node **head);
    
    #endif /* MYLIB_H */
    Last edited by stahta01; 04-19-2012 at 10:52 AM.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  2. Replies: 10
    Last Post: 09-30-2009, 10:10 AM
  3. struct declared in parameter list
    By fguy817817 in forum C Programming
    Replies: 5
    Last Post: 03-25-2009, 12:35 PM
  4. Linked List!!!Error in Clear() function!!please help me..
    By zaracattle in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2006, 01:11 PM
  5. file & linked list run time error
    By Micko in forum C Programming
    Replies: 9
    Last Post: 03-06-2004, 01:58 AM

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