Thread: Memmory Allocation

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Memmory Allocation

    How would I allocate memory for 2d array with 3 rows and 8 coulmns?

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    <type> **myArray;

    myArray = malloc( sizeof( <type> ) * 24 );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest
    Hi Quzah,
    Just wondering if you use a double pointer because of the two-dimensional array?

  4. #4
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    Code:
    #include "stdio.h"
    #include "stdlib.h"
    #include "time.h"
    
    #define ROWS 3
    #define COLS 8
    
    void fill_array (int **);    /* fills array with random numbers */
    
    void print_array (int **);   /* prints array */
    
    int main (void)    
    {
     int i;
     int **table;
    
     table = malloc (ROWS * sizeof (int *));  /* allocates memory for pointers to each row (actually to first element in each row) */
    
     for (i = 0; i < COLS; i++)
      table[i] = malloc (COLS * sizeof (int));  /* allocates memory for each row */
    
     fill_array (table);
    
     print_array (table);
    
     return 0;
    }
    
    void fill_array (int **table)
    {
     int row, col;
     
     srand (time (NULL));
    
     for (row = 0; row < ROWS; row++)
      for (col = 0; col < COLS; col++)
       *(*(table + row) + col) = rand() % 10;   /* same as table[row][col] = rand() % 10; */
    
     return;
    }
    
    void print_array (int **table)
    {
     int row, col;
    
     for (row = 0; row < ROWS; row++)
     {
      for (col = 0; col < COLS; col++)
       printf ("%4d", *(*(table + row) + col));
    
      printf ("\n");
     }
    
     return;
    }
    Last edited by PutoAmo; 04-16-2002 at 06:42 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Heh, Quzah's answer is way off, and PutoAmo's contains a bug

    In main, it should be
    Code:
    table = malloc (ROWS * sizeof (int *));  /* allocates memory for pointers to each row (actually to first element in each row) */
    
     for (i = 0; i < ROWS; i++) // was COLS
      table[i] = malloc (COLS * sizeof (int));  /* allocates memory for each row */

    #include "stdio.h"
    Should be
    #include <stdio.h>

    > *(*(table + row) + col) = rand() % 10; /* same as table[row][col] = rand() % 10; */
    Since there is no difference to the compiler, I usually go with the [row][col] syntax.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic allocation from 1 instead of zero
    By cfdprogrammer in forum C Programming
    Replies: 27
    Last Post: 04-28-2009, 08:21 AM
  2. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  3. Allocation from "static array"
    By Petike in forum C Programming
    Replies: 6
    Last Post: 10-12-2008, 03:17 AM
  4. redundant allocation
    By George2 in forum C++ Programming
    Replies: 22
    Last Post: 03-06-2008, 06:43 PM
  5. Dynamic allocation (I thought it would crash)
    By Baaaah! in forum C Programming
    Replies: 16
    Last Post: 11-30-2005, 05:10 PM