Thread: dynamic array

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    dynamic array

    I was reading online for stuff about opengl.(I need help with 3 dimentional arrays). I learned what I needed(i rememberd a thread that helped me(thanks Thantos)) Anyway, I found this on a site, I know this is wrong is there any validity in this statement??
    The arrays you have learnt so far are called static arrays because they have a fixed number of elements. A dynamic array lets you add elements to an array simply by using them.

    To declare a dynamic array you must not put a number in the square brackets of the declaration.

    int a[];

    Now just use it in the same way as a normal array. You can't however set the value of an element if you haven't first set the value of the one before it.

    int a[];
    a[0] = 12;
    a[1] = 23;
    a[2] = 34;

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I don't think there is anything valid about that statement. The closest thing I know of to that is a map in c++ which will allow you to do that.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I thought so. I just tested it and it didn't work. If you are interested here is the link

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Mail them and tell them they're wrong.

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

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I was thinking about this and only way I could possibly think of it working (mind you illegally) is if
    Code:
    int a[];
    Got reintruppted as
    Code:
    int *a;
    Of course then its pointing to some address willy nilly and can cause problems out the wazoo.

  6. #6
    Quote Originally Posted by linuxdude
    I was reading online for stuff about opengl.(I need help with 3 dimentional arrays). I learned what I needed(i rememberd a thread that helped me(thanks Thantos)) Anyway, I found this on a site, I know this is wrong is there any validity in this statement??
    The whole thing is certainely not about the C-language.

    To manage dynamic arrays, you have 2 solutions.
    • Create a linear array.
      Say 2 dimendions DIM1 and DIM2:
      Code:
      type_s *p_array = malloc (sizeof *p_array * DIM1 * DIM2);
      and home made calculated accessors
      Code:
      type_s *get_addr (DIM1, DIM2, size_t x, size_t y );
      Note that some context structure with the dimensions could help.
    • Create nested arrays of pointers. The data is non linear, but it allows the subscript notation.
      Code:
      /* 2Ds : DIM1 x DIM2 */
      type_s **pp = malloc (sizeof *pp * DIM1);
      
      for (i = 0; i < DIM1; i++)
      {
         pp[i] = malloc (sizeof *pp[i] * DIM2);
      }

    Of course, the usual security stuffs have to be added, and the data must be freed when no longer used.

    Isn't all of that in some FAQ ?
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2005, 12:13 AM
  2. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 01:59 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM