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
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.
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!
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks