By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

hi all

P: n/a
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

Jan 22 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
prashu wrote:
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


In a single word: look for information about "realloc".

Kind regards.

DISCLAIMER:
The proposed solution could not be valid if you are a programmer of the
UNIVAC or Manchester's Baby computer, if you work for embedded systems
for dishwashers, if you do not test it, and, in general, it is not
valid at all. Moreover, it is sure that is not standard, and undefined
behaviour.

Jan 22 '06 #2

P: n/a
prashu a écrit :
I hv some confusion....can we change the size of array in runtime....if
ITYM 'have'
so,then how


If it was allocated with malloc(), yes, using realloc(). Isn't it a FAQ ?

You should also consider linked lists.

--
A+

Emmanuel Delahaye
Jan 22 '06 #3

P: n/a
Emmanuel Delahaye wrote:
If it was allocated with malloc(), yes, using realloc().
Or allocated with another realloc, that is the most practical in lots
of situations:

void foo ( void )
{

char *a=NULL;
int max_elems=0;

while(...)
{
...
/* increase size */
max_elems++;
a=realloc(a,max_elems);
...
}

You should also consider linked lists. From standard:

"An array type describes a contiguously allocated nonempty set of
objects with a particular member object type, called the element type".
Kind regards.

Jan 22 '06 #4

P: n/a
tmp123 a écrit :
You should also consider linked lists.

From standard:

"An array type describes a contiguously allocated nonempty set of
objects with a particular member object type, called the element type".


Yes, but I was giving a design alternative. Sorry if was not clear enough.

--
A+

Emmanuel Delahaye
Jan 22 '06 #5

P: n/a
prashu wrote:
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.
================================================== =============
Jan 22 '06 #6

P: n/a
prashu wrote:

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


Lets get some things clear up front. This is Usenet (not google
groups). Google provides a very poor interface to Usenet. See my
sig. below for ways to use that broken interface intelligently.

You don't 'join' a newsgroup, you just use it. Anybody can.
However, you do not make things hard for other readers by using
silly abbreviations like hv, pls, knw, etc. Also use proper
punctuation, which .... is not.

One normally reads a newsgroup for a while to see what the
practices are. This is called lurking. When posting a question
ensure that it is clear, and that the subject adequately describes
it. The following references will clue you in:

http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.greenend.org.uk/rjk/2000/06/14/quoting.html
http://www.i-hate-computers.demon.co.uk/
http://web.ukonline.co.uk/g.mccaugha...ks/uquote.html

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>

Jan 22 '06 #7

P: n/a
In message <43***************@yahoo.com>, CBFalconer
<cb********@yahoo.com> writes
prashu wrote:

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


Lets get some things clear up front. This is Usenet (not google
groups). Google provides a very poor interface to Usenet. See my
sig. below for ways to use that broken interface intelligently.

You don't 'join' a newsgroup, you just use it. Anybody can.
However, you do not make things hard for other readers by using
silly abbreviations like hv, pls, knw, etc. Also use proper
punctuation, which .... is not.


Actually the ellipsis (...) is perfectly valid punctuation, it normally
denotes that something has been removed.
--
------------------------------------------------------------------------
TQ - The Voice Of insanity

------------------------------------------------------------------------

Jan 22 '06 #8

P: n/a
u can only do that by using link list. u can add links to increase the
size of array..but for that u cant take normal c array n have to define
array as a class. read data structures.

Jan 22 '06 #9

P: n/a
as*********@gmail.com wrote:
u can only do that by using link list.
Only do what? Please provide context, people might not have seen and
might never see the article you are responding to. Please see
http://cfaj.freeshell.org/google/ for details of how to do this.

Also please don't use contractions like "u" instead of you, they make is
far harder for others to read your posts.
u can add links to increase the
size of array..but for that u cant take normal c array n have to define
array as a class. read data structures.


C does not have classes, you might be thinking of a very different
language called C++.

In any case, there are other ways to do something very like expanding
arrays, such as using allocated memory and realloc to change the size as
others have already pointed out.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 22 '06 #10

P: n/a
Tony Quinn <to**@sixpints.demon.co.uk> writes:
In message <43***************@yahoo.com>, CBFalconer
<cb********@yahoo.com> writes
prashu wrote:

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


Lets get some things clear up front. This is Usenet (not google
groups). Google provides a very poor interface to Usenet. See my
sig. below for ways to use that broken interface intelligently.

You don't 'join' a newsgroup, you just use it. Anybody can.
However, you do not make things hard for other readers by using
silly abbreviations like hv, pls, knw, etc. Also use proper
punctuation, which .... is not.


Actually the ellipsis (...) is perfectly valid punctuation, it
normally denotes that something has been removed.


An ellipsis consists of 3 periods. "...." could be an ellipsis
followed by a period, but since it's not being used correctly anyway,
it's more likely just a bunch of dots.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 22 '06 #11

P: n/a
Tony Quinn wrote:
In message <43***************@yahoo.com>, CBFalconer
<cb********@yahoo.com> writes
prashu wrote:

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

<snip>
silly abbreviations like hv, pls, knw, etc. Also use proper
punctuation, which .... is not.


Actually the ellipsis (...) is perfectly valid punctuation, it
normally denotes that something has been removed.


Yes, but these consist of exactly 3 dots (as you have used
correctly), not four (this is c.l.c, after all). ;-)

Cheers

Vladimir

Jan 22 '06 #12

P: n/a
Keith Thompson wrote:
Tony Quinn <to**@sixpints.demon.co.uk> writes:
<cb********@yahoo.com> writes
prashu wrote:

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

Lets get some things clear up front. This is Usenet (not google
groups). Google provides a very poor interface to Usenet. See my
sig. below for ways to use that broken interface intelligently.

You don't 'join' a newsgroup, you just use it. Anybody can.
However, you do not make things hard for other readers by using
silly abbreviations like hv, pls, knw, etc. Also use proper
punctuation, which .... is not.


Actually the ellipsis (...) is perfectly valid punctuation, it
normally denotes that something has been removed.


An ellipsis consists of 3 periods. "...." could be an ellipsis
followed by a period, but since it's not being used correctly anyway,
it's more likely just a bunch of dots.


Why all this prattling about ellipsis? The point was to catch
Prashu (a potential googler and abbreviator) early and train
him/her into the acceptable practices on usenet. Reinforcement and
clarification of that message would be more productive.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Jan 22 '06 #13

P: n/a
CBFalconer wrote:
Keith Thompson wrote:
Tony Quinn <to**@sixpints.demon.co.uk> writes:
<cb********@yahoo.com> writes
prashu wrote:
>
> 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

Lets get some things clear up front. This is Usenet (not google
groups). Google provides a very poor interface to Usenet. See my
sig. below for ways to use that broken interface intelligently.

You don't 'join' a newsgroup, you just use it. Anybody can.
However, you do not make things hard for other readers by using
silly abbreviations like hv, pls, knw, etc. Also use proper
punctuation, which .... is not.

Actually the ellipsis (...) is perfectly valid punctuation, it
normally denotes that something has been removed.


An ellipsis consists of 3 periods. "...." could be an ellipsis
followed by a period, but since it's not being used correctly anyway,
it's more likely just a bunch of dots.


Why all this prattling about ellipsis? The point was to catch
Prashu (a potential googler and abbreviator) early and train
him/her into the acceptable practices on usenet. Reinforcement and
clarification of that message would be more productive.


I rest my case.

--
================================================== =============
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.
================================================== =============
Jan 22 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.