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

Altering Array Sizes within Functions

Im having trouble with arrays
(1) I want to create an array myArray in "main".
(2) Then call a function from "main" which I pass myArray to.
(3) The function modifies the size and elements of myArray.
(4) When we return from the function, myArray now has a new size, and new elements.

Is this possible with arrays (i realise it can be easily achieved with vectors)

Here is some code below outlining the general structure.

Any help greatly appreciated,

Pat

#include <iostream>
#include <string>
#include <sstream>

string arrayToString(const int a[],const int length);
void function(int *array,int& arrayLength);

int main(int argc, char *argv[])
{ //int *array = new int[3];
//array[0]=0;array[1]=1;array[2]=2;

//i would prefer to declare the array this way initially
int array[] = {1,2,3};

int arrayLength = 3;

//print out the original elements of the array
cout << arrayToString(array, arrayLength) << "\n";

//i want to change the size and content of "array"
function(array,arrayLength);

//print out the new elements of the array
cout << arrayToString(array, arrayLength) << "\n";
return 0;
}

void function(int *array,int& arrayLength)
{ //change the size of array to length 2 say
arrayLength = 2;
//change the contents of the array

}

string arrayToString(const int a[],const int length)
{ ostringstream result;
result << "[";

for (int i=0;i<length-1;i++)
result << a[i] << ", ";

result << a[length-1] << "]";
return result.str();
}
Jul 22 '05 #1
2 1268

"Patrick" <go***********@yahoo.co.uk> wrote in message
news:94**************************@posting.google.c om...
Im having trouble with arrays
(1) I want to create an array myArray in "main".
(2) Then call a function from "main" which I pass myArray to.
(3) The function modifies the size and elements of myArray.
(4) When we return from the function, myArray now has a new size, and new elements.
Is this possible with arrays (i realise it can be easily achieved with vectors)


It's possible with dynamically allocated memory, which of course is how
vector does it. So why not use vector, do you have something against it?

With honest to goodness arrays, it is impossible.

john
Jul 22 '05 #2

"Patrick" <go***********@yahoo.co.uk> wrote in message
news:94**************************@posting.google.c om...
Im having trouble with arrays
(1) I want to create an array myArray in "main".
(2) Then call a function from "main" which I pass myArray to.
(3) The function modifies the size and elements of myArray.
(4) When we return from the function, myArray now has a new size, and new elements.
Is this possible with arrays (i realise it can be easily achieved with vectors)
Here is some code below outlining the general structure.

Any help greatly appreciated,

Pat

#include <iostream>
#include <string>
#include <sstream>

string arrayToString(const int a[],const int length);
void function(int *array,int& arrayLength);

int main(int argc, char *argv[])
{ //int *array = new int[3];
//array[0]=0;array[1]=1;array[2]=2;

//i would prefer to declare the array this way initially
int array[] = {1,2,3};
If you want to be able to resize, then you'll have
to use dynamic allocation, either with 'malloc()'
or 'new[]'

int arrayLength = 3;

//print out the original elements of the array
cout << arrayToString(array, arrayLength) << "\n";

//i want to change the size and content of "array"
function(array,arrayLength);


If you use 'malloc()' to allocate the array, look up
'realloc()'. If you use 'new[]' to allocate the array,
your function will need to use 'new[]' again to allocate
the new size, copy the old data to the new array, and
delete[] the old array.

But I'd just use a vector, it does all this 'dirty work'
for you.

-Mike
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: vegetax | last post by:
I i need a decorator that adds a local variable in the function it decorates, probably related with nested scopes, for example: def dec(func): def wrapper(obj = None): if not obj : obj = Obj()...
18
by: Joshua Neuheisel | last post by:
The following code compiles with gcc 3.2.2 and Visual C++ 6: #include <stdio.h> int main() { int a = {3, 4}; printf ("%d\n", 0); return 0; }
16
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated,...
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...
8
by: lovecreatesbeauty | last post by:
Hello experts, I have seen following the code snippet given by Marc Boyer (with slight changes by me for a better format), and have doubts on it. I am so grateful if you can give me your kindly...
9
by: JimmyKoolPantz | last post by:
IDE: Visual Studio 2005 Language: VB.NET Fox Pro Driver Version: 9.0.0.3504 Problem: I currently have a problem altering a DBF file. I do not get any syntax errors when running the program. ...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
9
by: JoeC | last post by:
I am crating a new version of my map game and my map will be a 2d array. I had problems trying to create a 2d array dynamically, in fact C++ won't let me do it. My question is how to create the...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.