Re: arrays, malloc

From: Barry Schwarz (schwarzb_at_deloz.net)
Date: 03/06/04


Date: 6 Mar 2004 23:08:55 GMT

On Sat, 6 Mar 2004 20:59:34 +0100, "Kyle" <a@b.c> wrote:

>what's the easy way of malloc'ing 2 dimensional array
>
>i mean i may always do
>
>#define GET(array,i,j,size) array[i+size*j]
>
>and use 1 dim array, but if it's (mallocing 2 dim) not too complicated i
>will be very happy <:
>
The traditional method of allocating a 2D array of type T when the
size of each dimension (number of rows and columns) is not known until
run time is
     T **array = malloc(rows * sizeof *array);
     for (i = 0; i < rows; i++)
          array[i] = malloc(columns * sizeof *array[i]);

You can then refer to elements of the array as array[j][k] anywhere a
variable of type T could be used.

Naturally, all malloc calls should be checked for success.

<<Remove the del for email>>



Relevant Pages

  • Re: Error on UBound with Dynamic Array
    ... ReDim Preserve arrSplit+ 1) ... is that arrSplit has not yet been dimmed as having any dimension. ... need to use a dynamic array and may be checking the boundries, ... Public Function Split(csvString As String) As Variant ...
    (microsoft.public.access.modulesdaovba)
  • Re: Program Fails When Parameter Fixed Constants are Changed (F77) ??
    ... subroutine and then call DCpZeros passing VxGrid, nRdim, and nTHdim. ... Then DCpZeros can dimension VxGrid (nRdim, ... (PS. I'm not concerned or worried about the array bounds exceeded! ...
    (comp.lang.fortran)
  • Re: Playing with multidimensional array!?
    ... In traditional compiler languages, rows and columns are differentiated by ... the fact that a two dimensional array occupies contiguous storage locations. ... fact that redim can affect only the last dimension. ... consider these the first dimension and the second dimension rather than row ...
    (microsoft.public.scripting.vbscript)
  • Re: Array of pointer Vs Pointer to Array
    ... that points to an array. ... Now 'pa' is a pointer form of accessing the values in 'matrix'. ... that depends on a variety of things - the size of the other dimension, ... the regular indexing style is a good first guess, ...
    (comp.lang.c)
  • Re: initializing an array of user-defined data types
    ... After allocating it, I want to initialize it so that after ... because your array is allocatable. ... You could even use NULLby having a component of piece be a pointer, ...
    (comp.lang.fortran)