prashu said:
hello,
Ths is Prashant....i am a new joinee....
I hv some confusion....can we change the size of array in
runtime....if so,then how
pls let me knw the answer
thanx
I'm *perfectly certain* that you want the malloc/realloc route as suggested
here already, *but*, if you had a c99 compliant compiler, you could *sort of
do* what you're asking - "can we change the size of array in runtime" - by
using:
void func(int n)
{
int arr[n + 3];
// Use arr here
// ...
}
The runtime 'entity' arr's size can-change/changes according to the value of
'n' at each invocation.
Actually, as arr is created upon entry and destroyed on exit, I think you
could argue that it doesn't change, i.e., that as it's created, its size if
fixed, BUT, that you can cause any manifestation of it to have whatever size
you want (as long as n is an integer type). So, *if* you *weren't* wanting
the contents of your variable sized array to retain its contents, but wanted
something akin to malloc's flexibility in certain situations, you could use
it - and save the malloc/realloc/free calls.
I've not used it, but gcc has something like this [gnu's own variant] - so,
*if* you're using gcc, you could perhaps take a look at
http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html, as it might be what
you're looking for (although I doubt it)
--
===============================================================
In an attempt to reduce 'unwanted noise' on the 'signal' ...
Disclaimer:
Any comment/code I contribute might =NOT= be 100% portable, nor
semantically correct [read - 'not 100% pedantically correct'].
I don't care too much about that though, and I reckon it's the
same with most 'visitors' here. However, rest assured that any
'essential' (?) corrections WILL almost certainly appear v.soon
[read - 'to add noise as they see fit, *a pedant* will be along
shortly'].
WARNINGS: Always read the label. No beside-the-point minutiae
filter supplied. Keep away from children. Do not ignite.
===============================================================