pointer object of the struct

Question:
In first file i had made a struct and pointer object of that struct.
and now i have to access member of that struct in another file
now please EXPLAIN ME THE MEANING OF
extern st_simple_collection p_simp_collect;
and why we made pointer object in the first file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
In one file following struct code is there:
 
  typedef struct simple_collection {

	uint8_t variable_4;

	uint8_t variable_2;

	
	uint8_t variable_8;

} st_simple_collection, *simple_collection;

and in other file

extern st_simple_collection p_simp_collect; //made a reference

p_simp_collect->variable_8 = 0x00;
The code snipets you showed are invalid. Variable p_simp_collect is declared as having type st_simple_collection that is it is a structure. However further it is used as a pointer

p_simp_collect->variable_8 = 0x00;

The correct syntax would be

p_simp_collect.variable_8 = 0x00;
Last edited on
Are you using C or C++?

If you're using C++, you should define your struct as:
1
2
3
4
struct simple_collection
{
    // fields ...
};


Further more, you shouldn't define data in a header file as that symbol will be declared in every source file that includes that header. You do understand that include files are read by the preprocessor and the compiler just sees one stream of source code.

To use that structure in a source file, you need to include the header.
if i have to access struct through pointer object instead of following way

extern st_simple_collection p_simp_collect; //made a reference

then what would be the syntax in place of above line due which other code remains the same
You have the p_ on the value and not the pointer. That's a little confusion for everyone (mostly yourself). You really should stick to the convention.

Use either:
1
2
extern st_simple_collection p_simp_collect;
p_simp_collect.variable_8 = 0x00;


or more correctly:
1
2
extern st_simple_collection* p_simp_collect;
p_simp_collect->variable_8 = 0x00;
Last edited on
Topic archived. No new replies allowed.