# Pointer initialization issue

**URL:** <https://community.br-automation.com/t/pointer-initialization-issue/6076>\
**Category:** Programming\
**Tags:** automation-studio\
**Created:** [February 28, 2025, 7:11am UTC](https://community.br-automation.com/t/pointer-initialization-issue/6076 "2025-02-28T07:11:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Syed\_Karim](https://avatars.discourse-cdn.com/v4/letter/s/FF8800/32.png) [@Syed\_Karim](https://community.br-automation.com/u/Syed_Karim)\
**Post date:** [February 28, 2025, 7:11am UTC](https://community.br-automation.com/t/pointer-initialization-issue/6076/1 "2025-02-28T07:11:10Z")

</div>

Hello Everyone,  
I hope you’re all doing great and looking forward to a fantastic weekend ahead! 😉

I would really appreciate any insights on an issue I’m facing while programming. I am using a pointer array, `pODCommVar` (`CommVarODType[0..2]`), where each structure instance contains a member `wSystemCommand`. This member is assigned a constant value based on specific conditions. Below is the relevant part of my code:

```auto
#include <bur/plctypes.h>

#ifdef _DEFAULT_INCLUDES
#include <AsDefault.h>
#endif

#include <string.h>
#include <math.h>
#include "AuxFunction.h"

void _INIT ProgramInit(void)
{
	if (!PointersValid()) return;				
}

void _CYCLIC ProgramCyclic(void)
{
	for (i = 0; i < OD_COUNT; i++)
	{
		pCmd = &gpODx3[i].Cmd;
		pSetup = &gpSetupODx3[i];
// pODCommVar[i] = &CommVarOD[i];
		pODCommVar = &CommVarOD[i];		
		
/// *****************Edge Commands for three OD sensors******************************** /
RisingEdge(&pCmd->RestoreFactorySettingsSensor, &Cmd[i].RestoreFactorySettingsSensor, &Edges[i].RestoreFactorySettingsSensor);
RisingEdge(&pCmd->FactorySettingsSetup, &Cmd[i].FactorySettingsSetup, &Edges[i].FactorySettingsSetup);
RisingEdge(&pCmd->Reset, &Cmd[i].Reset, &Edges[i].Reset);
RisingEdge(&pCmd->TeachInChanelSP1, &Cmd[i].TeachInChanelSP1, &Edges[i].TeachInChanelSP1);
RisingEdge(&pCmd->ZeroPointReset, &Cmd[i].ZeroPointReset, &Edges[i].ZeroPointReset);
RisingEdge(&pCmd->ZeroPointTeach, &Cmd[i].ZeroPointTeach, &Edges[i].ZeroPointTeach);
		
/ ***********************Factory Reset**************************** /
		if (Cmd[0].FactorySettingsSetup) 
		{
			FactorySettingsOD0(&gpSetupODx3[0]);         
		}
		if (Cmd[1].FactorySettingsSetup) 
		{
			FactorySettingsOD1(&gpSetupODx3[1]);         
		}
		if (Cmd[2].FactorySettingsSetup) 
		{
			FactorySettingsOD2(&gpSetupODx3[2]);         
		}
/ ***************************< Reset var >**************************************** /
		Cmd[0].FactorySettingsSetup = 0;
		Cmd[1].FactorySettingsSetup = 0;
		Cmd[2].FactorySettingsSetup = 0;

/ **************************< State machine >********************************* /
		switch (StateOD[i]) 
		{
			case W_SYSTEM_COMMAND:
				if (Cmd[i].ZeroPointTeach)
				{
					LocalVar[i].ODWrite = 1;		
					pODCommVar[i]->wSystemCommand = 194;	
				}
				else if (Cmd[i].ZeroPointReset)
				{
					LocalVar[i].ODWrite = 1;
					pODCommVar[i]->wSystemCommand = 195;		
				}
				else if (Cmd[i].RestoreFactorySettingsSensor)
				{
					LocalVar[i].ODFactorySetting = 1;
					pODCommVar[i]->wSystemCommand = 130;		
				}

				/* Write operation for system command */
				if (LocalVar[i].ODWrite || LocalVar[i].ODFactorySetting)
				{
					StateOD[i] = WR_CYCLE_TIME;
				}
				break;

			case WR_CYCLE_TIME:
				if (Cmd[i].Reset)
				{
					LocalVar[i].ODWrite = 0;
					LocalVar[i].ODFactorySetting = 0;
					memset(&Cmd[i], 0, sizeof(Cmd[i])); // Reset All commands
					StateOD[i] = W_SYSTEM_COMMAND;
				}	
				break;
		}	
	}
}

```

**Issue:**  
For Cmd[0].ZeroPointTeach, Cmd[0].ZeroPointReset, and Cmd[0].RestoreFactorySettingsSensor, everything works as expected—LocalVar[0].ODWrite is correctly set, and pODCommVar[0]-\>wSystemCommand holds the correct assigned value.

However, for i = 1 and i = 2, LocalVar[i].ODWrite is correctly set, but pODCommVar[i]-\>wSystemCommand is displaying unexpected values:

pODCommVar[1]-\>wSystemCommand = 2  
pODCommVar[2]-\>wSystemCommand = 0

(Screenshot attached for reference)

 ![image](https://europe1.discourse-cdn.com/brcommunity/original/2X/8/8a6f597e8b71157ed4960711721e2433fac1ce2b.png)

**Question:**  
Is there something I’m missing in my pointer assignment or array indexing? Any guidance on what could be causing this discrepancy would be greatly appreciated.

Thanks in advance!

Best regards,  
Tabish

---

<div class="post-metadata">

**Author:** ![michael\_w](https://avatars.discourse-cdn.com/v4/letter/m/FF8800/32.png) [@michael\_w](https://community.br-automation.com/u/michael_w)\
**Post date:** [February 28, 2025, 3:34pm UTC](https://community.br-automation.com/t/pointer-initialization-issue/6076/2 "2025-02-28T15:34:49Z")

</div>

Hi there,

can you please export this into a task with all types in use so it’s compilable? Just from looking at the code it’s hard to tell…

Best regards

---

<div class="post-metadata">

**Author:** ![Markus\_Trapp](https://avatars.discourse-cdn.com/v4/letter/m/FF8800/32.png) [@Markus\_Trapp](https://community.br-automation.com/u/Markus_Trapp)\
**Post date:** [March 6, 2025, 1:39pm UTC](https://community.br-automation.com/t/pointer-initialization-issue/6076/3 "2025-03-06T13:39:20Z")

</div>

> [@Syed\_Karim](#):
>
> ```auto
> pODCommVar[i] = &CommVarOD[i];
> pODCommVar = &CommVarOD[i];		
> 
> ```

pODCommVar is double assigned. In the last line you assign the pointer array to &CommVarOD[i].

---

<div class="post-metadata">

**Author:** ![Syed\_Karim](https://avatars.discourse-cdn.com/v4/letter/s/FF8800/32.png) [@Syed\_Karim](https://community.br-automation.com/u/Syed_Karim)\
**Post date:** [March 7, 2025, 5:21am UTC](https://community.br-automation.com/t/pointer-initialization-issue/6076/4 "2025-03-07T05:21:37Z")

</div>

Hello Michael / ALL,  
Apologies for responding late as I was stucked with something else. I am attaching a zip file  
[PointerCheck.zip](https://community.br-automation.com/uploads/short-url/5PhkNdvzXz2EoeTw2SLF0SWy9Vq.zip) (87.9 KB)  
after exporting the project (Save Project As zip without upgrades is exceeding the upload size limit).  
Hopefully this way will work or let me know if you are unable to open it.  
TIA.

---

<div class="post-metadata">

**Author:** ![Syed\_Karim](https://avatars.discourse-cdn.com/v4/letter/s/FF8800/32.png) [@Syed\_Karim](https://community.br-automation.com/u/Syed_Karim)\
**Post date:** [March 7, 2025, 6:19am UTC](https://community.br-automation.com/t/pointer-initialization-issue/6076/5 "2025-03-07T06:19:09Z")

</div>

Hi Markus,  
pODCommVar is not double assigned. The second line is commented out 🙂

---

<div class="post-metadata">

**Author:** ![michael\_w](https://avatars.discourse-cdn.com/v4/letter/m/FF8800/32.png) [@michael\_w](https://community.br-automation.com/u/michael_w)\
**Post date:** [March 7, 2025, 8:37am UTC](https://community.br-automation.com/t/pointer-initialization-issue/6076/6 "2025-03-07T08:37:04Z")

</div>

Hi @Syed_Karim ,

unfortunately this export is not complete. After i manually imported only the task, the code does not compile. It is missing various datatypes, so basically not much more useful to me/us as the initial example.  
Regarding file sizes, maybe this helps:

> [@How to properly share AS projects](https://community.br-automation.com/t/how-to-properly-share-as-projects/5758):
>
> Hi everyone, Often, code and configuration are more expressive than screenshots of an error or general text descriptions of an issue. This is true for various use cases, but of course, given the context, I am doing this with the community in mind. This article is also meant for everyone to link whenever you ask for a shared project in whatever context so you do not have to describe the process yourself. A little data Comparing a new project, a compiled project folder, a zipped project folde…

 ![image](https://europe1.discourse-cdn.com/brcommunity/original/2X/1/15ce8ee80f4a7e2c2b286f340c48e71442496284.png)

Please export the task, import it into a new project (no need to configure any hardware besides the PLC there) and make sure it compiles - that way you can make sure it also compiles for us trying to help.

Best regards

---

<div class="post-metadata">

**Author:** ![jiri.jecha](https://avatars.discourse-cdn.com/v4/letter/j/FF8800/32.png) [@jiri.jecha](https://community.br-automation.com/u/jiri.jecha)\
**Post date:** [March 7, 2025, 1:52pm UTC](https://community.br-automation.com/t/pointer-initialization-issue/6076/7 "2025-03-07T13:52:49Z")

</div>

Hello @Syed_Karim,  
In your task, you have provided, I can see 2 issues:  
Firstly, lets take a look on your variable and pointer declarations:

```auto
CommVarOD : ARRAY[0..2] OF CommVarODType;
pODCommVar : REFERENCE TO ARRAY[0..2] OF CommVarODType;

```

First, on line 27, you need to use **&** to assign an address of CommVarOD to a ponter pODCommVar. It should look like this:  
`Line 27: pODCommVar = &CommVarOD;`

Second, in your code, you use the pointer as “an array of pointers”.  
`Line 83: pODCommVar[i]->wSystemCommand = 194;`  
But your declaration and assignment are actually “pointer to an array”.  
A way how to make your code work is, to dereference the pointer to the array and then accesses the element at the specified index. Here is corrected notation:  
`Line 83: (*pODCommVar)[i].wSystemCommand = 194;`

By editing lines 27, 83, 89 and 94 according to provided examples, your code should work. 🙂

---

<div class="post-metadata">

**Author:** ![Syed\_Karim](https://avatars.discourse-cdn.com/v4/letter/s/FF8800/32.png) [@Syed\_Karim](https://community.br-automation.com/u/Syed_Karim)\
**Post date:** [March 10, 2025, 7:00am UTC](https://community.br-automation.com/t/pointer-initialization-issue/6076/8 "2025-03-10T07:00:19Z")

</div>

Ahoj Jirko,  
Mohu potvrdit, že tvůj návrh byl naprosto správný! 🤜

Díky moc a přeji hezký den!

Tabish
