Strange behaviour of array

Hello Guys,

I’ve experienced such a strange behavior of a function call. The function returns an array (the struct method) it works fine
but the last element in the array is always 0 when I call the function. Strange thing is: if I use the same method inside main, it works perfectly.
Does anybody has experienced such thing before?
Thanks for the help in advance!

The input file contains lines like this:

1,65,F,87,

#include "parsing.h"

uint8_t chipSelect = 4;
File inputFile;
int commandValue;

void setup() {
  Serial.begin(9600);
  pinMode(chipSelect, OUTPUT);
  SD.begin(chipSelect);
  inputFile = SD.open("test.smp", FILE_READ);
}

void loop() {

  do{
      struct arrWrap command = getCommand(inputFile);
      Serial.println(command.arr[0]);
      Serial.println(command.arr[1]);
      Serial.println(command.arr[2]);
      Serial.println(command.arr[3]);

  }while(inputFile.available());
  while(true);
}
#ifndef parsing_h
#define parsing_h

#include <Arduino.h>
#include <SD.h>
#include <SPI.h>
#include <Wire.h>           
#include <string.h>

enum commandID {FLUSH=0,PRESSURIZE=1,RELEASE=2,SAMPLING=3};

String inputString, actString;
int commaPosition, previousPosition;

struct arrWrap { 
    int arr[3]; 
}; 

struct arrWrap getCommand(File inputFile) 
{ 
    struct arrWrap x; 
    previousPosition = 0;
    commaPosition = 0;
    inputString = inputFile.readStringUntil('\n');
    Serial.println(inputString);
    for(uint8_t i = 0; i<4 ;i++){
      commaPosition = inputString.indexOf(',', previousPosition);
   
      if(i==2){
        actString = inputString.substring(previousPosition,commaPosition);
        if(actString.equals("F")) x.arr[i] = FLUSH;
        else if(actString.equals("P")) x.arr[i] = PRESSURIZE;
        else if(actString.equals("S")) x.arr[i] = SAMPLING;
        else if(actString.equals("R")) x.arr[i] = RELEASE;
      }
      else {
        x.arr[i] = inputString.substring(previousPosition,commaPosition).toInt();
        Serial.println(x.arr[i]);
    }   
    previousPosition = commaPosition+1;
    }
    return x; 
} 

#endif

A three element array doesn't have an index 3.

Check your loops

What AWOL said + in this function

you are returning a non static structure that was local to the function.. By the time you go back to the main code and try to do other stuff, the memory on the stack is no longer valid and might have been used by another function/method

IGNORE - need coffee :slight_smile:

just what AWOL said, as always (a structure is exactly the same as any built-in type for purposes of parameter passing, return values, and assignment.)

J-M-L:
What AWOL said + in this function

struct arrWrap getCommand(File inputFile) 

{
    struct arrWrap x;
...
    return x;
}


you are returning a non static structure that was local to the function.. By the time you go back to the main code and try to do other stuff, the memory on the stack is no longer valid and might have been used by another function/method

Have the rules on returning a struct changed then?

AWOL:
Have the rules on returning a struct changed then?

No was just thinking I wrote BS :slight_smile: as you wrote back

You can pass arrays by reference. There is no need to waste resources wrapping a single array in a struct.

Thanks for the help guys. its clear :slight_smile:

Hey Guys,

I’ve managed to found a working solution, based on what you’ve suggested.
If I define a static integer array in main, then passing it to the function and fill
out the members of the array inside, its working perfectly.
Thanks again for the help!

Cheers,

#ifndef parsing_h
#define parsing_h

#include <Arduino.h>
#include <SD.h>
#include <SPI.h>
#include <Wire.h>           
#include <string.h>

enum commandID {FLUSH=0,PRESSURIZE=1,RELEASE=2,SAMPLING=3};

String inputString, actString;
int commaPosition = 0, previousPosition = 0;

void getCommand(File inputFile, int commandArray[]) 
{ 
    previousPosition = 0;
    commaPosition = 0;
    inputString = inputFile.readStringUntil('\n');
    Serial.println(inputString);
    for(uint8_t i = 0; i<4 ;i++){
      commaPosition = inputString.indexOf(',', previousPosition);
      if(i==2){
        actString = inputString.substring(previousPosition,commaPosition);
        if(actString.equals("F")) commandArray[i] = FLUSH;
        else if(actString.equals("P")) commandArray[i] = PRESSURIZE;
        else if(actString.equals("S")) commandArray[i] = SAMPLING;
        else if(actString.equals("R")) commandArray[i] = RELEASE;
      }
      else {
        commandArray[i] = inputString.substring(previousPosition,commaPosition).toInt();
    }   
    previousPosition = commaPosition+1;
    }
} 

#endif
#include <parsing.h>

uint8_t chipSelect = 4;
File inputFile;
static int commandValue[3];

void setup() {
  Serial.begin(9600);
  pinMode(chipSelect, OUTPUT);
  SD.begin(chipSelect);
  inputFile = SD.open("test.smp", FILE_READ);
}

void loop() {

  do{
      getCommand(inputFile, commandValue);
      Serial.println(commandValue[0]);
      Serial.println(commandValue[1]);
      Serial.println(commandValue[2]);
      Serial.println(commandValue[3]);

  }while(inputFile.available());
  while(true);
}

there is no need to declare commandValue as static as it's scope is global

don't forget to close your file