|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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 |
| Sponsored Links |
|
|
|
|||
|
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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java:Tutorial - Arrays | John | Java Tutorials | 1 | 05-17-2008 11:56 AM |
| Using input to change a variable? | Plutonic | Java Help | 3 | 07-17-2007 05:02 PM |
| Arrays | clookid | PHP Tutorials | 1 | 01-11-2007 09:30 PM |
| Java:Tutorial - The Variable | John | Java Tutorials | 0 | 12-09-2006 10:59 AM |
| Variable Names | Paradine | PHP Forum | 3 | 11-03-2006 12:29 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |