declaring size of array

Hello all,
I'm trying to declare an array, whose size will be defined when executing a local function. eventually I want to do a lot of things with this array. The problem is that I'm getting a lot of problems regarding declaring of the array.
is there a way to declare a GLOBAL array within a LOCAL function ?

is there a way to declare a GLOBAL array within a LOCAL function ?

No. A variable is either global in scope or local to a function.

You can use dynamic memory allocation (with care) to allocate memory when the size is known, and make a global pointer, initialized to NULL, point to that allocated memory.

I'm trying to declare an array, whose size will be defined when executing a local function.

Why does the array size need to be defined within a function ?

I'm trying to declare an array, whose size will be defined when executing a local function

So, what's the problem?

I'm importing values from a txt file on SD card and the number of values is written also in the file so i need to execute the local function first in order to get the size of the array (the number of the values) and after that I will process these values in the loop function. however I'm having problems with this import process even if the number of the values is already known and defined as the size of the array :frowning:

You could use dynamic memory allocation to size the array, but there's really no point. Just declare a global array of the largest size you need and another global that tells you how many items you actually read from the file. Make sure that your code checks that the number provided doesn't exceed the space you have set aside.

Let's suppose that the array will be required to hold say x values at some time. Whether you declare it with that size in a function or as a global it will require the same amount of memory so why not declare a global array of the maximum required size then, when the actual number of array values that will be/have been read set a variable to that number and use it in subsequent array manipulation ?

Actually I’m having a problem with the importing process although I’m trying with a known number of values.
my code looks like this:

#include <SD.h>
#include <SPI.h>

const int chipSelect = 53;
File dataFile;

const int num = 2;
int A[num];
int B[num];
int C[num];
int D[num];
bool AgroesserC;
bool BgroesserD;
bool imported = false;

void setup()
{
  File dataFile;
  SD.begin(chipSelect);
}

void import()
{
  File dataFile;
  dataFile = SD.open("Import.txt", FILE_READ);
  //for(p;num;p++)
    //{
      A[0] = dataFile.parseInt();
      B[0] = dataFile.parseInt();
      C[0] = dataFile.parseInt();
      D[0] = dataFile.parseInt();
    //}
  dataFile.close();
  if(A[0] > C[0])
    {
      bool AgroesserC = true;
    }
  else
    {
      bool AgroesserC = false;
    }
  if(B[0] > D[0])
    {
      bool BgroesserD = true;
    }
  else
    {
      bool BgroesserD = false;
    }
}
void printValues()
{
  File dataFile;
  dataFile = SD.open("printValues.txt", FILE_WRITE);
  //for(b;num;b++)
  //{
    dataFile.print(A[0]);
    dataFile.print(" ");
    dataFile.print(B[0]);
    dataFile.print(" ");
    dataFile.print(C[0]);
    dataFile.print(" ");
    dataFile.println(D[0]);
  //}
  dataFile.close();
}
void loop()
{
  if (imported == false)
  {
    import();
    printValues();
    imported = true;
  }
}

Actually I'm having a problem with the importing process although I'm trying with a known number of values.

And you want us to guess what the problem is. Well, I have a guess, but I'll keep it to myself for now.

void setup()
{
  File dataFile;
  SD.begin(chipSelect);
}

Why are you declaring a File object that goes out of scope without being used?

  //for(p;num;p++)

That's a silly looking for statement. No initialization, no useful condition.

  if(A[0] > C[0])
    {
      bool AgroesserC = true;
    }

Another local variable that goes out of scope without being used. There are lots more, too.

At the the risk of getting into trouble with a lot of people… why don’t you let the Array be a “String” of course by now that is not your major issue, and to prevent any extra posts, let me already say “that is a really bad idea…” but using a Mega, you have the memory. ah no it is a bd idea, you can declare a whole lot of space and create your own heap, that way you can still get support if something isn’t working as expected.

I'm still not seeing a problem here.

But, I'm not seeing any representative code either.

MrMajd:
Hello all,
I'm trying to declare an array, whose size will be defined when executing a local function. eventually I want to do a lot of things with this array. The problem is that I'm getting a lot of problems regarding declaring of the array.
is there a way to declare a GLOBAL array within a LOCAL function ?

If you mean global extent and local scope, use the static keyword inside the function.

If you don't know the difference between extent and scope, its worth checking out, some
languages conflate the two notions.