Returning a 2D array from a function








  PRODUCT  :  Borland C++                           NUMBER  :  1547
  VERSION  :  All
       OS  :  All
     DATE  :  July 21, 1993                            PAGE  :  1/2

    TITLE  :  Returning a 2D array from a function




  //--------------------------------------------------------------
  //
  //  Description: Example program demonstrates declaring a
  //  function that  returns a two-dimensional array and a
  //  pointer to a function of that  type.
  //
  //--------------------------------------------------------------

  #include <iostream.h>

  const arraySize = 50;
  const arrayCount = 5;
  const initElem = 10;

  // Func takes an int and returns a pointer to an array of
  // arrayCount arrays of arraySize doubles after initializing
  // the initElem'th element of the first array with the passed
  // double value.
  double (* Foo( double i )) [arraySize]
  {
      // Allocate space ( arrayCount * arraySize *
      // sizeof(double) )
      double (*p)
             [arraySize] = new double [arrayCount] [arraySize];

      // Initialize the initElem'th element of the first array
      p[0][initElem] = i;

      return p;
  }

  int main()
  {
      // Pointer to a function which takes an int and returns a
      // pointer to an open array of arrays of arraySize doubles
      double (* (*FuncPtr)(double) ) [arraySize];

      // The compiler would not allow this if the function
      // prototype and the function pointer were not of
      // compatible types.
      FuncPtr = Foo;

      // Pointer to an open array of arrays of arraySize doubles













  PRODUCT  :  Borland C++                           NUMBER  :  1547
  VERSION  :  All
       OS  :  All
     DATE  :  July 21, 1993                            PAGE  :  2/2

    TITLE  :  Returning a 2D array from a function




      double (*data)[arraySize];

      // Function allocates data space and returns with
      // initElem'th element  of first array initialized to
      // passed value.
      data = FuncPtr( 5.5 );

      cout << "array[0][" << initElem << "]=" << data[0][initElem]
           << endl;

      delete [] data;

      return 0;
  }


  DISCLAIMER: You have the right to use this technical information
  subject to the terms of the No-Nonsense License Statement that
  you received with the Borland product to which this information
  pertains.





























Comments

Popular posts from this blog

BOTTOM LIVE script

Fawlty Towers script for "A Touch of Class"