Closed Thread
Results 1 to 4 of 4

Thread: variable length arrays in C

  1. #1
    rattlepanos is offline Newbie
    Join Date
    Feb 2007
    Posts
    4
    Rep Power
    0

    variable length arrays in C

    hi all
    i m currently having trouble handling variable length arrays in C.
    To be more specific :I am trying to create an array,let it be my_array[] that is 3 dimensional and its 2 first dimensions are fixed integers (let say m and n,already known integers),while its last dimension is dynamically allocated.

    The exact pseudo-code I want to implement is:

    for i=0:m-1
    for j=0:n-1
    read the value of my_array[i][j][0] that is the current length of the vector my_array[i][j][:]
    expand the my_array[i][j][:] vector by two extra slots
    assign a given value (let say x1) to the slot
    my_array[i][j][previous_index +1]
    and another given value (let say x2) to the slot
    my_array[i][j][previous_index +2]
    raise the value of my_array[i][j[0] by two
    end
    end

    In other words,I want to iterate through all the values that are stored on the first mxn matrix (the my_array[i][j][0]),read the respective values for each set of i and j, and then dynamically allocate more memory slots (2 more) for the corresponding vector (the my_array[i][j][:]).

    I dont know how (or even if) I could do this.If anyone has a clue,I d be grateful!

    Bye

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    I'm a bit confused by your pseduocode, and what you exactly want to do.
    It will be easier for us to help you, if you starts writing some of code, so, at least I, can understand what you want to do.
    Last edited by v0id; 05-02-2007 at 06:21 AM.

  4. #3
    rattlepanos is offline Newbie
    Join Date
    Feb 2007
    Posts
    4
    Rep Power
    0
    I m sorry about the confusion style I use.
    I ll try to describe in simple words:
    I want to create an initial 2-D array that has predetermined dimensions (say m by n) and all its values must be set to zero.
    The next step is running some code (i already got that) and some of the cells of this array are filled with certain values.Till here I m ok.

    The final part is go through a double for-loop (i=0:m-1) and (j=0:n-1) and read the respective value (that is,the my_array[i][j] value) and reserve the amount of memory slots this value indicates.For example,say the my_array[0][0] value is 100.I want to allocate 100 memory slots in the vector my_array[0][0][0:100].

    The final output of the whole code will be a 3-D array whose 1-st and 2-nd dimensions are explicitly set (m and n,respectively),while the number of slots for each pair of i and j (the third dimension) are computed dynamically,during run-time.Plainly put,the 'depth' of the array in the third dimension will depend on the set of i and j.So,the my_array[0][0][:] could have a length of 100,while the my_array[10][8][:] could have a length of 10 or 1.That's what Im trying to do...

    and im once more sorry about the complicated way of writing!

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143
    The short answer would be: C does not support variable sized arrays. However, you can create a struct that contains an array and a pointer to the same struct. Then work with it like a linked list of 2D arrays.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C Defining Data Arrays And Not BSS Arrays?
    By RhetoricalRuvim in forum C and C++
    Replies: 1
    Last Post: 09-02-2011, 07:59 AM
  2. Using malloc() and realloc() for a variable-length string
    By DarkLordofthePenguins in forum C and C++
    Replies: 6
    Last Post: 03-17-2011, 10:04 PM
  3. Replies: 3
    Last Post: 11-26-2010, 08:53 AM
  4. Mail Merge - Data Source with Variable Length
    By Ilikestring in forum Pascal and Delphi
    Replies: 5
    Last Post: 04-18-2010, 06:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts