Altering Array Sizes within Functions

Discussion in 'C++' started by Patrick, Mar 4, 2004.

  1. Patrick

    Patrick Guest

    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 << ", ";

    result << a[length-1] << "]";
    return result.str();
    }
    Patrick, Mar 4, 2004
    #1
    1. Advertising

  2. "Patrick" <> wrote in message
    news:...
    > 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
    John Harrison, Mar 4, 2004
    #2
    1. Advertising

  3. Patrick

    Mike Wahler Guest

    "Patrick" <> wrote in message
    news:...
    > 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
    Mike Wahler, Mar 4, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,581
    Victor Bazarov
    Jun 21, 2004
  2. Replies:
    2
    Views:
    336
    Dave Thompson
    Feb 27, 2006
  3. jimgardener
    Replies:
    3
    Views:
    674
    Arne Vajhøj
    Dec 3, 2008
  4. nroberts

    Array sizes in function parameters.

    nroberts, Feb 25, 2012, in forum: C Programming
    Replies:
    3
    Views:
    570
    Ian Collins
    Feb 25, 2012
  5. Les Cargill

    Array sizes and const

    Les Cargill, Jun 2, 2012, in forum: C Programming
    Replies:
    49
    Views:
    986
    Joe keane
    Jun 9, 2012
Loading...

Share This Page