<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to how to declare an array size without constant</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/065a694f1b/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/065a694f1b/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/065a694f1b/</id><updated>2020-02-27T14:04:27.892000Z</updated><subtitle>Recent posts to how to declare an array size without constant</subtitle><entry><title>how to declare an array size without constant</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/065a694f1b/?limit=25#0caa" rel="alternate"/><published>2020-02-27T14:04:27.892000Z</published><updated>2020-02-27T14:04:27.892000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.com2278377777e2b2e7620a33f8e25e02e059e12076</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; barkamharikrishna&lt;/p&gt;
&lt;p&gt;thank you so much&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>how to declare an array size without constant</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/065a694f1b/?limit=25#5838" rel="alternate"/><published>2020-02-27T14:04:27.537000Z</published><updated>2020-02-27T14:04:27.537000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.coma0739f48f2b36025383b014e357c1b7507855ed6</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; scott_cunningham&lt;/p&gt;
&lt;p&gt;In general, array size is not dynamic. With the latest CoDeSys releases, you can now dynamically extend memory and arrays as suggested above. Be careful with this - dynamic memory allocation can cause problems - especially when machines are running... In fact, the programming standards offered by the PLCipen group even recommends not doing it at all. &lt;/p&gt;
&lt;p&gt;In your problem, I would either figure out how to not need a variable array size or switch to a POINTER TO BYTE, DataSize and NumberElements for your three inputs. I use this solution for generic ring buffers. However, pointer math now falls on you to get it right. &lt;/p&gt;
&lt;p&gt;Some people (including myself) will tell you to avoid pointers as they are the biggest cause of strange bugs. But casting arrays and pointers to arrays can be just as bad. (This is why I suggested finding a different way that doesn't use an array).  Sometimes a diffferent oath simplifies life. Perhaps just burn memory and define the largest array you think you will need.&lt;/p&gt;
&lt;p&gt;None of these are perfect answers and do not show the "flexibility" of a C# or Java or Python or whatever, but we are programming machines, and when code breaks, people fly on Friday nights to fix it!&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>how to declare an array size without constant</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/065a694f1b/?limit=25#3f85" rel="alternate"/><published>2020-02-27T14:04:27.174000Z</published><updated>2020-02-27T14:04:27.174000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.com5cfe5ed3eb5341415c436095addd48f7e648d484</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; barkamharikrishna&lt;/p&gt;
&lt;p&gt;hello rjafrate ,&lt;br/&gt;
Can you please give Little more Information please.&lt;/p&gt;
&lt;p&gt;Regards&lt;br/&gt;
Harikrishna&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>how to declare an array size without constant</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/065a694f1b/?limit=25#aaf8" rel="alternate"/><published>2020-02-27T14:04:26.275000Z</published><updated>2020-02-27T14:04:26.275000Z</updated><author><name>rickj</name><uri>https://forge.codesys.com/u/rickj/</uri></author><id>https://forge.codesys.com07869d3d5f4e8b59bc40ecdca49ac51685737ea0</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;I think yannickasselin1 has the best answer.  The only alternative I know for making a FB that can operate on arrays of different sizes is to pass a pointer to the array and size to the function block as shown below.    &lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;VAR_INPUT&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;maxsize&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;as16_ColorChange&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;POINTER&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="nb"&gt;ARRAY&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;32767&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;OF&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;END_VAR&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;To use a symbolic constant instead of a literal constant yoy can do the following.  However this doesn't help to make a FB that can operate on different size arrays. &lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;VAR_CONSTANT&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;maxsize&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;INT&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="nt"&gt;100&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;END_VAR&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;VAR_INPUT&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;as16_ColorChange&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;ARRAY&lt;/span&gt; &lt;span class="cp"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="bp"&gt;..&lt;/span&gt;&lt;span class="nx nx-Member"&gt;maxsize&lt;/span&gt;&lt;span class="cp"&gt;]&lt;/span&gt; &lt;span class="nt"&gt;OF&lt;/span&gt; &lt;span class="nt"&gt;INT&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;END_VAR&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;</summary></entry><entry><title>how to declare an array size without constant</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/065a694f1b/?limit=25#af5b" rel="alternate"/><published>2020-02-27T14:04:25.992000Z</published><updated>2020-02-27T14:04:25.992000Z</updated><author><name>learnetk</name><uri>https://forge.codesys.com/u/learnetk/</uri></author><id>https://forge.codesys.come72a7a19658352a02141a33516cd2364ceeafd8e</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi,&lt;br/&gt;
yannickasselin1 is right but,&lt;br/&gt;
i think this Feature is only available in the newest Version of Codesys(Sp9).&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>how to declare an array size without constant</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/065a694f1b/?limit=25#1384" rel="alternate"/><published>2020-02-27T14:04:25.694000Z</published><updated>2020-02-27T14:04:25.694000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.comdc74b0f09c961ec1a14dc72f3b8262cec1002506</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; barkamharikrishna&lt;/p&gt;
&lt;p&gt;hi yannickasselin1,&lt;br/&gt;
thanks for the reply&lt;br/&gt;
can please send an example program if you have&lt;/p&gt;
&lt;p&gt;Regards&lt;br/&gt;
Harikrishna&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>how to declare an array size without constant</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/065a694f1b/?limit=25#746b" rel="alternate"/><published>2020-02-27T14:04:25.408000Z</published><updated>2020-02-27T14:04:25.408000Z</updated><author><name>yannickasselin1</name><uri>https://forge.codesys.com/u/yannickasselin1/</uri></author><id>https://forge.codesys.com21005091ed221ac6f1665f0d7c016016b878b0b8</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;I don't think this is possible. I've been trying to do this for a long time.&lt;/p&gt;
&lt;p&gt;But what you can do is declare your array in the FB like so:&lt;/p&gt;
&lt;p&gt;VAR_IN_OUT&lt;br/&gt;
    as16_ColorChange: ARRAY&lt;span&gt;[*]&lt;/span&gt; OF INT;&lt;br/&gt;
END_VAR&lt;/p&gt;
&lt;p&gt;And then, you can use LOWER_BOUND and UPPER_BOUND functions to get the lower and upper bounds.&lt;/p&gt;
&lt;p&gt;Ex:&lt;br/&gt;
myLowerBound := LOWER_BOUND(as16_ColorChange, 1);&lt;br/&gt;
myUpperBound := UPPER_BOUND(as16_ColorChange, 1);&lt;/p&gt;
&lt;p&gt;The 1 means first dimension of the array because you can create multiple dimensions variable arrays like so: my3dimArray: ARRAY&lt;span&gt;[&lt;em&gt;,&lt;/em&gt;,*]&lt;/span&gt; OF INT;&lt;/p&gt;
&lt;p&gt;I hope it helps&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>how to declare an array size without constant</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/065a694f1b/?limit=25#5988" rel="alternate"/><published>2020-02-27T14:04:24.540000Z</published><updated>2020-02-27T14:04:24.540000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.com8a84f497103cb0bb8150b19170ba06bdb4d5e27f</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; barkamharikrishna&lt;/p&gt;
&lt;p&gt;hi all,&lt;br/&gt;
can any one help on Array declaration.&lt;br/&gt;
 i am writing an FB. I want to give size of Array with Input variable instead of a constant.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example :&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;as16_ColorChange        : ARRAY &lt;span&gt;[0..100]&lt;/span&gt; OF INT;&lt;/p&gt;
&lt;p&gt;instead of 100 value  i want give some int variable.&lt;/p&gt;
&lt;p&gt;as16_ColorChange        : ARRAY &lt;span&gt;[0..max size]&lt;/span&gt; OF INT;&lt;/p&gt;
&lt;p&gt;maxsize is Input var for my FB:&lt;/p&gt;
&lt;p&gt;Many thanks in advance&lt;/p&gt;
&lt;p&gt;Regards&lt;br/&gt;
Harikrishna &lt;/p&gt;&lt;/div&gt;</summary></entry></feed>