Problem defining structure

This is a discussion on Problem defining structure within the C Programming forums, part of the General Programming Boards category; I am trying to compile a program but GCC shows an error in it. I looked around and the code ...

  1. #1
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198

    Problem defining structure

    I am trying to compile a program but GCC shows an error in it. I looked around and the code looks perfectly valid to me. Can you tell what's wrong?

    Code:
    /*
    Linked List of Text Lines For Use with Text Editors
    */
    
    #include <stdio.h>	//For File I/O
    #include <stdlib.h>	//For malloc(), etc.
    #include <string.h>	//Array Manipulation Functions
    
    struct linked_line {	// Linked Line Structure
    	char* text = NULL;
    	unsigned long alloc = 0;
    	unsigned long len = 0;
    	struct linked_line* prev = NULL;
    	struct linked_line* next = NULL;
    };
    typedef struct linked_line LINKED_LINE;
    
    #define LL_ALLOC_STEP 1024                         //Line memory allocation step
    
    LINKED_LINE* ll_new();                                   //Create new blank line
    
    LINKED_LINE* ll_new(FILE* file);            //Create new list of lines from file
    
    void ll_realloc(LINKED_LINE* ll, unsigned long num_chars); //Reallocate in steps
    
    void ll_squash(LINKED_LINE* ll);                //Reallocate to just enough room
    
    unsigned long ll_get_line_num(LINKED_LINE* ll);             //Get number of line
    
    unsigned long ll_get_line_count(LINKED_LINE* ll);          //Get amount of lines
    
    void ll_ins_char(LINKED_LINE* ll, char c, unsigned long col;  //Insert character
    
    void ll_ins_line(LINKED_LINE* ll);                                 //Insert line
    
    void ll_ins_str(LINKED_LINE* ll, char* str, unsigned long col);  //Insert string
    
    void ll_merge_lines(LINKED_LINE *ll);                              //Merge lines
    
    void ll_split_line(LINKED_LINE *ll, unsigned long col);             //Split line
    Error Message:

    Code:
    $ gcc editor2.c linked-line.c -lncurses
    In file included from editor2.c:2:
    linked-line.h:10: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
    editor2.c:4: error: expected declaration specifiers or ‘...’ before ‘wprint_ll’
    In file included from linked-line.c:1:
    linked-line.h:10: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
    linked-line.c:4: error: expected ‘;’, ‘,’ or ‘)’ before ‘{’ token

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    Code:
    void ll_ins_char(LINKED_LINE* ll, char c, unsigned long col;  //Insert character
    Something's missing from this line

    EDIT: Also noticed that the structure is trying to initialize its members which is a bit no-no.
    Last edited by bithub; 09-08-2009 at 01:26 PM.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    I corrected that, but still the same error.

    Now it is even has pages and pages of stuff that looks like this, too:

    Code:
    linked-line.c:120: error: ‘LINKED_LINE’ has no member named ‘len’
    linked-line.c:121: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:122: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:122: error: ‘LINKED_LINE’ has no member named ‘text’
    linked-line.c:122: error: ‘LINKED_LINE’ has no member named ‘len’

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    In that case, I would need to see some of the code in the .c file to see what is generating the error.

    This compiles just fine on my machine (gcc 4.01):
    Code:
    #include <stdio.h>  //For File I/O
    #include <stdlib.h> //For malloc(), etc.
    #include <string.h> //Array Manipulation Functions
    
    struct linked_line {    // Linked Line Structure
        char* text;
        unsigned long alloc;
        unsigned long len;
        struct linked_line* prev;
        struct linked_line* next;
    };
    typedef struct linked_line LINKED_LINE;
    
    #define LL_ALLOC_STEP 1024                         //Line memory allocation step
    
    LINKED_LINE* ll_new();                                   //Create new blank line
    
    LINKED_LINE* ll_new(FILE* file);            //Create new list of lines from file
    
    void ll_realloc(LINKED_LINE* ll, unsigned long num_chars); //Reallocate in steps
    
    void ll_squash(LINKED_LINE* ll);                //Reallocate to just enough room
    
    unsigned long ll_get_line_num(LINKED_LINE* ll);             //Get number of line
    
    unsigned long ll_get_line_count(LINKED_LINE* ll);          //Get amount of lines
    
    void ll_ins_char(LINKED_LINE* ll, char c, unsigned long col);  //Insert character
    
    void ll_ins_line(LINKED_LINE* ll);                                 //Insert line
    
    void ll_ins_str(LINKED_LINE* ll, char* str, unsigned long col);  //Insert string
    
    void ll_merge_lines(LINKED_LINE *ll);                              //Merge lines
    
    void ll_split_line(LINKED_LINE *ll, unsigned long col);             //Split line
    
    int main(void)
    {
        LINKED_LINE line;
        line.text = NULL;
        line.alloc = 0;
        line.len = 0;
        line.prev = NULL;
        line.next = NULL;
        return 0;
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Here are the three source files:
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    Well, one problem is that the ll_new() function is overloaded. While this is perfectly valid C++, it is illegal in C.

    Also, the ll_new() function is returning a reference to a local variable, so that will crash when the application is run.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Quote Originally Posted by bithub View Post
    Well, one problem is that the ll_new() function is overloaded. While this is perfectly valid C++, it is illegal in C.
    I named the one that takes a FILE ll_file().

    Quote Originally Posted by bithub View Post
    Also, the ll_new() function is returning a reference to a local variable, so that will crash when the application is run.
    I made it to create a pointer and return it, instead of creating a LINKED_LINE and returning its address.

    But it still produces errors!

    Code:
    $ gcc editor2.c linked-line.c -lncurses
    editor2.c:4: error: expected declaration specifiers or ‘...’ before ‘LINKED_LINE’
    editor2.c: In function ‘wprint_ll’:
    editor2.c:6: error: ‘LINKED_LINE’ undeclared (first use in this function)
    editor2.c:6: error: (Each undeclared identifier is reported only once
    editor2.c:6: error: for each function it appears in.)
    editor2.c:6: error: ‘temp’ undeclared (first use in this function)
    editor2.c:6: error: ‘ll’ undeclared (first use in this function)
    editor2.c:9: warning: passing argument 1 of ‘wprintw’ from incompatible pointer type
    /usr/include/ncurses.h:771: note: expected ‘struct WINDOW *’ but argument is of type ‘char *’
    editor2.c: In function ‘main’:
    editor2.c:40: error: ‘LINKED_LINE’ undeclared (first use in this function)
    editor2.c:40: error: ‘line’ undeclared (first use in this function)
    editor2.c:51: error: ‘ll’ undeclared (first use in this function)
    editor2.c:55: error: invalid type argument of ‘->’ (have ‘long unsigned int’)
    editor2.c:63: error: ‘text’ undeclared (first use in this function)
    In file included from linked-line.c:1:
    linked-line.h:10: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
    linked-line.c: In function ‘ll_new’:
    linked-line.c:6: error: ‘LINKED_LINE’ has no member named ‘text’
    linked-line.c:7: error: ‘LINKED_LINE’ has no member named ‘text’
    linked-line.c:8: error: ‘LINKED_LINE’ has no member named ‘prev’
    linked-line.c:9: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:10: error: ‘LINKED_LINE’ has no member named ‘alloc’
    linked-line.c:11: error: ‘LINKED_LINE’ has no member named ‘len’
    linked-line.c: In function ‘ll_file’:
    linked-line.c:21: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:22: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:24: error: ‘LINKED_LINE’ has no member named ‘len’
    linked-line.c:25: error: ‘LINKED_LINE’ has no member named ‘len’
    linked-line.c:28: error: ‘LINKED_LINE’ has no member named ‘prev’
    linked-line.c:29: error: ‘LINKED_LINE’ has no member named ‘prev’
    linked-line.c: In function ‘ll_realloc’:
    linked-line.c:38: error: ‘LINKED_LINE’ has no member named ‘alloc’
    linked-line.c:39: error: ‘LINKED_LINE’ has no member named ‘text’
    linked-line.c:39: error: ‘LINKED_LINE’ has no member named ‘text’
    linked-line.c:40: error: ‘LINKED_LINE’ has no member named ‘alloc’
    linked-line.c: In function ‘ll_squash’:
    linked-line.c:46: error: ‘LINKED_LINE’ has no member named ‘alloc’
    linked-line.c:46: error: ‘LINKED_LINE’ has no member named ‘len’
    linked-line.c:47: error: ‘LINKED_LINE’ has no member named ‘text’
    linked-line.c:47: error: ‘LINKED_LINE’ has no member named ‘text’
    linked-line.c:47: error: ‘LINKED_LINE’ has no member named ‘len’
    linked-line.c: In function ‘ll_get_line_num’:
    linked-line.c:55: error: ‘LINKED_LINE’ has no member named ‘prev’
    linked-line.c:56: error: ‘LINKED_LINE’ has no member named ‘prev’
    linked-line.c: In function ‘ll_get_line_count’:
    linked-line.c:66: error: ‘LINKED_LINE’ has no member named ‘prev’
    linked-line.c:67: error: ‘LINKED_LINE’ has no member named ‘prev’
    linked-line.c:71: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:72: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c: In function ‘ll_ins_char’:
    linked-line.c:80: error: ‘LINKED_LINE’ has no member named ‘len’
    linked-line.c:81: error: ‘LINKED_LINE’ has no member named ‘text’
    linked-line.c:81: error: ‘LINKED_LINE’ has no member named ‘text’
    linked-line.c:81: error: ‘LINKED_LINE’ has no member named ‘len’
    linked-line.c:82: error: ‘LINKED_LINE’ has no member named ‘text’
    linked-line.c:83: error: ‘LINKED_LINE’ has no member named ‘len’
    linked-line.c: In function ‘ll_ins_line’:
    linked-line.c:88: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:89: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:90: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:92: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:93: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:94: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:95: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:96: error: ‘LINKED_LINE’ has no member named ‘prev’
    linked-line.c:96: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c: In function ‘ll_ins_str’:
    linked-line.c:103: error: ‘LINKED_LINE’ has no member named ‘len’
    linked-line.c:104: error: ‘LINKED_LINE’ has no member named ‘text’
    linked-line.c:104: error: ‘LINKED_LINE’ has no member named ‘text’
    linked-line.c:104: error: ‘LINKED_LINE’ has no member named ‘len’
    linked-line.c:105: error: ‘LINKED_LINE’ has no member named ‘text’
    linked-line.c:106: error: ‘LINKED_LINE’ has no member named ‘len’
    linked-line.c: In function ‘ll_merge_lines’:
    linked-line.c:111: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:111: error: ‘LINKED_LINE’ has no member named ‘len’
    linked-line.c:112: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:112: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:113: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:114: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c: In function ‘ll_split_line’:
    linked-line.c:120: error: ‘LINKED_LINE’ has no member named ‘len’
    linked-line.c:121: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:122: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:122: error: ‘LINKED_LINE’ has no member named ‘text’
    linked-line.c:122: error: ‘LINKED_LINE’ has no member named ‘len’
    linked-line.c:123: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:124: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:124: error: ‘LINKED_LINE’ has no member named ‘next’
    linked-line.c:125: error: ‘LINKED_LINE’ has no member named ‘text’
    linked-line.c:126: error: ‘LINKED_LINE’ has no member named ‘len’
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    Code:
    int main()
    {
    	// ...
    	LINKED_LINE* line;
    	unsigned long line = 0, col = 0;
    In the above code, you have 2 variables declared right next to each other. Both variables are named line. Also, your editor.c file is not including linked-line.h.
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Quote Originally Posted by bithub View Post
    Code:
    int main()
    {
    	// ...
    	LINKED_LINE* line;
    	unsigned long line = 0, col = 0;
    In the above code, you have 2 variables declared right next to each other. Both variables are named line. Also, your editor.c file is not including linked-line.h.
    Didn't help.
    Attached Files Attached Files

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well, keep posting your error messages. Did you fix this?
    Also, your editor.c file is not including linked-line.h.
    You also have some invalid code here:
    Code:
    struct linked_line {    // Linked Line Structure
            char* text = NULL;
            unsigned long alloc = 0;
            unsigned long len = 0;
            struct linked_line* prev = NULL;
            struct linked_line* next = NULL;
    };
    You can't initialize members of structures like that; that's just a structure declaration, it doesn't create an instance of it at all.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    That fixed it. I didn't know that you can't initialize members of a struct like that.

    Of course at the first character I typed I got a seg fault , but I will work on that.

  12. #12
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Now it's working.

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    That fixed it. I didn't know that you can't initialize members of a struct like that.
    Could have saved you (and me) a lot of time if you had just read my first post in this thread...
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem getting structure to work in my function
    By Tom Bombadil in forum C Programming
    Replies: 18
    Last Post: 05-28-2009, 10:53 AM
  2. Structure problem
    By lolguy in forum C Programming
    Replies: 4
    Last Post: 12-18-2008, 10:32 PM
  3. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 10:35 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 02:53 PM

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