Altering Array Sizes within Functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick

    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(c onst 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(a rray, 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(a rray, 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(c onst 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();
    }
  • John Harrison

    #2
    Re: Altering Array Sizes within Functions


    "Patrick" <googleaddress@ yahoo.co.uk> wrote in message
    news:94740f05.0 403040034.66037 45a@posting.goo gle.com...[color=blue]
    > 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[/color]
    elements.[color=blue]
    >
    > Is this possible with arrays (i realise it can be easily achieved with[/color]
    vectors)[color=blue]
    >[/color]

    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


    Comment

    • Mike Wahler

      #3
      Re: Altering Array Sizes within Functions


      "Patrick" <googleaddress@ yahoo.co.uk> wrote in message
      news:94740f05.0 403040034.66037 45a@posting.goo gle.com...[color=blue]
      > 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[/color]
      elements.[color=blue]
      >
      > Is this possible with arrays (i realise it can be easily achieved with[/color]
      vectors)[color=blue]
      >
      > Here is some code below outlining the general structure.
      >
      > Any help greatly appreciated,
      >
      > Pat
      >
      > #include <iostream>
      > #include <string>
      > #include <sstream>
      >
      > string arrayToString(c onst 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};[/color]

      If you want to be able to resize, then you'll have
      to use dynamic allocation, either with 'malloc()'
      or 'new[]'
      [color=blue]
      >
      > int arrayLength = 3;
      >
      > //print out the original elements of the array
      > cout << arrayToString(a rray, arrayLength) << "\n";
      >
      > //i want to change the size and content of "array"
      > function(array, arrayLength);[/color]

      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


      Comment

      Working...