Return pointer to 2d array

This is a discussion on Return pointer to 2d array within the C Programming forums, part of the General Programming Boards category; hi all, i want to creat a 2D array and return the pointer of it, and then pass the pointer ...

  1. #1
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    Question Return pointer to 2d array

    hi all,
    i want to creat a 2D array and return the pointer of it, and then pass the
    pointer to another function. How can i write my "return" code and define
    the second function's "parameter" type?
    thanks.

    Code:
    #include <stdio.h>
    
    int creat2dArray()
    {
        int array[10][10]= {0};
        return (int**)array;
    }
    void 2dpointers(int *p[10])
    {
       ;  
    }
    int main(void)
    {
       2dpointers(creat2dArray());
        
       return 0;
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    array in create2dArray() will be destroyed when the funcion exits.

    Use malloc() to create the array on the heap.
    Last edited by zacs7; 10-22-2007 at 04:34 AM. Reason: Spelling :)

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    The syntax ain't pretty...
    Code:
    #include <stdio.h>
    
    int (*creat2dArray())[10]
    {
        static int array[10][10]= {0};
        return array;
    }
    void twodpointers(int (*p)[10])
    {
       ;  
    }
    int main(void)
    {
       twodpointers(creat2dArray());
        
       return 0;
    }
    If you mean to return a real array, you need to make it static to prevent it going out of scope when the function returns.

    As zacs7 says, you should use malloc for a far more flexible solution.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    thanks!

    got it! many thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Need to pass a pointer of a unknown sized 2d array
    By (Slith++) in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2007, 09:15 PM
  3. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21