MY bad this should below to C#...
ignore this one please, sorry about that...


---------------------------------------------------------------------------------------------

Hi people I found I can only reset the values in the array but I can resize it in a new method and return it back to main method,

static void Main()
        {
            int[] myArray = new int[4];
            setLength(myArray, 8);
        }

        static void setLength(int[] myArray, int length)
        {
            Array.Resize(ref myArray, length);
        }

This is the code what I wrote, it works at the end of the setLength method,
but after it turns back to the main mathod, the array returns to the original one...

Could someone help me please?

your code doesn't looks like the c++ I familiar with
where is your "resize" come from?
and what is ref?

Array.Resize(ref myArray, length);

besides,

int[] myArray = new int[4];

is not the way to declare one dimension array
you should use

int *myArray = new int[4];

Here is the simplest way to do it what I know fow now

#include<vector>

void setLength(std::vector<int> &myArray, int length)
{
  myArray.resize(length);
}

int main()
{
  std::vector<int> myArray;
  setLength(myArray, 10);

  return 0;
}

besides, c++ only allow one main
what is the meaning to add static infront of normal function(or they are member function?)

exactly, with vector you don't need to design setLength
just call ".resize()"

the other way than vector<int>(not recommend)

int *setLength(int *myArray, int length)
{
  if(myArray)
  delete[] myArray;

  myArray = new int[length];
  return myArray;
}

int main()
{
  int *myArray = new int[5];
  myArray = setLength(myArray, 10);

  delete []myArray;  

  return 0;
}

my conclusion
use vector instead of raw pointer
you could learn pointer step by step, don't need
to be hasty

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.