473,513 Members | 2,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2164
"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
2757
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
575
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 print_r cmd any suggestions would be great. Thanks much Todd //avShed array Array ( => Array ( => 1 => 08:00 ) => Array ( => 1 => 08:05 ) ...
15
5156
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
3465
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( "name", "title", "reports to user id", "start date in the format: mm/dd/yyyy" ) ); How can I display this hierarchy in simple nested <li> tags in...
12
55528
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 also removed) So far I have (val is value, ar is array, returns new array):
8
10199
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 it is assigned a text literal?? HOW can I change the array value to upper case then? What other method exists for arrays? Ex: var GridArrayName1...
58
10050
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 code... TCHAR myArray; DoStuff(myArray);
104
16853
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 that array Could you show me a little example how to do this? Thanks.
7
3168
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 up looking for characters that wrap around the stripped characters and it ends up as a recursive ordeal that fails to identify a poorly constructed...
17
7212
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 to show the array data to the end user. Can I do that? How?
0
7565
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7128
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5704
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5103
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4759
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3255
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.