I'm having an issue retuning a byte array in Visual Studio. The following code outputs as expected in online compiler https://www.onlinegdb.com/
The expected output is an array of 8 bytes: BB-CC-C3-02-5C-11-6D-00
And the online compiler outputs the same as expected:
*******************************************************************************/
#include <stdio.h>
#include <stdint.h>
uint8_t * createByteArray(float power, int power_coefficient);
int main()
{
float power = 4444;
int power_coefficient = 1;
uint8_t * returned_ptr = createByteArray(power, power_coefficient);
for (int i = 0; i < returned_ptr[3]+6; i++)
printf("%02X-", returned_ptr[i]);
return 0;
}
uint8_t * createByteArray(float power, int power_coefficient)
{
uint16_t power_ushort = (uint16_t)(power * power_coefficient);
uint8_t bytes_power[2];
bytes_power[0] = (uint8_t)((power_ushort >> 8) & 0xFF);
bytes_power[1] = (uint8_t)(power_ushort & 0xFF);
uint8_t firstHalf_power = bytes_power[0];
uint8_t secondHalf_power = bytes_power[1];
int parity = (int)firstHalf_power + (int)secondHalf_power;
uint16_t parity_ushort = (uint16_t)(parity);
uint8_t bytes_parity[2];
bytes_parity[0] = (uint8_t)((parity_ushort >> 8) & 0xFF);
bytes_parity[1] = (uint8_t)(parity_ushort & 0xFF);
uint8_t firstHalf_parity = bytes_parity[0];
uint8_t secondHalf_parity = bytes_parity[1];
uint8_t telegram_set_power[8] = {0xBB, 0xCC, 0xC3, 0x02, secondHalf_power, firstHalf_power, secondHalf_parity, firstHalf_parity};
uint8_t * ptr = telegram_set_power;
return ptr;
}
But in Visual Studio 2022, the same code returns the following output:
AA-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-00-00-00-00-00-00-00-00-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-CC-A8-00-00-00-00-00-00-00-A8-00-00-00-00-00-00-00-45-64-DD-8D-FA-7F-00-00-00-00-00-00-00-00-00-00-64-00-1B-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-36-9D-EA-8D-FA-7F-00-00-A0-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-CC-F6-11-AE-2C-00-00-00-10-00-00-00-00-00-00-00-D0-F5-11-AE-2C-00-00-00-1C-00-1B-00-00-01-00-00-E0-F5-11-AE-2C-00-00-00-14-00-00-00-00-00-00-01-00-00-00-00-00-00-00-00-00-00-
I have been trying to fix it but failed. What could be the reason for this?
Edit: This happens when I compile as 64 bit. When I compile under x86 it works. But I need to compile as x64 for some other reasons.