473,373 Members | 1,410 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,373 software developers and data experts.

hi all

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
13 2594
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.