Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-02-2007, 08:42 AM
rattlepanos rattlepanos is offline
Newbie
 
Join Date: Feb 2007
Posts: 4
Rep Power: 0
rattlepanos is on a distinguished road
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-02-2007, 09:11 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

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.
__________________
05-03-2007 - 11-13-2008

Last edited by v0id; 05-02-2007 at 09:21 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-02-2007, 09:37 AM
rattlepanos rattlepanos is offline
Newbie
 
Join Date: Feb 2007
Posts: 4
Rep Power: 0
rattlepanos is on a distinguished road
Default

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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-03-2007, 12:35 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,418
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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


All times are GMT -5. The time now is 03:01 AM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads