Creating a function to return 2d arrays help please

This is a discussion on Creating a function to return 2d arrays help please within the C Programming forums, part of the General Programming Boards category; Hello, I am trying to create a function outside of the main method which creates a 2d array based on ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    21

    Creating a function to return 2d arrays help please

    Hello,

    I am trying to create a function outside of the main method which creates a 2d array based on two numbers given to it. I want to be able to run this function multiple times to create a number of arrays to be accessed later.

    Code:
    char *room(int xRoom, int yRoom){
          
        static char charRoom[xRoom][yRoom];       
        return charRoom;
    }
    At the moment it throws up compiler errors as the array length isn't constant. Am I going about what I want to do the right way or is there another? Or if I am how do I continue from this point onwards?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You will have to dynamically create the 2D array and return that.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    21
    What do you mean?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,501
    Read this link: Question 6.16

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Here, run the code below as an example of why you can't do that...

    Code:
    #include <stdio.h>
    
    int* MyFunction(int a, int b, int c)
      {  static int array[3];
         array[0] = a;
         array[1] = b;
         array[2] = c;
         return array;  } // return a pointer.
    
    
    int main (void)
      { int *a1, *a2;  // int pointers
    
        printf("calling a1 = MyFunction(10,20,30);\t");
        a1 = MyFunction(10,20,30);
        printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        printf("calling a2 = MyFunction(100,200,300);\t");
        a2 = MyFunction(100,200,300);
        printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);
    
        printf("\nLooks good, except...\t"); 
        printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        getchar();
        return 0; }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. change and return arrays from a function
    By cuizy in forum C Programming
    Replies: 2
    Last Post: 05-27-2009, 03:52 PM
  2. Functions that return arrays
    By Scarecrowm in forum C Programming
    Replies: 4
    Last Post: 10-21-2007, 02:08 AM
  3. Replies: 8
    Last Post: 01-23-2007, 01:31 PM
  4. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  5. Creating a Carriage-Return style Break in a program
    By JoeMomma5000 in forum C Programming
    Replies: 2
    Last Post: 10-29-2002, 12:06 AM

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