title: C - Moving 2D array between functions
body: <p>So basically, I am currently learning functions and pointers. I understand that I can't directly return an array from a function, and instead need to use pointers.</p>
  
  <p>Form the code below, you can see that I am using the function <code>char * generate_2D_array( )</code> in order to create a 2D array. I am then looking for a way in which I can make this array work in my function called <code>Menu()</code>. My overall goal at this time is to then be able to print the <code>2D array</code>.</p>
  
  <p>When I generate the 2D array and print it from within the same method, it works fine. I get a <code>12 x 12</code> grid of dots. However, with the code I have, I am getting a <code>12 x 12</code> grid of random characters and spaces. Please help me to fix this! </p>
  
  <p>Current output is <a href="http://i.imgur.com/M0xpPMt.png" rel="nofollow">here</a> </p>
  
  <p>Here is my code:</p>
  
  <pre><code>    #include &lt;stdio.h&gt;
      #include &lt;stdlib.h&gt;
      #include &lt;time.h&gt;
  
      int rand_num_gen( )
      {
          srand(time(NULL));
          int randNum;
          randNum = (rand() % 12);
          printf("%d", randNum);
          return randNum;
      }
  
      char rand_char_gen( )
      {
          char randChar = ' ';
          int randNum = 0;   
          randNum = 26 * (rand() / (RAND_MAX + 1.0));
          randNum = randNum + 97;
          randChar = (char) randNum; 
          return randChar;
      }
  
  
      char * generate_2D_array( )
      {
          srand(time(NULL));
          char grid[12][12];
          char *p;
          p = *grid;
          int i, j;
          for (i = 0;i &lt; 12; i++)
          {
              for (j = 0; j &lt; 12; j++)
              {
                      grid[i][j] = '.'; //rand_char_gen( );
              }
          }
          return grid;
      }
  
      void print_array(char * array)
      {
          int i, j;
          for (i = 0; i &lt; 12; i++)
          {
              for (j = 0; j &lt; 12; j++)
              {
                printf("%c  ", *array++);
              }
              printf("\n\n");
          }    
      }
  
  
  
      Menu()
      {            
                  char *myArray; 
                  myArray = generate_2D_array( );
                  print_array(myArray);
  
                  printf("\nChoose a catagory: ");
                  printf("\nPleease select your number of choice...");
                  printf("\n1. Animals");
                  printf("\n2. Colors");
                  printf("\n3. Planets");
                  printf("\n4. Weekdays");
  
                  int valid = 0;
                  while(valid == 0)
                  {
                              printf("\n\nYour selection: ");
                              int catagory;
                              scanf("%d", &amp;catagory);
  
                              if(catagory == 1)
                              {
                                          printf("\n\nPlaceholder\n");
                                          char word1[] = "dog";
                                          int length = sizeof(myArray);
                                          printf("myArray = %d", length);
                                          valid++;  
                              }
                              else if(catagory == 2)
                              {
                                          printf("\n\nPlaceholder\n");
                                          valid++;         
                              }
                              else if(catagory == 3)
                              {
                                          printf("\n\nPlaceholder\n");
                                          valid++;
                              }
                              else if(catagory == 4)
                              {
                                          printf("\n\nPlaceholder\n");
                                          valid++;
                              }
                              else if(catagory &gt; 4)
                              {
                                          printf("\n\nInvalid input. Please select again.\n");                                    
                              }
                              else if(catagory &lt; 1)
                              {
                                          printf("\n\nInvalid input. Please select again.\n");
                              }
                  }                
      }       
  </code></pre>
  
  <p>EDIT:
  Okay so i fixed the issue. All i needed to do was make my array called Grid a static array. :) </p>