468,738 Members | 1,686 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

int array

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
Nov 14 '05 #1
5 2013
"Matthew Jakeman" <m.*******@nospam.lancaster.ac.uk> wrote in
news:cg**********@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 ->
--
Nov 14 '05 #2
In article <Xn********************************@130.133.1.4> , Mark A.
Odell wrote:
"Matthew Jakeman" <m.*******@nospam.lancaster.ac.uk> wrote in
news:cg**********@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.


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 Leek | 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
Nov 14 '05 #3
Rob van der Leek <ro***********@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.
Nov 14 '05 #4
In <cg**********@alliance.lancs.ac.uk> "Matthew Jakeman" <m.*******@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: Da*****@ifh.de
Nov 14 '05 #5
Rob van der Leek wrote:
Mark A. Odell wrote:
"Matthew Jakeman" <m.*******@nospam.lancaster.ac.uk> wrote in
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.


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
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Brian | last post: by
2 posts views Thread by Stormkid | last post: by
8 posts views Thread by vcardillo | last post: by
12 posts views Thread by Sam Collett | last post: by
8 posts views Thread by Mike S. Nowostawsky | last post: by
104 posts views Thread by Leszek | last post: by
7 posts views Thread by Jim Carlock | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.