returning d array from function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abdulsamad
    New Member
    • Feb 2008
    • 7

    returning d array from function

    can any1 tell me how to return 2d array from a nested functions......


    i am doing it like that i have two classes........ ...

    Game{
    private:
    int array[6][6];
    public:
    int** fillArray()
    {
    return array;
    }

    };
    Graphics
    {
    private:
    Game play;
    int** fillArray()
    {
    int **array=play.fi llArray();
    return array;
    }

    };
    void main()
    {
    Graphics gr
    int **arr= gr.fillaArray() ;
    }


    If there is some way to do this kindly tell me i am in extreme trouble ....there is some trouble in this code so kindly tell me


    regards Abdul Samad
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You'd better read this first:
    Originally posted by weaknessforcats
    First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
    [code=c]
    int array[5];
    [/code]

    That is an array of 5 elements each of which is an int.

    [code=c]
    int array[];
    [/code]

    won't compile. You need to declare the number of elements.

    Second, this array:
    [code=c]
    int array[5][10];
    [/code]

    is still an array of 5 elements. Each element is an array of 10 int.

    [code=c]
    int array[5][10][15];
    [/code]

    is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


    [code=c]
    int array[][10];
    [/code]

    won't compile. You need to declare the number of elements.

    Third, the name of an array is the address of element 0
    [code=c]
    int array[5];
    [/code]

    Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

    [code=c]
    int array[5][10];
    [/code]

    Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
    [code=c]
    int array[5][10];

    int (*ptr)[10] = array;
    [/code]

    Fourth, when the number of elements is not known at compile time, you create the array dynamically:

    [code=c]
    int* array = new int[value];
    int (*ptr)[10] = new int[value][10];
    int (*ptr)[10][15] = new int[value][10][15];
    [/code]

    In each case value is the number of elements. Any other brackets only describe the elements.

    Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

    [code=c]
    int** ptr = new int[value][10]; //ERROR
    [/code]

    new returns the address of an array of 10 int and that isn't the same as an int**.

    Likewise:
    [code=c]
    int*** ptr = new int[value][10][15]; //ERROR
    [/code]

    new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.

    With the above in mind this array:
    [code=cpp]
    int array[10] = {0,1,2,3,4,5,6, 7,8,9};
    [/code]
    has a memory layout of

    0 1 2 3 4 5 6 7 8 9

    Wheras this array:
    [code=cpp]
    int array[5][2] = {0,1,2,3,4,5,6, 7,8,9};
    [/code]
    has a memory layout of

    0 1 2 3 4 5 6 7 8 9

    Kinda the same, right?

    So if your disc file contains

    0 1 2 3 4 5 6 7 8 9

    Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.

    Therefore, when you do your read use the address of array[0][0] and read as though you have a
    one-dimensional array and the values will be in the correct locations.
    and then post again if you still have questions.

    Comment

    • abdulsamad
      New Member
      • Feb 2008
      • 7

      #3
      then should i return it like that


      int* fillArray()
      {
      return array; // where array is a 2d array
      }


      void main()
      {
      int array[6][6];
      array=fillArray ();
      }


      if not kindly send me the code which will return the 2D array

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        A 2D array of int cannot be returned as an int*.

        I have just added an article on arrays in the C/C++ HowTos forum and toward the end the article talks about returning arrays from a function.

        Have a look: http://www.thescripts.com/forum/thread772412.html.

        Comment

        Working...