hi all

P

prashu

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
 
T

tmp123

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

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.
 
E

Emmanuel Delahaye

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.
 
T

tmp123

Emmanuel said:
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.
 
E

Emmanuel Delahaye

tmp123 a écrit :
"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.
 
P

pemo

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.
===============================================================
 
C

CBFalconer

prashu said:
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.mccaughan/g/remarks/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/>
 
T

Tony Quinn

CBFalconer said:
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.
 
A

ashishcoolz

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.
 
F

Flash Gordon

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.
 
V

Vladimir S. Oka

said:
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
 
K

Keith Thompson

Tony Quinn said:
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.
 
C

CBFalconer

Keith said:
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/>
 
P

pemo

CBFalconer said:
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.
===============================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top