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 ...
- 07-01-2011 #1Just 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
- 07-02-2011 #2
- 07-02-2011 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
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!
- 07-04-2011 #4Just 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.


Reply With Quote