Thread: Dynamic, multi-dimensional pointers!

  1. #16
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If you follow the pattern
    Code:
    thing = malloc ( N * sizeof *thing );
    you need not worry about getting it correct.

    So this
    Code:
    group_total = malloc (x * sizeof (long **));
    becomes this
    Code:
    group_total = malloc (x * sizeof *group_total);
    This
    Code:
    group_total[loop] = malloc (y * sizeof (long *));p
    becomes this
    Code:
    group_total[loop] = malloc (y * sizeof *group_total[loop]);
    And this
    Code:
    group_total[loop][loop2] = malloc (z * sizeof (long));
    becomes
    Code:
    group_total[loop][loop2] = malloc (z * sizeof *group_total[loop][loop2]);
    Then you can change **group_total to say double and have nothing to change but the declaration of group_total.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  2. #17
    Registered User
    Join Date
    May 2005
    Posts
    207
    Thanks Dave!

    And I appreciate your spelling it out for me too! :-)

    mw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array of pointers?
    By baniakjr in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2006, 09:46 AM
  2. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  3. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  4. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  5. dynamic memory allocation and returning pointers
    By sballew in forum C Programming
    Replies: 7
    Last Post: 11-03-2001, 03:21 PM