Help needed with pointers and arrays

Status
Not open for further replies.

M4ngu

Well-known member
Hi there, I'm using this code to save and load files from the microSD card, but instead of repeating this 33 times (one for each variable I need to save) would like to do in a loop, using an array
Save:
Code:
  String file = "/VAL.DAT";
  if (SD.exists(file.c_str()))
  {
    SD.remove(file.c_str());
  }
  dataFile = SD.open(file.c_str(), FILE_WRITE);
  dataFile.write((uint8_t *)&val, sizeof(val));
  dataFile.close();
Load:
Code:
    String file = "/VAL.DAT";
    dataFile = SD.open(file.c_str(), FILE_READ);
    dataFile.read((uint8_t *)&val, sizeof(val));
so I do an array for the names, to use fileName instead of "/VAL.DAT"
Code:
const String fileName[] = { "/VAL.DAT", "/PROB.DAT", "/STEPS.DAT" ...etc };
but no idea about how to do an array with the pointers of the variables I need to save & load,
and no idea how the code should be to use that array,
any advice would be much appreciated, thanks
 
Use C strings?
Code:
const char * fileNames[] = { "/....", "/....", ... };

for (int i = 0 ; i < sizeof(fileNames)/sizeof(char*) ; i++)
{
  const * fileName = fileNames[i] ;
  if (SD.exists(fileName))
  {
    SD.remove(fileName);
  }
  dataFile = SD.open(fileName, FILE_WRITE);
  dataFile.write((uint8_t *)&val, sizeof(val));
  dataFile.close();
}
 
Thanks but the names part is not the problem, as I explain in the OP (maybe not clear enough)
what I need to figure out is how to make an array of pointers for the variables to be saved and loaded from the memory card,
and how those 2 lines should be modified:
Code:
//save:
dataFile.write((uint8_t *)&val, sizeof(val));
//load:
dataFile.read((uint8_t *)&val, sizeof(val));
 
Hi there, I'm using this code to save and load files from the microSD card, but instead of repeating this 33 times (one for each variable I need to save) would like to do in a loop, using an array

but no idea about how to do an array with the pointers of the variables I need to save & load,
and no idea how the code should be to use that array,
any advice would be much appreciated, thanks

@M4ngu:

If I am understanding correctly what you are after, here's a short sketch to demonstrate an approach that I believe matches what you are needing. You will create an array containing the addresses of your variables, & you will access "the value stored at those addresses" inside your loop.

Code:
unsigned int test00 = 0;
unsigned int test01 = 1;
unsigned int test02 = 2;
unsigned int test03 = 3;
unsigned int test04 = 4;
unsigned int test05 = 5;
unsigned int test06 = 6;
unsigned int test07 = 7;
unsigned int test08 = 8;
unsigned int test09 = 9;

unsigned int *ary1[] = { &test00, &test01, &test02, &test03, &test04, &test05, &test06, &test07, &test08, &test09, };

void setup() {
   Serial.begin(19200);

   // print out the contents of the array of variables
   for (int i = 0; i < 10; i++)
   {
      Serial.print("entry #");
      Serial.print(i);
      Serial.print(" in the array : ");
      Serial.println(*ary1[i]);
   }

   Serial.println("");

   // modify the variables themselves directly
   test00 *= 2;
   test01 *= 2;
   test02 *= 2;
   test03 *= 2;
   test04 *= 2;
   test05 *= 2;
   test06 *= 2;
   test07 *= 2;
   test08 *= 2;
   test09 *= 2;

   // print out the contents of the array of modified variables
   for (int i = 0; i < 10; i++)
   {
      Serial.print("entry #");
      Serial.print(i);
      Serial.print(" in the array : ");
      Serial.println(*ary1[i]);
   }

   Serial.println("");

   // simple example that shaows how the variables are accessed indirectly within the array in order to be modified
   for (int i = 0; i < 10; i++)
   {
      *ary1[i] = *ary1[i] * 2;  // could also be written as "*ary1[i] *= 2;", but written out explicitly here for clarity
   }

   // print out the contents of the array of modified variables
   for (int i = 0; i < 10; i++)
   {
      Serial.print("entry #");
      Serial.print(i);
      Serial.print(" in the array : ");
      Serial.println(*ary1[i]);
   }
}

void loop() {
}

Hope this helps . . .

Mark J Culross
KD5RXT
 
Last edited:
Status
Not open for further replies.
Back
Top