<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Initialising Structures</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/14293/initialising-structures</link><description> Hello, 
 
I am using the PK51 package. My program requires a structure of arrays. The following files are used:- 
1) declare.c For defining the global variables 
2) declare.h Containing the extern definations of all the variables declared in declare</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Initialising Structures</title><link>https://community.arm.com/thread/53756?ContentTypeID=1</link><pubDate>Thu, 28 Jun 2001 21:34:24 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:fe1648db-462c-458c-81ef-07ec30ee08d9</guid><dc:creator>Mark Odell</dc:creator><description>&lt;p&gt;No don&amp;#39;t. Do it the C way:&lt;br /&gt;
&lt;pre&gt;/* NastyGlobals.c */
#include &amp;quot;NastyGlobals.h&amp;quot;

/* Actually define storage */
int g_yuckyOne;
int g_yuckyTwo[100];
struct Thing g_thing;

/* NastyGlobals.h */
#ifndef NastyGlobals_h_Included
#define NastyGlobals_h_Included

/* Declare objects to other modules */
struct Thing {
    char nameStr[128];
    int  id;
};
extern int g_yuckyOne;
extern int g_yuckyTwo[];
extern struct Thing g_thing;

#endif&lt;/pre&gt;
&lt;br /&gt;
Now include NastyGlobals.h where needed and simply add NastyGlobals.obj to your linker line.&lt;br /&gt;
&lt;br /&gt;
- Mark&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Initialising Structures</title><link>https://community.arm.com/thread/94808?ContentTypeID=1</link><pubDate>Thu, 28 Jun 2001 17:29:01 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:7f57016f-657a-462e-b539-0d35820f02af</guid><dc:creator>Andrew Neil</dc:creator><description>&lt;p&gt;There is nothing wrong with having &lt;br /&gt;
&lt;pre&gt;// &lt;b&gt;header.h&lt;/b&gt;
extern int i;&lt;/pre&gt;
&lt;pre&gt;// &lt;b&gt;file.c&lt;/b&gt;
#include &amp;quot;header.h&amp;quot; // Contains an extern &lt;i&gt;declaration&lt;/i&gt; of &amp;#39;&lt;b&gt;i&lt;/b&gt;&amp;#39;
int i;              // &lt;i&gt;definition&lt;/i&gt; of &amp;#39;&lt;b&gt;i&lt;/b&gt;&amp;#39;&lt;/pre&gt;
In fact, this has the positive &lt;b&gt;advantage&lt;/b&gt; of ensuring that the &lt;i&gt;declaration&lt;/i&gt; in &lt;b&gt;header.h&lt;/b&gt; is consistent with the &lt;i&gt;definition&lt;/i&gt; in &lt;b&gt;file.c&lt;/b&gt;!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Initialising Structures</title><link>https://community.arm.com/thread/37653?ContentTypeID=1</link><pubDate>Thu, 28 Jun 2001 17:11:59 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:5d29f597-e417-424e-8c79-6d62da09ab19</guid><dc:creator>Andrew Neil</dc:creator><description>&lt;p&gt;Go back to my original reply!&lt;br /&gt;
&lt;br /&gt;
You have definitions of &amp;#39;struct param&amp;#39; in &lt;b&gt;both&lt;/b&gt; declare.c &lt;b&gt;and&lt;/b&gt; declare.h - this is bad: you have no guarantee that they&amp;#39;re the same! a simple typo in one will cause you all sorts of grief elsewhere!&lt;br /&gt;
Put the &amp;#39;struct param&amp;#39; definition in &lt;b&gt;one&lt;/b&gt; place &lt;b&gt;only&lt;/b&gt;, and &lt;b&gt;always&lt;/b&gt; use that one definition. Thus the definition should be in declare.h, and &lt;b&gt;anything&lt;/b&gt; which needs to refer to &amp;#39;struct param&amp;#39; should #include declare.h.&lt;br /&gt;
Remove the &amp;#39;struct param&amp;#39; definition from declare.c&lt;br /&gt;
&lt;br /&gt;
Strictly, your files should be called &amp;quot;declare.h&amp;quot; and &amp;quot;define.c,&amp;quot; since the Header (&lt;b&gt;.h&lt;/b&gt;) file contains variable &lt;i&gt;declarations,&lt;/i&gt; whilst the &lt;b&gt;.c&lt;/b&gt; file contains variable &lt;i&gt;definitions&lt;/i&gt;.&lt;br /&gt;
&lt;br /&gt;
You should #define your array sizes, rather than use &amp;quot;magic numbers&amp;quot; and rely on remembering to subtract 1 where required.&lt;br /&gt;
&lt;pre&gt;//&lt;b&gt;declare.h&lt;/b&gt;
#define SIZE 10
struct param 
{
   float value[SIZE];
   char  mode [SIZE];
};

