How to return a pointer to an array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mani

    How to return a pointer to an array?

    int (*nVar)[10]..This is the variable i used in a function.. i
    tried ...nothing worked.. anyone please tell me how to return it....
  • Pranav

    #2
    Re: How to return a pointer to an array?

    On Oct 18, 4:56 pm, mani <manigand...@gm ail.comwrote:
    int (*nVar)[10]..This is the variable i used in a function.. i
    tried ...nothing worked.. anyone please tell me how to return it....
    Just return nVar.....,

    Comment

    • Salt_Peter

      #3
      Re: How to return a pointer to an array?

      On Oct 18, 7:56 am, mani <manigand...@gm ail.comwrote:
      int (*nVar)[10]..This is the variable i used in a function.. i
      tried ...nothing worked.. anyone please tell me how to return it....
      Show minimum, compileable code please.
      We don't know if what you have is a local array and a dangling
      pointer.
      Prefer const references. Prefer std::vector<ove r fixed arrays.

      #include <iostream>
      #include <vector>
      #include <algorithm>
      #include <iterator>

      template< typename T >
      void foo( const std::vector< T >& r_v )
      {
      std::copy( r_v.begin(),
      r_v.end(),
      std::ostream_it erator< T >(std::cout, " ") );
      std::cout << std::endl;
      }

      int main()
      {
      std::vector< int vn(10, 99);
      foo( vn );
      }

      /*
      99 99 99 99 99 99 99 99 99 99
      */

      The above should really be an operator<<
      If you prefer working with dumb fixed arrays:

      template< typename T, const std::size_t Size >
      void bar( const T (& r_a)[ Size ] )
      {
      for( std::size_t i = 0; i < Size; ++i)
      {
      std::cout << r_a[i];
      std::cout << " ";
      }
      std::cout << std::endl;
      }

      int main()
      {
      int array[10] = { 0 };
      bar( array );
      }

      /*
      0 0 0 0 0 0 0 0 0 0
      */

      Comment

      • blargg

        #4
        Re: How to return a pointer to an array?

        In article
        <2be38d55-4704-4ae5-a2db-7abf5681bbbe@40 g2000prx.google groups.com>, mani
        <manigandane@gm ail.comwrote:
        int (*nVar)[10]..This is the variable i used in a function.. i
        tried ...nothing worked.. anyone please tell me how to return it....
        I recommend avoiding C-style arrays, especially pointers to them, as
        you'll quickly get lost in the details. But if you insist, this code shows
        how:

        #include <iostream>

        typedef int i10 [10];

        i10* func()
        {
        i10* p = new i10 [1]; // int (*p) [10] = new int [1] [10]
        (*p) [0] = 1234;
        // ...
        (*p) [9] = 5678;
        return p;
        }

        int main()
        {
        i10* p = func();
        std::cout << (*p) [0] << '\n';
        // ...
        std::cout << (*p) [9] << '\n';
        delete [] p;
        }

        Comment

        Working...