Robby
2009-09-15 21:01:02 UTC
Hello,
Okay, I know that defining structures globally is not a good programming
habit and boy did I learn that one from you guys. So we should always typedef
the structs so that we are able to reuse them whereever we need them.
Simply put... so doing stuff like this is okay! right?
====================================.h
typedef struct tag_ts
{
int on_activation_state_1;
long ts_area_1;
long relevant_ctrl_msg_1;
}ts, *p_ts;
======================================.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
ts t;
p_ts pts;
pts = NULL;
t.on_activation_state_1 = 10;
pts->on_activation_state_1 = 11;
return 0;
}
===================================
But suppose I just need a structure to hold some data globally and I know I
will never ever need more than one instance. It will simply reside globally
and will be accessed whenever required by other functions. Now in my sample I
did not show functions accessing the data, but you know what I mean.
Is it acceptable to just keep a structure holding specific reusable
informations like this:
==================================.h
struct tag_ts
{
int on_activation_state_1;
long ts_area_1;
long relevant_ctrl_msg_1;
}ts, *p_ts;
====================================.c
int main()
{
p_ts = &ts;
p_ts->on_activation_state_1 = 11;
return 0;
}
====================================
The reason I am asking is I know we went through alot of this but always
within the typedefing of structures concept....Now, I find it sort wastefull
to typedef a structure that its only purpose is to hold one set of
informations which will be read and written too so its information are
globally available from *all* functions without the necessity of passing its
pointer around from one function to another.
I could use global variables, but prefer a structure.
Thanks for all your feedback!
Okay, I know that defining structures globally is not a good programming
habit and boy did I learn that one from you guys. So we should always typedef
the structs so that we are able to reuse them whereever we need them.
Simply put... so doing stuff like this is okay! right?
====================================.h
typedef struct tag_ts
{
int on_activation_state_1;
long ts_area_1;
long relevant_ctrl_msg_1;
}ts, *p_ts;
======================================.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
ts t;
p_ts pts;
pts = NULL;
t.on_activation_state_1 = 10;
pts->on_activation_state_1 = 11;
return 0;
}
===================================
But suppose I just need a structure to hold some data globally and I know I
will never ever need more than one instance. It will simply reside globally
and will be accessed whenever required by other functions. Now in my sample I
did not show functions accessing the data, but you know what I mean.
Is it acceptable to just keep a structure holding specific reusable
informations like this:
==================================.h
struct tag_ts
{
int on_activation_state_1;
long ts_area_1;
long relevant_ctrl_msg_1;
}ts, *p_ts;
====================================.c
int main()
{
p_ts = &ts;
p_ts->on_activation_state_1 = 11;
return 0;
}
====================================
The reason I am asking is I know we went through alot of this but always
within the typedefing of structures concept....Now, I find it sort wastefull
to typedef a structure that its only purpose is to hold one set of
informations which will be read and written too so its information are
globally available from *all* functions without the necessity of passing its
pointer around from one function to another.
I could use global variables, but prefer a structure.
Thanks for all your feedback!
--
Best regards
Roberto
Best regards
Roberto