Home > Archive > C > August 2004 > int array
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Matthew Jakeman 2004-08-30, 3:55 pm |
| Hi, if i define an int array globally as follows :
int test[] ;
Is it possible to set the size of this array inside a function later in the
code ?
TIA
Matt
| |
| Mark A. Odell 2004-08-30, 3:55 pm |
| "Matthew Jakeman" <m.jakeman@nospam.lancaster.ac.uk> wrote in
news:cgvb17$kcb$1@alliance.lancs.ac.uk:
> Hi, if i define an int array globally as follows :
>
> int test[] ;
>
> Is it possible to set the size of this array inside a function later in
> the code ?
No.
--
- Mark ->
--
| |
| Rob van der Leek 2004-08-30, 3:55 pm |
| In article <Xns9555663793102CopyrightMarkOdell@130.133.1.4>, Mark A.
Odell wrote:
> "Matthew Jakeman" <m.jakeman@nospam.lancaster.ac.uk> wrote in
> news:cgvb17$kcb$1@alliance.lancs.ac.uk:
>
>
> No.
If the size of the array is only known at run-time, you need to allocate
memory for the array at run-time. This is called "dynamic allocation"
(if you specify the array size at compile-time (e.g. int test[3]) it is
called "static allocation").
I advice you to consult a C book on these topics.
Regards,
--
Rob van der L | rob(at)ricardis(dot)tudelft(dot)nl
Ricardishof 73-A | http://www.ricardis.tudelft.nl/~rob
2614 JE Delft, The Netherlands
+31 (0)6 155 244 60
| |
| Christopher Benson-Manica 2004-08-30, 3:55 pm |
| Rob van der L <robvanderl @yahoo.com> spoke thus:
> If the size of the array is only known at run-time, you need to allocate
> memory for the array at run-time. This is called "dynamic allocation"
> (if you specify the array size at compile-time (e.g. int test[3]) it is
> called "static allocation").
I believe the size of OP's array is known at compile time; the issue
is that there is no way to determine the size of an array outside the
scope in which it is declared, which seems to be what OP wants to do.
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
| |
| Dan Pop 2004-08-30, 8:55 pm |
| In <cgvb17$kcb$1@alliance.lancs.ac.uk> "Matthew Jakeman" <m.jakeman@nospam.lancaster.ac.uk> writes:
>Hi, if i define an int array globally as follows :
> ^^^^^^
>int test[] ;
>
>Is it possible to set the size of this array inside a function later in the
>code ?
An object *definition* is a declaration that also allocates memory for
that object. How can the compiler allocate memory for test[] without
knowing its size?
What you can do is *declaring* test[] as a global without specifying its
size:
extern int test[];
and define it elsewhere in your code. Note that you cannot use
sizeof test until the compiler has seen the actual definition or a
declaration also specifying the size.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
| |
| CBFalconer 2004-08-30, 8:55 pm |
| Rob van der L wrote:
> Mark A. Odell wrote:
>
> If the size of the array is only known at run-time, you need to
> allocate memory for the array at run-time. This is called
> "dynamic allocation" (if you specify the array size at compile-
> time (e.g. int test[3]) it is called "static allocation").
>
> I advice you to consult a C book on these topics.
Note that Mark replied to a query that dealt with 'globally'
defined arrays, which can be loosely interpreted to mean
statically allocated.
--
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
"We have always known that heedless self-interest was bad
morals. We now know that it is bad economics" - FDR
|
|
|
|
|