Thread: Struct error

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    46

    Struct error

    I'm new in structs so this question might sound silly, but I cannot see the problem:

    I'm defining a struct in the header. This is the struct:
    Code:
    // struct to sentencedata
    struct sentencestruct {
    	int numberOfSentences;
            char *sentenceArr[10000];
    };
    But when I'm trying to use it i get error-messages:

    Code:
    void speechrecognitioncontrol() {
    
    int tempnt=0;
    struct sentencestruct speechSentences;
    signed int  pid;
    int i;
    ....
    Compiling gives this error:

    control-module.c:380: error: storage size of 'speechSentences' isn't known


    Somebody please help..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You need to include that header file in the source file which uses it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    46
    Quote Originally Posted by Salem View Post
    You need to include that header file in the source file which uses it.
    I have done that.. But i might not have written extern struct ... (like other extern variables is defined) in the beginning, now you mention it.. I'll try it out right away..

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    46
    It don't seem to work.. in the beginning of the file is written:

    Header:
    Code:
    // struct to sentencedata
    struct sentencestruct {
    	int numberOfSentences;
            char *sentenceArr[10000];
    };
    .c file:
    Code:
    #include "control-module.h"
    extern struct sentencestruct;
    
    ....
    void speechrecognitioncontrol() {
    
    int tempnt=0;
    struct sentencestruct speechSentences;
    signed int  pid;
    int i;
    ....
    Now i only get one warning about this extern struct:
    warning: useless keyword or type name in empty declaration
    control-module.c:382: error: storage size of 'speechSentences' isn't known

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    extern struct sentencestruct;

    is just like
    extern int;

    it has no meaning

    is this code
    Code:
    // struct to sentencedata
    struct sentencestruct {
    	int numberOfSentences;
            char *sentenceArr[10000];
    };
    located in the "control-module.h" header? or some other header?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    46
    I know i has no meaning, but if its not defined as en extern ressource then it cannot refference it..

    And no i is defined in global.h which is compiled together and all other variables in it is working fine.. But you caught the problem I think. Because after I tried to move it to my control-module header it can now find it; it seems at least at the moment.. Why can't I define this in the global header where all the files have access to. .?

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by sniper83 View Post
    I know i has no meaning, but if its not defined as en extern ressource then it cannot refference it..

    And no i is defined in global.h which is compiled together and all other variables in it is working fine.. But you caught the problem I think. Because after I tried to move it to my control-module header it can now find it; it seems at least at the moment.. Why can't I define this in the global header where all the files have access to. .?
    Putting something in a header file means jack if the source file in question does not #include that header file. There is no global header file that is automatically accessed by all the source files. If the struct was defined in global.h then you should have #include'd global.h for the compiler to recognize the struct.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    46
    It seem that I can make a struct.h and put all the structs in it, and then include this one in both c-files where i use them..

    But now I get another error due to these. It complains about this line in one of the files:

    Code:
    struct sentencestruct speechSentences; 
    ...
    speechSentences =	receiveMsgSpeechRecognition();
    with this message:
    control-module.c:518: error: incompatible types in assignment

    but the metod is declared like this in the other one:

    Code:
    struct sentencestruct receiveMsgSpeechRecognition() { ... }
    Doesn't it look right..? The sentencestruct now is defined in one header structs.h which is included in both files..

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    46
    Quote Originally Posted by hk_mp5kpdw View Post
    Putting something in a header file means jack if the source file in question does not #include that header file. There is no global header file that is automatically accessed by all the source files. If the struct was defined in global.h then you should have #include'd global.h for the compiler to recognize the struct.
    Thanks alot... But I have had no problems of the global variables.. :S Strange.. But I just included the global.h in all the files and now the other problem dissapered as well..

    Thanks you all.. Great help..

  10. #10
    Registered User
    Join Date
    May 2007
    Posts
    46
    NO.. Maybe I was a litte fast.. The makefile just make it seem it was not there anymore.. It still exists.. :/
    Help will be appresiated..

    btw: All the extern references to the variables in the files i had in the header as it complaint about without extern, should i delete these..?

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Post all your files for this project you're working on as attachments and all the error messages reported by the compiler.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User
    Join Date
    May 2007
    Posts
    46
    Quote Originally Posted by hk_mp5kpdw View Post
    Post all your files for this project you're working on as attachments and all the error messages reported by the compiler.
    It seriously a lot of code.. And alone the two files we talk about is around 3000-4000 lines.. I tries to seperate the problem in two c-files wih only the nessasary methods and their headers and upload them in a couple of minutes..

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    46
    Okay.. Now I have seperated it.. Just run make, and you will see the problem.. EDIT: Cannot upload the makefile, but I uploads it in a second in makefile.h. Just erase the .h and run it.. Orelse the output is

    [...Temp4]# make
    cc -c -o control-module.o control-module.c
    control-module.c: In function `speechrecognitioncontrol':
    control-module.c:126: error: incompatible types in assignment
    control-module.c:132: error: incompatible types in assignment
    make: *** [control-module.o] Error 1

  14. #14
    Registered User
    Join Date
    May 2007
    Posts
    46
    And the makefile..

  15. #15
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, for the code you've provided, I also get undefined function messages for receiveMsgSpeechRecognition and also RecogniseWordInSentence which would account for those errors. If the compiler doesn't understand what those two functions are then it will assume they return an int and then you get the errors because it can't convert an int to type struct sentencestruct. So, where are the prototypes for those two functions located?

    Also, instances of variables should be restricted to the source files and not put into headers like you've done in global.h. Header files should generally contain function prototypes and extern statements and struct definitions while the source file(s) that #include those headers contain the actual instances of any variables.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  2. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM