How to declare function returning array of Integer pointers

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

    #1

    How to declare function returning array of Integer pointers

    How can I declare function returning array of Integer pointers . Please
    help !!

  • Rolf Magnus

    #2
    Re: How to declare function returning array of Integer pointers

    Tany wrote:
    [color=blue]
    > How can I declare function returning array of Integer pointers . Please
    > help !![/color]

    You cannot return arrays from functions. What exactly do you want to do?
    Some alternatives are passing in a pointer to the beginning of the array and
    filling it within the function or using std::vector instead of a raw array.

    Comment

    • osmium

      #3
      Re: How to declare function returning array of Integer pointers

      "Tany" writes:
      [color=blue]
      > How can I declare function returning array of Integer pointers .[/color]

      The closest approximation to what you ask for that I know of is to wrap the
      array in a structure and return the structure. I suspect the cure is worse
      than the disease. You might want to re-think what you are trying to do. In
      the code I point at a global variable for test purposes. After all, a
      pointer serves no purpose without a pointee. I would think in terms of the
      caller setting up an array, perhaps empty, and passing a reference to that
      array to the function.

      Boilerplate omitted
      ------------------------
      int k = 1024;

      struct S
      {
      int* ip[20];
      };
      //---------------
      S foo()
      {
      S s;
      s.ip[16] = &k;
      return s;
      }
      //===============
      int main()
      {
      S t;
      t = foo();
      cout << *(t.ip[16]) << endl;
      }


      Comment

      Working...