#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    5
    Rep Power
    0

    Dynamic 2d Arrays


    I believe that the following code is correct for creating a dynamic 2d array K(which is a **int) :

    int **K;
    int n;

    /* Create a pointer to each row of the matrix. */
    K = (int **)malloc(size * sizeof(int *));

    /* Create each row of the matrix */
    for(n = 0; n < size; n++)
    K[n] = (int *)malloc(size * sizeof(int));

    My question is: If I refer to an element in K, is it valid to use subscripts(ex. K[1][1])? Is the memory region created by my code analgous to the memory region generated by a statitic 2d
    declaration(ex. int K[size][size])? I have had no problems yet, however I am worried that I will find a problem later.
    Thanks
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,594
    Rep Power
    4207
    http://www.eskimo.com/~scs/C-faq/q6.16.html
    The FAQ on this page answers both your questions. Hope this helps :)
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    5
    Rep Power
    0

    Cool


    Thank you! Excellent response!(That second method in the FAQ, however, looks horrible.:D )

IMN logo majestic logo threadwatch logo seochat tools logo