extern struct param a; 
extern int          i;&lt;/pre&gt;
&lt;pre&gt;//&lt;b&gt;define.c&lt;/b&gt;
#include &amp;quot;declare.h&amp;quot;
struct param a;
int          i; // Why is this global, as it&amp;#39;s only used for a loop counter?&lt;/pre&gt;
&lt;pre&gt;//&lt;b&gt;main.c&lt;/b&gt;
#include &amp;quot;declare.h&amp;quot;
main()
{
   i = 0; // Unnecessary, as the initstruct() loop initialises i
   initstruct(); // Wot? No prototype!
}&lt;/pre&gt;
&lt;pre&gt;//&lt;b&gt;sub.c&lt;/b&gt;
#include &amp;quot;declare.h&amp;quot;
void initstruct()
{
   for (i=0; i&amp;lt;SIZE; i++)
   {
      a.value[i] = 2.5;
      a.mode [i] = 1;
   }
}&lt;/pre&gt;
&lt;br /&gt;
When you #include one file within another, it is as if you had &amp;quot;cut&amp;quot; the entire text of the #included file and &amp;quot;pasted&amp;quot; it into the including file.&lt;br /&gt;
The Debug information produced by the compiler does &lt;b&gt;not&lt;/b&gt; include file references with its line number information - it just assumes that a line number reference in x&lt;b&gt;.obj&lt;/b&gt; refers to the source file x.&lt;b&gt;c&lt;/b&gt;; thus you will  obviously not be able to debug properly when one &lt;b&gt;.c&lt;/b&gt; file includes another &lt;b&gt;.c&lt;/b&gt; file!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Initialising Structures</title><link>https://community.arm.com/thread/53755?ContentTypeID=1</link><pubDate>Thu, 28 Jun 2001 15:17:45 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ffc28828-9ddc-4dd3-aa2f-384e1fd45cb6</guid><dc:creator>Jon Ward</dc:creator><description>&lt;p&gt;Or, take a look at the following URL:&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.keil.com/support/docs/1868.htm"&gt;http://www.keil.com/support/docs/1868.htm&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Jon&lt;/b&gt;&lt;br /&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Initialising Structures</title><link>https://community.arm.com/thread/37654?ContentTypeID=1</link><pubDate>Thu, 28 Jun 2001 14:59:18 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:98fcb761-7584-46a7-a82b-7def0eb0f180</guid><dc:creator>Jon Young</dc:creator><description>&lt;p&gt;If your style is to place all globals in one large declare.h file.  Try this approach.&lt;br /&gt;
&lt;br /&gt;
In the Main.c add:&lt;br /&gt;
&lt;pre&gt;
#define DECLARE
#include &amp;quot;declare.h&amp;quot;
&lt;/pre&gt;
In all other .c files add:&lt;br /&gt;
&lt;pre&gt;
#include &amp;quot;declare.h&amp;quot;
&lt;/pre&gt;
In the Declare.h file add the EXTERN macro:&lt;br /&gt;
&lt;pre&gt;
//Declare.h

#ifdef DECLARE
  #define EXTERN
#else
  #define EXTERN extern
#endif

struct param {
  float value[10];
  char mode[10];
};

EXTERN struct param a; 
EXTERN int i;
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>