Results 1 to 4 of 4

Thread: Function that returns a 2d array

  1. #1
    Join Date
    Apr 2009
    Posts
    87

    Function that returns a 2d array

    A function can return a 2d array?Can one some give a explanation of this with example?

    I tried the following code but does not work...
    Code:
    int function (array [] [120]) 
      ( 
      / / program 
      return array [] [120]; 
      )

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Function that returns a 2d array

    Is it possible in C++ to have a function that returns a 2-D or indeed an X-D array from a function.Another alternative is to pass in the 2D array as a parameter into the function (by reference) and then filll it and then just leave the function which would maintain the array's content.

  3. #3
    Join Date
    Jan 2009
    Posts
    199

    Re: Function that returns a 2d array

    Return a 2D array from a function in C :
    Code:
    #include 
    
    struct array_def 
    { 
    int **q; 
    int row1; 
    int col; 
    }; 
    
    int** func(); 
    
    array_def* func_struc(); 
    
    void main() 
    { 
    int **b = func(); 
    for(int j=0;j < 2; ++j) 
    { 
    for(int k=0;k < 2; ++k) 
    cout<cout<} 
    
    array_def *pArr = func_struc(); 
    
    for(j =0; j < pArr->row; ++j) 
    { 
    for(int k =0; k< pArr->cols; ++k) 
    cout<p[j][k]<cout<} 
    
    
    } 
    
    int** func() 
    { 
    int **q; 
    int cnt =7; 
    q = new int*[2]; 
    for(int j =0;j < 2; ++j) 
    p[i] = new int[2]; 
    
    for(j =0; j < 2; ++j) 
    for(int k=0; k < 2; ++k) 
    p[j][k] = cnt++; 
    
    return q; 
    } 
    
    array_def* func_struc() 
    { 
    array_def *pArray = new array_def; 
    int cnt =1; 
    pArray->q = new int*[2]; 
    for(int j =0;j < 2; ++j) 
    pArray->p[j] = new int[2]; 
    
    for(j =0; j < 2; ++j) 
    for(int k=0; k < 2; ++k) 
    pArray->p[j][k] = cnt++; 
    
    pArray->cols =2; 
    pArray->row = 2; 
    
    return pArray; 
    }

  4. #4
    Join Date
    Dec 2008
    Posts
    177

    Re: Function that returns a 2d array

    In your case you pass an array argument (but bad because you did not indicate the type), this table will be modified directly, so it is unnecessary to return.

    A dynamically allocated 2D array can be returned by cons because it is a pointer to pointer.

Similar Threads

  1. Pass Array into a function
    By Viensterrr in forum Software Development
    Replies: 3
    Last Post: 24-12-2009, 12:55 PM
  2. Two dimensional array to a function
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 27-11-2009, 12:47 PM
  3. How to return an array from function
    By Rilex in forum Software Development
    Replies: 3
    Last Post: 02-10-2009, 09:18 AM
  4. function that takes a 2D array as a parameter
    By Umang in forum Software Development
    Replies: 4
    Last Post: 06-05-2009, 11:59 PM
  5. Function that returns an array
    By ByteCoder in forum Software Development
    Replies: 3
    Last Post: 17-04-2009, 07:02 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,449,065.84431 seconds with 16 queries