<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
<channel>
  <title>Linux Format forums</title>
  <link>http://linuxformat.com/forums/index.php</link>
  <description>Help, discussion, magazine feedback and more</description>
  <language>english</language>
  <copyright>(c) Copyright Sun May 19, 2013 10:38 pm by Linux Format forums</copyright>
  <managingEditor>webmaster@linuxformat.com</managingEditor>
  <webMaster>webmaster@linuxformat.com</webMaster>
  <pubDate>Sun May 19, 2013 10:38 pm</pubDate>
  <lastBuildDate>Sun May 19, 2013 10:38 pm</lastBuildDate>
  <docs>http://backend.userland.com/rss</docs>
  <generator>phpBB2 RSS Syndication Mod by Lucas</generator>
  <ttl>1</ttl>

  <image>
    <title>Linux Format forums</title>
    <url></url>
    <link>http://linuxformat.com/forums/</link>
    <description>Help, discussion, magazine feedback and more</description>
  </image>

                                      <item>
                                        <title>Re: C Structure multiple files</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=110321#110321</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=66604'&gt;Crispy&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Fri Feb 08, 2013 12:43 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Thank you for your reply einonm.&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Quote:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;quote&quot;&gt;It's virtually contiguous, in that the addresses are a contiguous range. It's not guaranteed to be physically contiguous though - heck, the memory may not even exist yet due to the kernel using something called 'over committing'. There are special ways of asking for physically contiguous memory (e.g. for fast graphics operations). I think at this level, you don't need to worry about it. &lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
Yeah don't think I need to worry about over committing or anything like that, maybe in the future &lt;img src=&quot;images/smiles/icon_biggrin.gif&quot; alt=&quot;Very Happy&quot; border=&quot;0&quot; /&gt;.&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Quote:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;quote&quot;&gt;This code probably won't work. You can only _declare_ variables in header files, you must _define_ a variable in a .c file to use it (and then use the 'extern' keyword in the header to share it). This is because a variable declaration doesn't allocate any memory for the variable, only the variable definition does that.&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
Yeah, sorry I don't think I explained it very well.  Basically in the header file I declare all the data I require for the program.  That data is then initialised in the main file so assume that p was set to be 10 for example before testfnc was called.&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Quote:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;quote&quot;&gt;Er, yes. Allocate once and not many times is better, probably...&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
Yeah I thought it would be, the thing is I've got a loop:&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;for &amp;#40;i = 0; i &amp;lt; 100; i++&amp;#41;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; testfnc&amp;#40; p &amp;#41;;&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
where testfnc then allocates a 1D array of size 40,000, then deallocates before leaving:&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;void testfnc&amp;#40; int j &amp;#41;&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; double *test;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; test = &amp;#40;double *&amp;#41; malloc&amp;#40; 40000 * sizeof&amp;#40;*test&amp;#41; &amp;#41;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; free&amp;#40;test&amp;#41;;&lt;br /&gt;
&amp;#125;&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
The size of the array testfnc allocates doesn't change in the 100 iterations so I thought it might be more efficient.  Perhaps I'll do a test.&lt;br /&gt;
&lt;br /&gt;
Thanks again,&lt;br /&gt;
Chris</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=110321#110321</comments>
                                        <author>Crispy</author>
                                        <pubDate>Fri Feb 08, 2013 12:43 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=110321#110321</guid>
                                      </item>
                                      <item>
                                        <title>Re: C Structure multiple files</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=110320#110320</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=68033'&gt;einonm&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Fri Feb 08, 2013 12:22 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      &lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Crispy wrote:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;quote&quot;&gt;&lt;br /&gt;
Finally, if the function testfnc has to allocate memory (using malloc or calloc), say a large array, and the function testfnc is called from the main file within a for loop, is it more efficient to create a kind of constructor in funcs.c that is called outside the loop and allocate all of the memory (assuming a fixed size) prior to calling testfnc? Rather than allocating and deallocating the memory each time testfnc is called?&lt;br /&gt;
Chris&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
Er, yes. Allocate once and not many times is better, probably...</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=110320#110320</comments>
                                        <author>einonm</author>
                                        <pubDate>Fri Feb 08, 2013 12:22 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=110320#110320</guid>
                                      </item>
                                      <item>
                                        <title>Re: C Structure multiple files</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=110319#110319</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=68033'&gt;einonm&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Fri Feb 08, 2013 12:14 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      &lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Crispy wrote:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;quote&quot;&gt;Thank you for your replies and my apologies for such a delayed response.  Thank you as well for the tips &lt;img src=&quot;images/smiles/icon_smile.gif&quot; alt=&quot;Smile&quot; border=&quot;0&quot; /&gt;.&lt;br /&gt;
&lt;br /&gt;
I have a couple of new questions if that's ok?  If I used malloc to allocate some memory, is that memory contiguous? I think that's the right phrasing - i.e. each element of the array is next to each other?  If so does that still hold for 2D arrays?  If it isn't, would it be more efficient to make it contiguous?&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
It's virtually contiguous, in that the addresses are a contiguous range. It's not guaranteed to be physically contiguous though - heck, the memory may not even exist yet due to the kernel using something called 'over committing'. There are special ways of asking for physically contiguous memory (e.g. for fast graphics operations). I think at this level, you don't need to worry about it.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Crispy wrote:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;quote&quot;&gt;&lt;br /&gt;
My next question is a bit difficult to explain - lets assume I have 3 files.  A main file, main.c, a header file which contains nothing but data, data.h - for example:&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;int p;&lt;br /&gt;
int *ptr;&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
and a file funcs.c which contains a function testfnc (also have funcs.h - only has function prototype).  The main file includes data.h and funcs.h, however funcs.c only includes funcs.h.  The function testfnc is then called from the main file.  Is it more efficient to pass the integer p from data.h to the function testfnc as an argument or include the data.h file in funcs.c? I.e. from the main file: &lt;span style=&quot;font-weight: bold&quot;&gt;testfnc( p );&lt;/span&gt; or &lt;span style=&quot;font-weight: bold&quot;&gt;testfnc();&lt;/span&gt;?&lt;br /&gt;
&lt;br /&gt;
Finally, if the function testfnc has to allocate memory (using malloc or calloc), say a large array, and the function testfnc is called from the main file within a for loop, is it more efficient to create a kind of constructor in funcs.c that is called outside the loop and allocate all of the memory (assuming a fixed size) prior to calling testfnc? Rather than allocating and deallocating the memory each time testfnc is called?&lt;br /&gt;
&lt;br /&gt;
I hope that makes sense, it was kind of difficult to explain.&lt;br /&gt;
&lt;br /&gt;
Thanks in advance,&lt;br /&gt;
Chris&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
This code probably won't work. You can only _declare_ variables in header files, you must _define_ a variable in a .c file to use it (and then use the 'extern' keyword in the header to share it). This is because a variable declaration doesn't allocate any memory for the variable, only the variable definition does that. After the compiler gets it's hands on the code, it doesn't matter where the declarations are - you're still accessing the same memory, regardless of which c file it is in. Obviously passing that variable as a parameter involves another copy, so is less efficient (but more secure and less bug-prone), than a 'global' variable. &lt;br /&gt;
&lt;br /&gt;
Again, for individual variables on a modern computer at this level, you really shouldn't care about these issues. Just write the code to do something useful!&lt;br /&gt;
&lt;br /&gt;
Cheers,&lt;br /&gt;
&lt;br /&gt;
Mark</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=110319#110319</comments>
                                        <author>einonm</author>
                                        <pubDate>Fri Feb 08, 2013 12:14 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=110319#110319</guid>
                                      </item>
                                      <item>
                                        <title>Re: C Structure multiple files</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=110318#110318</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=66604'&gt;Crispy&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Fri Feb 08, 2013 11:57 am&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Thank you for your replies and my apologies for such a delayed response.  Thank you as well for the tips &lt;img src=&quot;images/smiles/icon_smile.gif&quot; alt=&quot;Smile&quot; border=&quot;0&quot; /&gt;.&lt;br /&gt;
&lt;br /&gt;
I have a couple of new questions if that's ok?  If I used malloc to allocate some memory, is that memory contiguous? I think that's the right phrasing - i.e. each element of the array is next to each other?  If so does that still hold for 2D arrays?  If it isn't, would it be more efficient to make it contiguous?&lt;br /&gt;
&lt;br /&gt;
My next question is a bit difficult to explain - lets assume I have 3 files.  A main file, main.c, a header file which contains nothing but data, data.h - for example:&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;int p;&lt;br /&gt;
int *ptr;&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
and a file funcs.c which contains a function testfnc (also have funcs.h - only has function prototype).  The main file includes data.h and funcs.h, however funcs.c only includes funcs.h.  The function testfnc is then called from the main file.  Is it more efficient to pass the integer p from data.h to the function testfnc as an argument or include the data.h file in funcs.c? I.e. from the main file: &lt;span style=&quot;font-weight: bold&quot;&gt;testfnc( p );&lt;/span&gt; or &lt;span style=&quot;font-weight: bold&quot;&gt;testfnc();&lt;/span&gt;?&lt;br /&gt;
&lt;br /&gt;
Finally, if the function testfnc has to allocate memory (using malloc or calloc), say a large array, and the function testfnc is called from the main file within a for loop, is it more efficient to create a kind of constructor in funcs.c that is called outside the loop and allocate all of the memory (assuming a fixed size) prior to calling testfnc? Rather than allocating and deallocating the memory each time testfnc is called?&lt;br /&gt;
&lt;br /&gt;
I hope that makes sense, it was kind of difficult to explain.&lt;br /&gt;
&lt;br /&gt;
Thanks in advance,&lt;br /&gt;
Chris</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=110318#110318</comments>
                                        <author>Crispy</author>
                                        <pubDate>Fri Feb 08, 2013 11:57 am</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=110318#110318</guid>
                                      </item>
                                      <item>
                                        <title>Re: C Structure multiple files</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=108676#108676</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=68033'&gt;einonm&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Wed Nov 14, 2012 12:26 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      &lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;CJLL wrote:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;quote&quot;&gt;&lt;br /&gt;
The keyword struct tells the compiler that you are defining a new data type, and shouldn't be used in a function parameter list. &lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
Yes, the compiler thinks that a new struct is being defined, only because it can't find the definition elsewhere. Putting the struct definition in the header file above the function declaration would solve this.&lt;br /&gt;
&lt;br /&gt;
Using the keyword 'struct' in a function parameter list is the correct thing to do, the only way to get around this is to typedef the struct, which I wouldn't recommend doing - so the function declaration is all good, just missing the struct definition.</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=108676#108676</comments>
                                        <author>einonm</author>
                                        <pubDate>Wed Nov 14, 2012 12:26 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=108676#108676</guid>
                                      </item>
                                      <item>
                                        <title>Re: C Structure multiple files</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=108674#108674</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=68033'&gt;einonm&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Wed Nov 14, 2012 12:22 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      &lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Crispy wrote:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;quote&quot;&gt;I figured out a way around it, place all typedefs in a header file then include that header file in anything that requires those typedefs.&lt;br /&gt;
&lt;br /&gt;
The #ifndef directive is to prevent multiple definition as the file will be included in lots of different source files.  I am curious as to whether there is another way to do it, anyone have any ideas?&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
Hi Chris,&lt;br /&gt;
&lt;br /&gt;
You've discovered correctly that structures shared between many source files need to be placed in a header file and #included in those source files. This is the right thing to do.&lt;br /&gt;
&lt;br /&gt;
A few other points are:&lt;br /&gt;
&lt;br /&gt;
1. You don't need to typedef the struct, just putting the struct definition into the header file will be fine - and preferable, as it doesn't hide the fact that it's a struct (as opposed to an enum or int, for example). &lt;br /&gt;
&lt;br /&gt;
2. If you #include the header file containing the struct definition, there is no need to declare it as extern, just use it. Try and avoid using extern if you can.</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=108674#108674</comments>
                                        <author>einonm</author>
                                        <pubDate>Wed Nov 14, 2012 12:22 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=108674#108674</guid>
                                      </item>
                                      <item>
                                        <title>Re: C Structure multiple files</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=108647#108647</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=2007'&gt;CJLL&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Sun Nov 11, 2012 5:09 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      &lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Crispy wrote:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;quote&quot;&gt;Hi,&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
int test_struct_fnc&amp;#40; struct test *t__i &amp;#41;&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
The keyword struct tells the compiler that you are defining a new data type, and shouldn't be used in a function parameter list. &lt;img src=&quot;images/smiles/icon_rolleyes.gif&quot; alt=&quot;Rolling Eyes&quot; border=&quot;0&quot; /&gt;</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=108647#108647</comments>
                                        <author>CJLL</author>
                                        <pubDate>Sun Nov 11, 2012 5:09 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=108647#108647</guid>
                                      </item>
                                      <item>
                                        <title>Re: C Structure multiple files</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=108570#108570</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=66604'&gt;Crispy&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Wed Oct 31, 2012 3:30 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      I figured out a way around it, place all typedefs in a header file then include that header file in anything that requires those typedefs. For example:&lt;br /&gt;
&lt;br /&gt;
typedefs.h&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
#ifndef TEST_H_FILE&lt;br /&gt;
#define TEST_H_FILE&lt;br /&gt;
&lt;br /&gt;
typedef struct testy&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&amp;nbsp; int t1;&lt;br /&gt;
&amp;nbsp; int t2;&lt;br /&gt;
&amp;#125; test;&lt;br /&gt;
&lt;br /&gt;
#endif&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
The #ifndef directive is to prevent multiple definition as the file will be included in lots of different source files.  I am curious as to whether there is another way to do it, anyone have any ideas?&lt;br /&gt;
&lt;br /&gt;
Thanks,&lt;br /&gt;
Chris</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=108570#108570</comments>
                                        <author>Crispy</author>
                                        <pubDate>Wed Oct 31, 2012 3:30 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=108570#108570</guid>
                                      </item>
                                      <item>
                                        <title>C Structure multiple files</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=108566#108566</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=66604'&gt;Crispy&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Wed Oct 31, 2012 12:58 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Hi,&lt;br /&gt;
&lt;br /&gt;
I have been playing around a little bit with C but have come across a problem which I can't seem to get my head around.  I have a main file which contains a structure:&lt;br /&gt;
&lt;br /&gt;
main.c&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;test.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
struct test&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&amp;nbsp; int t1;&lt;br /&gt;
&amp;nbsp; int t2;&lt;br /&gt;
&amp;#125;;&lt;br /&gt;
&lt;br /&gt;
static struct test t;&lt;br /&gt;
&lt;br /&gt;
int main&amp;#40; int argc, char **argv &amp;#41;&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&amp;nbsp; int err = 0;&lt;br /&gt;
&amp;nbsp; t.t1 = 0;&lt;br /&gt;
&amp;nbsp; t.t2 = 0;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; err = test_struct_fnc&amp;#40; &amp;amp;t &amp;#41;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; printf&amp;#40;&amp;quot;t.t1 = %d\n&amp;quot;, t.t1&amp;#41;;&lt;br /&gt;
&amp;nbsp; printf&amp;#40;&amp;quot;t.t2 = %d\n&amp;quot;, t.t2&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; return 0;&lt;br /&gt;
&amp;#125;&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
It includes the file test.h which simply contains a function prototype:&lt;br /&gt;
&lt;br /&gt;
test.h&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
int test_struct_fnc&amp;#40; struct test *t__i &amp;#41;;&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
This function is then found in test.c:&lt;br /&gt;
&lt;br /&gt;
test.c&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
int test_struct_fnc&amp;#40; struct test *t__i &amp;#41;&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&amp;nbsp; t__i-&amp;gt;t1 = 10;&lt;br /&gt;
&amp;nbsp; t__i-&amp;gt;t2 = 100;&lt;br /&gt;
&amp;#125;&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
which simply changes the values of t1 and t2 to 10 and 100 respectively.  However, when I compile:&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
gcc -g -c test.c&lt;br /&gt;
gcc -g -c main.c&lt;br /&gt;
gcc -g -o test.exe test.o main.o&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
I get an error from test.c saying:&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
test.c&amp;#58;1&amp;#58; warning&amp;#58; ‘struct test’ declared inside parameter list&lt;br /&gt;
test.c&amp;#58;1&amp;#58; warning&amp;#58; its scope is only this definition or declaration, which is probably not what you want&lt;br /&gt;
test.c&amp;#58; In function ‘test_struct_fnc’&amp;#58;&lt;br /&gt;
test.c&amp;#58;3&amp;#58; error&amp;#58; dereferencing pointer to incomplete type&lt;br /&gt;
test.c&amp;#58;4&amp;#58; error&amp;#58; dereferencing pointer to incomplete type&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
I think this is because the structure test is declared in main not in test.c.  However declaring the structure to be external - extern struct test; - in test.h doesn't work it still gives the same error.&lt;br /&gt;
&lt;br /&gt;
I thought that perhaps changing struct test to typedef struct test would help, however I can't figure out how to share the type definition across the multiple files?  I tried&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&amp;nbsp;extern typedef test; &lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
In the header file test.h however that gave an error saying it expected a ) before * token in test.c - which makes no sense to me.  I just can't figure out what I'm supposed to do here so any help would be greatly appreciated.&lt;br /&gt;
&lt;br /&gt;
Thanks in advance,&lt;br /&gt;
Chris</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=108566#108566</comments>
                                        <author>Crispy</author>
                                        <pubDate>Wed Oct 31, 2012 12:58 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=108566#108566</guid>
                                      </item></channel></rss>