C++ Returning an Array using a pointer

  arguments, arrays, c++, function

Doing a beginner coding assignment for my c++ course. I’m fairly new and pointers has me stumbling a bit. What I’m trying to do with my code is have the function intSwapper return an array using a pointer. Since arrays can’t be normally returned, I have to use a pointer but I’m still trying to figure out how to make that work.

int* intSwapper(int* inputArray, int size) {
for (int i = 0; i < size; i++) {
     int* p = &inputArray[i];
     int* j = &size;
     //The static int I created below was just to test if I could even return what I needed.
     static int arr[size] = { 3,4,5,9,1 };
     int* p2 = arr;
     return arr;
     delete p;
     delete p2;
}

}

The code above does not even work, let me say that, that’s just a combination of failing and testing different things. The thing I’m trying to achieve is to pass the size and array through the function, and the function intSwapper is supposed to change the first number of the array being passed in with the last. Then, the second number is supposed to match the second to last number being passed in. The return type should be a pointer I believe. The assignment isn’t as easy as reversing the numbers and calling it quits. Apparently you don’t dynamically create memory or create a new array for this so I’m really lost. Thanks in advance you all, hope I was able to provide enough information.

Source: Windows Questions C++

LEAVE A COMMENT