I
I
Igor2013-12-03 19:52:03
C++ / C#
Igor, 2013-12-03 19:52:03

How to pass a pointer to an array as a function argument?

I work in Visual Studio 2012 Ultimate, the task is to make my own library. I write in Visual C++. I decided to do it simply - 3 library functions that will sort the input array. The problem is that I cannot pass a pointer to an array as a function argument, so that this array can then be parsed, processed and returned by a library function. Perhaps the problem is that I am declaring the array incorrectly.
I declare an array like this:

static array<int^>^ MyArray = gcnew array<int^>(50);

I give the function like this:
Sort::SortMethod::Select(MyArray,i);
I accept it in the .h file like this:
namespace Sort
{
    class SortMethod
    {
    public:
    
        static int Select(int MyArray, int number);

    };
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
Teivaz, 2013-12-04
@ReLogan

If, after all, we are talking about C ++, then an array can be declared like this:
or like this:
well, or like this:
And, accordingly, use it in functions like this:

void Sort(int array[50 ar]);
Sort(myArray);

void Sort(int* ar, unsigned int size);
Sort(myArray, 50);

void Sort(std::vector<int>& ar);
Sort(myArray);

L
lexdevel, 2013-12-03
@lexdevel

static int Select(array<int^>^ buffer, int number)
{
        // ToDo: implement
        return -1;
}

I
Igor, 2013-12-04
@ReLogan

@Teivaz , thanks for the detailed answer!
The compiler no longer swears at the code, but when debugging, it sees two errors, the meaning of which I didn’t get a taste of, Google didn’t really help.

2>WindowsFormsApplication1.obj : error LNK2028: oё√yr er eHerchЁх°heeє■ uhhёh (0A00000C) "public: static int __clrcall Sort::SortMethod::Select(int *,unsigned int)" ([email protected]@[email protected] @[email protected]) t ЇєеъЎшш "private: void __clrcall WindowsFormsApplication1::Form1::sort_Click(class System::Object ^,class System::EventArgs ^)" ([email protected]@[email protected]@$$FA$ [email protected]@@[email protected]@@Z)
2>WindowsFormsApplication1.obj : error LNK2019: oё√yr er eHerchЁх°hee√sh teh°eshshch oёtyuy "public: static int __clrcall Sort::SortMethod::Select(int *,unsigned int)" ([email protected]@Sort @@[email protected]) t ЇєеъЎшш "private: void __clrcall WindowsFormsApplication1::Form1::sort_Click(class System::Object ^,class System::EventArgs ^)" ([email protected]@[email protected]@$$FA [email protected]@@[email protected]@@Z)
2

I will give an abbreviated code of the project files, perhaps I just have an elementary error, but I cannot find it.
Main file:
#pragma endregion
    static int* MyArray = new int[50]; //объявляем масив
    static int i=0;
  private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
       MyArray[i] = Convert::ToInt32(number -> Text);
       i++;

       }
  private: System::Void sort_Click(System::Object^  sender, System::EventArgs^  e) {
       Sort::SortMethod::Select(MyArray,i);			 
     }

The .h file of my library:
namespace Sort
{
    class SortMethod
    {
    public:
    
        static int Select(int *MyArray, unsigned int number);

    };
}

Library .cpp file:
namespace Sort
{
    int SortMethod::Select(int *MyArray, unsigned int number)
    {
    int tmp;
    for(int i = 0; i < number; ++i) // i - номер текущего шага
    { 
      int pos = i; 
      tmp = MyArray[i];
      for(int j = i + 1; j < number; ++j) // цикл выбора наименьшего элемента
      {
        if (MyArray[j] < tmp) 
         {
           pos = j; 
           tmp = MyArray[j]; 
         }
      }
      MyArray[pos] = MyArray[i]; 
      MyArray[i] = tmp; // меняем местами наименьший с a[i]
    }
    return *MyArray;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question