0

I am trying to learn pointers with arrays.
I want to do this with the function below:

int* ArrayManipulator(int* arrayPoiner, const int size)

to reverse the array list:

   #include <iostream>

using namespace std;

int* ArrayManipulator(int* arrayPoiner, const int size)
{
    int i=0;
    int tt[size];
    for (i=0;i<=size;i++)
    {
        cout<<arrayPoiner[size-i]<<endl;
        cout<<&arrayPoiner[size-i]<<endl;

        tt[i]=arrayPoiner[size-i];
    }
    return tt;
}


int main()
{
    int t[]={1,2};
    int *ReversedArray;
    ReversedArray = ArrayManipulator(t,1);
    int i=0;
    for (i=0;i<2;i++)
    {
        cout<< ReversedArray[i]<<endl;
    }
  return 0;
}

I am receiving error at cout<< ReversedArray[i]; cout<< &ReversedArray[i]; is printing the address of the value but I am unable to print the value - codeblocks error out there

EDIT Code: enter image description here

I want the final two lines to be 2 and 1, where am I going wrong on code.

4
  • What is the error? What is your question? Commented Dec 8, 2015 at 23:30
  • you should get SEGV , and tt should be on the heap, and you also forgot to return the pointer. Commented Dec 8, 2015 at 23:33
  • ArrayManipulator returns a pointer to an array that was allocated on the stack; this invokes undefined behaviour. Commented Dec 8, 2015 at 23:38
  • You are also passing the size of the array - 1 to your function and then declaring int tt[size] or tt[1] so that tt has only 1 element Commented Dec 8, 2015 at 23:58

2 Answers 2

1

You must return the pointer to the array that has been allocated on the heap:

Modify your method signature:

int** ArrayManipulator(int* arrayPoiner, const int size)

Allocate on the heap:

int **tt = new int*[size];

return the pointer to the array;

return tt;

and modify your calls such that:

int **ReversedArray = ArrayManipulator(t,1);

and to output do this:

cout<< *ReversedArray[i];

Don't forget to delete the space allocated with:

  delete [] ReversedArray;
Sign up to request clarification or add additional context in comments.

4 Comments

I was beating my head for this - now need to learn more on ** pointers :(
Thank you very much for the time and knowledge.
Why are you using a **?
@Bob__ the OP modified the question a bit since this was answered. In the original version he was actually using a array to pointers.
1

Function ArrayManipulator returns nothing. So the program is already has undefined behaviour.

I think what you want to get is the following

#include <iostream>

using namespace std;

int * ArrayManipulator( const int *source, size_t n, int *dest )
{
    dest = dest + n;

    while ( n-- ) *--dest = *source++;

    return dest;        
}

int main()
{
    const size_t N = 2;

    int a[N] = { 1, 2 };
    int b[N];

    int * ReversedArray = ArrayManipulator( a, N, b );

    for ( size_t i = 0; i < N; i++ )
    {
        cout << ReversedArray[i] << ' ';
    }

    cout << endl;

    return 0;
}

Take into account that there is standard algorithm std::reverse_copy declared in header <algorithm> that does a similar work. If you want to reverse an array "in place" then you can use another standard algorithm std::reverse.

1 Comment

that is actually a very nice solution to this problem, if one wants to avoid heap allocation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.