How to properly add or modify element of a struct array?

 

Hi! I'm pretty new to programming and trying out the following code. It works for an int array but not on struct array. Maybe it will work if I check for the specific member of the struct like a[iLast].mInt != NULL on the check at the Add1DArrElem (the line with comment // illegal operation error here) function definition but then the whole function will be specific to this struct definition. I wanted to create a function I could reuse on other arrays of any type including struct. Is there already a library predefined with such functions (for mql4)? Or is there a better way of doing this altogether?

/*
*  Array Functions
*/

// Modify Element of an Array
template <typename T>
void Mod1DArrElem(T& a[], int i, T element){
   a[i] = element;
}


// Add Element to Array 
template <typename T>
void Add1DArrElem(T& a[], T element){
   int iLast = ArraySize(a)-1;
   if (a[iLast] != NULL){ // illegal operation error here
      ArrayResize(a, ArraySize(a)+1, 10);
   }
   for (int i=0; i<ArraySize(a); i++){
      if (a[i] != NULL){continue;}
      Mod1DArrElem(a, i, element);
      break;
   }
}


struct SStruct
  {
      int      mInt;
      string   mString;
      
      SStruct(){
         mInt     = NULL;
         mString  = NULL;
      }
      ~SStruct(){}
  };


void OnStart(){
   SStruct StructArr[];
   int s = ArraySize(StructArr);
   PrintFormat("initial size = %i", s);
   ArrayResize(StructArr, 5);
   s = ArraySize(StructArr);
   PrintFormat("after resize size = %i", s);
   for (int x=0; x<s; x++) {
      StructArr[x].mInt = x+1;
      StructArr[x].mString = "Initial Value";
   }
   // Confirm initial Data set
   for (int i=0; i<s; i++){
      PrintFormat("Current StructArr[%i].mInt = %i, StructArr[%i].mString = %s", i, StructArr[i].mInt, StructArr[i].mString);
   };

   // Define Sample Struct Element to add
   SStruct newElem;
   newElem.mInt      = 1000;
   newElem.mString   = "New element";

   // Add struct element using function defined above
   Add1DArrElem(StructArr, newElem);
      
   // Check if element addition was successful
   s = ArraySize(StructArr);
   PrintFormat("After adding element size = %i", s);
   for (int i=0; i<s; i++){
      PrintFormat("Current StructArr[%i].mInt = %i, StructArr[%i].mString = %s", i, StructArr[i].mInt, StructArr[i].mString);
   };
   


// Try with int array (This works)

//   int arr[] = {1,2,3,4,5};
//   
//   // Confirm initial data set
//   int s = ArraySize(arr);
//   PrintFormat("initial size = %i", s);
//   for (int i=0; i<s; i++){
//      PrintFormat("Current arr[%i] = %i", i, arr[i]);
//   };
//   
//   // Add int element using function defined above
//   Add1DArrElem(arr, 10);  
//
//   // Check if element addition was successful
//   s = ArraySize(arr);
//   PrintFormat("final size = %i", s);
//   for (int i=0; i<s; i++){
//      PrintFormat("Current arr[%i] = %i", i, arr[i]);
//   };
//   

}
 
You cannot compare a struct to NULL.

You would need a comparison struct element as replacement of NULL.

Since you are using a constructor in the struct, you could replace NULL with a local struct variable, representing an empty element to be compared to your array element.
 
Thank you very much. I'll try your suggestion.
 

Did you try overloading the == operator?

bool operator== (const SStruct &op) { return this.mInt==op.mInt && this.mString==op.mString; }
bool operator!= (const SStruct &op) { return this.mInt!=op.mInt || this.mString!=op.mString; }
bool operator== (int i) { return this.mInt==i; } // ==NULL goes here
bool operator!= (int i) { return this.mInt!=i; } // !=NULL goes here