Find the answer to your Linux question:
Results 1 to 4 of 4
Please can someone help with my understanding of strucutre declaration and definition. In global.h I am declaring:- struct VariableMap{ void *ptr2var; char label[17]; } ; In myfile.c /* ....... Include ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2009
    Location
    On the bay, Morecambe Bay.
    Posts
    15

    structure member definition


    Please can someone help with my understanding of strucutre declaration and definition.

    In global.h I am declaring:-
    struct VariableMap{
    void *ptr2var;
    char label[17];
    } ;

    In myfile.c
    /* ....... Include Files ....... */
    #include "globals.h"

    int ADC_addr = 0;
    . . .
    struct VariableMap a2daddr;
    a2daddr.ptr2var = (void *)&ADC_addr;
    strcpy(a2daddr.label,"name");
    /* ------------------------------------------------ */

    int main( int argc, char *argv[])
    {
    . . .
    }

    Running this through gcc gives the error
    myfile.c:#: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token

    If I rearrange the code to:-
    /* ....... Include Files ....... */
    #include "globals.h"

    int ADC_addr = 0;
    . . .
    struct VariableMap a2daddr;
    /* ------------------------------------------------ */

    int main( int argc, char *argv[])
    {
    . . .
    a2daddr.ptr2var = (void *)&ADC_addr;
    strcpy(a2daddr.label,"name");

    }

    This compiles with no errors. Please can someone explain why?

    Thanks in anticipation

  2. #2
    Just Joined! natediddy's Avatar
    Join Date
    Oct 2010
    Location
    Ky
    Posts
    6
    It's because they're outside of a function

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,709
    You call strcpy() outside of main() in your first example. This is not valid code. It has to be within a function, such as main() or something else.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Jan 2009
    Location
    On the bay, Morecambe Bay.
    Posts
    15

    structure member definition

    Thanks for the advice about calling functions outside of main(), however, that is not the problem. I should have been more specific about the line where the error is reported.

    If I declare the structure in globals .h as
    struct VariableMap{
    void *ptr2var;
    char label[17];
    } ;
    I have created a 'template' for strucutre of the form VariableMap with scope for the whole of any source file where the header is #included, correct?

    So, if I then declare a structured variable of the form VariableMap within that scope e.g.
    struct VariableMap a2daddr;

    Is it not than acceptable to complete the definition of the strucutre's individual variables by assigning values within scope?
    i.e. it is legal to globally declare and define
    int ADC_addr = 0;
    but not
    a2daddr.ptr2var = (void *)&ADC_addr; //error line
    If this definition is outside of main() I get the error
    myfile.c:#: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token

    The application I am building is for an embedded controller with data acquisituion and machine control, where I require many global variables. One of my targets is to make provision for initialising many of these variables from text based config files. Hence the need to fully understand the rules for declaring, defining and assigning all variable types.
    Constructive help and suggestions much appreciated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •