<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/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/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Keil can&amp;#39;t recognize next struct</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/93463/keil-can-t-recognize-next-struct</link><description>Hi, 
 
 I am currently developing a project using ultrasonic sensor for measuring level. So I was migrating library from development kit to nrf sdk code. I am using Keil uVision to develop the code. 
 So in my case, there are 2 structs that connected</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 03 Nov 2022 08:52:06 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/93463/keil-can-t-recognize-next-struct" /><item><title>RE: Keil can't recognize next struct</title><link>https://devzone.nordicsemi.com/thread/393807?ContentTypeID=1</link><pubDate>Thu, 03 Nov 2022 08:52:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:abda908b-49ba-4f4e-ba9e-f3830292e8d8</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;/p&gt;
&lt;p&gt;To access the same variable from multiple files you need to use the extern keyword.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;To do this you need to declare the variable in a header file, using extern:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;extern ch201_device my_device;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Then in one of your C files you need to initialize it:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;ch201_device my_device = {..};&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And from now on you can access the variable in any file that includes the header file, and access it directly.&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Keil can't recognize next struct</title><link>https://devzone.nordicsemi.com/thread/393794?ContentTypeID=1</link><pubDate>Thu, 03 Nov 2022 08:01:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b23d9e10-63ea-4eab-9257-0b6114a3da58</guid><dc:creator>azwawe</dc:creator><description>&lt;p&gt;Hi, thank you for response. Thanks for confirming that, but I mean call it on another header at function declaration.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I think it is really weird. Hopefully you can double check my ticket here&amp;nbsp;&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/93500/can-t-call-struct-on-another-file-or-call-the-function-in-main-code"&gt;Can&amp;#39;t call struct on another file or call the function in main code&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thank you.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Keil can't recognize next struct</title><link>https://devzone.nordicsemi.com/thread/393786?ContentTypeID=1</link><pubDate>Thu, 03 Nov 2022 07:29:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bc00225c-ca22-4cb2-b7fe-664f69044a2a</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Since you&amp;#39;ve used a typedef you can now use your struct as a type (similar to int, char etc).&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In order to use your struct you typically need to instantiate and initialize it somewhere, like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;// Create an instance of the ch201_device struct called my_device
ch201_device my_device;

// You can also populate the fields of the struct during initialization
ch201_device my_device = {
    .oversample = 10,
    .fw_version_string = &amp;quot;Version 1&amp;quot;};&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;To access the fields just use the . operator:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;my_device.oversample = 12;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;To provide a pointer to the struct you use the &amp;amp; operator.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Whenever you want to access the fields of a struct through a pointer rather than directly through the instance you have to use the -&amp;gt; operator instead of the . operator.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;ch201_device *my_device_pointer = &amp;amp;my_device;
my_device_pointer-&amp;gt;oversample = 0;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;For a good understanding of the C programming language I would recommend getting The C Programming Language, by Kernighan and Ritchie, widely considered the gold standard of C books.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Keil can't recognize next struct</title><link>https://devzone.nordicsemi.com/thread/393695?ContentTypeID=1</link><pubDate>Wed, 02 Nov 2022 14:23:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f2dd84c1-e95c-475e-b865-4413a4645fb3</guid><dc:creator>azwawe</dc:creator><description>&lt;p&gt;great, it works properly. Anyway, if I have another header file in same folder.&lt;/p&gt;
&lt;p&gt;And I have method that use ch201_device struct, how do I call the struct? sorry for a basic question. Thank you.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I already put it in header file #include &amp;quot;ch201.h&amp;quot; (file that contains typedef struct ch201_device), but still undefined. Thank you.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Keil can't recognize next struct</title><link>https://devzone.nordicsemi.com/thread/393685?ContentTypeID=1</link><pubDate>Wed, 02 Nov 2022 13:48:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:944e26fc-809a-41b8-8a1e-49ad18c65f81</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In the case where you have circular dependencies between structs you&amp;nbsp;can use a forward declaration, where you declare the name of the struct first, then you define the second struct which references the first, and then you declare the first struct, which references the second.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you write the following above your typedefs I believe it should work:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;typedef struct ch201_device;

// Typedefs

// First struct

// Second struct&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;If it still doesn&amp;#39;t work please let me know, and I will fire up Keil and try it out myself &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>