Returning an array from a fuction

Hi,

I am in a small bit of confusion, I would like to know how I might be able to return an entire array from a function, I am passing in a an array to a function and changing values in the array according to some if staement and then I want to return the entire array but I am not quiet sure how I can do this? Should I use pointers or reference the array?

Thanks,
Alan


Comments

  • : Hi,
    :
    : I am in a small bit of confusion, I would like to know how I might be able to return an entire array from a function, I am passing in a an array to a function and changing values in the array according to some if staement and then I want to return the entire array but I am not quiet sure how I can do this? Should I use pointers or reference the array?
    :
    : Thanks,
    : Alan
    :
    :
    :

    you didn't say what kind of array but something like this
    [code]
    int * foo(int array[])
    {
    return array;
    }
    [/code]

  • : : Hi,
    : :
    : : I am in a small bit of confusion, I would like to know how I might be able to return an entire array from a function, I am passing in a an array to a function and changing values in the array according to some if staement and then I want to return the entire array but I am not quiet sure how I can do this? Should I use pointers or reference the array?
    : :
    : : Thanks,
    : : Alan
    : :
    : :
    : :
    :
    : you didn't say what kind of array but something like this
    : [code]
    : int * foo(int array[])
    : {
    : return array;
    : }
    : [/code]
    :
    :
    I am declaring an array of ints, so I dont have to assign a name to it just return a pointer to the array? Like the code above?

    Thanks
  • : : : Hi,
    : : :
    : : : I am in a small bit of confusion, I would like to know how I might be able to return an entire array from a function, I am passing in a an array to a function and changing values in the array according to some if staement and then I want to return the entire array but I am not quiet sure how I can do this? Should I use pointers or reference the array?
    : : :
    : : : Thanks,
    : : : Alan
    : : :
    : : :
    : : :
    : :
    : : you didn't say what kind of array but something like this
    : : [code]
    : : int * foo(int array[])
    : : {
    : : return array;
    : : }
    : : [/code]
    : :
    : :
    : I am declaring an array of ints, so I dont have to assign a name to it just return a pointer to the array? Like the code above?
    :
    : Thanks
    :

    Yes. Depending of the functionality maybe you don't need to return anything, sence the array is passed by reference. It's values will be changed anyway.
    You will only need to return a pointer if there is a reason for it, like for example a pointer to a specific element in the array (like it is done in the strstr() function).
    Remember that you can only return pointers to memory allocated outside the function, or dynamicly allocated memory. You shouldn't return a pointer to a local array, sence it's memory location isn't valid outside the function's namespace range.

  • : Remember that you can only return pointers to memory allocated outside the function, or dynamicly allocated memory. You shouldn't return a pointer to a local array, sence it's memory location isn't valid outside the function's namespace range.
    :

    Funny I find this thread, because I have a very similar question:
    A char* is basically an array. If you pass it as an argument, you only pass a pointer (correct me if I'm wrong). So, this code should be invalid:

    char* testfunkt()
    {
    char* temp = "Teststring
    ";
    return temp;
    }

    int main()
    {
    char* temp2 = testfunkt();
    return 0;
    }

    Now, in the main-method, the temp2-pointer should be invalid... is this correct?

    MedO
  • [b][red]This message was edited by stober at 2004-12-10 15:51:25[/red][/b][hr]
    : : Remember that you can only return pointers to memory allocated outside the function, or dynamicly allocated memory. You shouldn't return a pointer to a local array, sence it's memory location isn't valid outside the function's namespace range.
    : :
    :
    : Funny I find this thread, because I have a very similar question:
    : A char* is basically an array. If you pass it as an argument, you only pass a pointer (correct me if I'm wrong). So, this code should be invalid:
    :
    : char* testfunkt()
    : {
    : char* temp = "Teststring
    ";
    : return temp;
    : }
    :
    : int main()
    : {
    : char* temp2 = testfunkt();
    : return 0;
    : }
    :
    : Now, in the main-method, the temp2-pointer should be invalid... is this correct?

    [blue]Your code is correct because its only passing pointers, not arrays. But since the string resides in read-only memory, the function should return const char* to let main() know that the string cannot be changed.[/blue]
    [code]
    const char* testfunkt()
    {
    const char* temp = "Teststring
    ";
    return temp;
    }
    [/code]

    This example, however, is illegal because, now, temp is NOT a pointer, but an array that was allocated on the stack in testfunkt(). The memory for that array disappears as soon as the function returns, and main() is receiving a pointer to invalid memory location.
    [code]
    char* testfunkt()
    {
    char temp[] = "Teststring
    ";
    return temp;
    }
    [/code]

    :
    : MedO
    :





  • Hi,

    I am in a small bit of confusion, I would like to know how I might
    be able to return an entire array from a function, I am passing in a
    an array to a function and changing values in the array according to
    some if staement and then I want to return the entire array but I am
    not quiet sure how I can do this? Should I use pointers or reference
    the array?
    the program is here

    int partition(int a1[], int pivot, int first)
    {
    int temp;
    int x = pivot;
    int i =first-1;
    for(int j = first; j<=pivot-1; j++)
    {
    if(a1[j]<=x)
    i=i+1;
    temp = a1[j];
    a1[j] = a1[i];
    a1[i] =temp;
    }
    temp = a1[i+1];
    a1[i+1] = pivot;
    pivot = temp;
    return(i+1);
    }
    void quicksort(int a1[],int first,int pivot)
    {
    if(first<pivot)
    {
    int q = partition(a1,first,pivot);
    quicksort(a1,first,q-1);
    quicksort(a1,q+1,pivot);
    }
    return(a1);
    }
    please help me in correcting the problem
    Thanks,
    mamoorkhan

  • This post has been deleted.
  • This post has been deleted.
  • This post has been deleted.
  • This response is total rubbish.

    Arrays are second-class in C++ and all variable persistence is subject to scope. This means if you employ the method suggested your function will return a pointer to an int which has fallen out of scope and can be overwritten.

    Not only that but the explanation is muddled and incorrect. The best thing to do with this answer is ignore it.

    I wish people would NOT waste everybody's time with hazarding answers to technical queries they do not understand.
This discussion has been closed.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories