473,231 Members | 1,865 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,231 software developers and data experts.

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

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.