This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Extern struct

Hi all:

I am trying to use data from a structure in different modules ( extern struct), with no success. Here's what I am doing:

main.c:

#include "main.h"
struct my_struct new_struct;

void main(void)
{
   new_struct.a = 4321;
}

file1.c:

#include "main.h"
extern my_struct new_struct;


void test()
{
    new_struct.a = 1234;
}

main.h:


struct my_struct
{
    int a;
    int b;
}


The compiler tells me that left side of . requires struct or union....
As I have followed previous threads about this, I see this guidelines are correct. What can be wrong here ?
I am using c51 ver 8.12

Thanks in advance

Juan


Parents
    1. With new_struct's declaration in file1.c lacking the keyword 'struct', it is not a structure declaration, it's an 'int' declaration.
    2. C51 produced an error message telling you what types the '.' operator works with (none are 'int').
    3. You used Visual C++ to compile your example without error.
    4. Successful compilation with Visual C++ led you to believe the problem was C51's bug rather than it a problem with your declaration.

    Some observations:

    • There is no setting to have C51 accept valid C, but C that you did not intend.
    • Certainly it would be less effort to have carefully examined the error message and correct the declaration by inserting "struct" between "extern" and "my_struct" than to try to find a means of having C51 change its concept of standard C's "default int" behavior.
    • If you are going to use a C++ compiler to validate C code, you should be investigating options for it, not C51 -- options that put in a strictly C-conforming mode.
    • Other options include using lint.

    For example, I've pasted the following into Gimpel's online lint gimpel-online.com/.../genPage.py :

    struct my_struct
    {
        int a;
        int b;
    };
    
    extern my_struct new_struct;
    
    void test()
    {
        new_struct.a = 1234;
    }
    

    With these results:

    FlexeLint for C/C++ (Unix) Vers. 9.00c, Copyright Gimpel Software 1985-2009
    
    --- Module:   diy.c (C)
                     _
    extern my_struct new_struct;
    diy.c  10  Info 808: No explicit type given symbol 'my_struct',
        int assumed
    diy.c  10  Error 10: Expecting ';'
    diy.c  10  Error 19: Useless Declaration
        _
        new_struct.a = 1234;
    diy.c  14  Error 40: Undeclared identifier 'new_struct'
    diy.c  14  Error 10: Expecting a structure or union
    diy.c  14  Error 40: Undeclared identifier 'a'
    diy.c  14  Error 63: Expected an lvalue
    
        --- Wrap-up for Module: diy.c
    
    Info 753: local struct 'my_struct' (line 4, file diy.c) not
        referenced
    diy.c  4  Info 830: Location cited in prior message
    Info 754: local structure member 'my_struct::a' (line 6, file
        diy.c) not referenced
    diy.c  6  Info 830: Location cited in prior message
    Info 754: local structure member 'my_struct::b' (line 7, file
        diy.c) not referenced
    diy.c  7  Info 830: Location cited in prior message
    
    --- Global Wrap-up
    
    Info 714: Symbol 'test(void)' (line 12, file diy.c) not
        referenced
    diy.c  12  Info 830: Location cited in prior message
    

Reply
    1. With new_struct's declaration in file1.c lacking the keyword 'struct', it is not a structure declaration, it's an 'int' declaration.
    2. C51 produced an error message telling you what types the '.' operator works with (none are 'int').
    3. You used Visual C++ to compile your example without error.
    4. Successful compilation with Visual C++ led you to believe the problem was C51's bug rather than it a problem with your declaration.

    Some observations:

    • There is no setting to have C51 accept valid C, but C that you did not intend.
    • Certainly it would be less effort to have carefully examined the error message and correct the declaration by inserting "struct" between "extern" and "my_struct" than to try to find a means of having C51 change its concept of standard C's "default int" behavior.
    • If you are going to use a C++ compiler to validate C code, you should be investigating options for it, not C51 -- options that put in a strictly C-conforming mode.
    • Other options include using lint.

    For example, I've pasted the following into Gimpel's online lint gimpel-online.com/.../genPage.py :

    struct my_struct
    {
        int a;
        int b;
    };
    
    extern my_struct new_struct;
    
    void test()
    {
        new_struct.a = 1234;
    }
    

    With these results:

    FlexeLint for C/C++ (Unix) Vers. 9.00c, Copyright Gimpel Software 1985-2009
    
    --- Module:   diy.c (C)
                     _
    extern my_struct new_struct;
    diy.c  10  Info 808: No explicit type given symbol 'my_struct',
        int assumed
    diy.c  10  Error 10: Expecting ';'
    diy.c  10  Error 19: Useless Declaration
        _
        new_struct.a = 1234;
    diy.c  14  Error 40: Undeclared identifier 'new_struct'
    diy.c  14  Error 10: Expecting a structure or union
    diy.c  14  Error 40: Undeclared identifier 'a'
    diy.c  14  Error 63: Expected an lvalue
    
        --- Wrap-up for Module: diy.c
    
    Info 753: local struct 'my_struct' (line 4, file diy.c) not
        referenced
    diy.c  4  Info 830: Location cited in prior message
    Info 754: local structure member 'my_struct::a' (line 6, file
        diy.c) not referenced
    diy.c  6  Info 830: Location cited in prior message
    Info 754: local structure member 'my_struct::b' (line 7, file
        diy.c) not referenced
    diy.c  7  Info 830: Location cited in prior message
    
    --- Global Wrap-up
    
    Info 714: Symbol 'test(void)' (line 12, file diy.c) not
        referenced
    diy.c  12  Info 830: Location cited in prior message
    

Children
No data