I need to write a function that accepts int array and array's size as arguments. Then the function should create a copy of the array, except that the element values is in reverse in the copy. The function should return a pointer to the new array.
I am having a slight problem. I get this error, Error 1 error C2664: 'reverseArray' : cannot convert parameter 1 from 'int [5]' to 'int' and I don't know what I've done wrong. Any help will be much appreciated

#include <iostream>
using namespace std;

int reverseArray(int, int, int *);

int main() {//start main

	const int size = 5;
	int array1 [size] = {10,20,30,40,50};
	int newArray [size];
	int* numptr = array1;

reverseArray(array1, size, numptr);
}//end main


int reverseArray(int array1[], int size, int *numptr) {//start function
	
	int count;
	
	
	 
	cout << "This is the orginal array: " ;
	 
	while (numptr < &array1[4]) {//start while

		cout << *numptr << " ";

		numptr ++;
	}//end while 


	return 0;

		



;}// end function

Look at your function prototype it does not match your function below. int reverseArray(int, int, int *); should be int reverseArray(int[5], int, int *); Edit: or just int reverseArray(int[], int, int *);

Ok you seem to have made a number of mistakes that stem from a simple mis-understanding of how arrays and pointers interact.

So I am going to go through the basics of this :

First off an array as defined by int array1[5]; reserves a block of memory that is 5 integers long. They are sequential in memory, i.e. there isn't another object/item/value in between.

Now a pointer can point to any memory location, or it could point to no memory location (it value will be zero).

Multiple pointers can point to the same memory locations, if required. Pointer are like book marks they point to pages in a book. Change there location does nothing to change the book. Only if you write to the memory location pointed to by the pointer does the array change, [similar to writing in a book at the page indicated by a book mark.]

So looking at your code:
You want to reverse an array into a new array.
You need to pass three things to a function. Original array LOCATION , size, NEW ARRAY LOCATION.

However, you correctly get the first and second but no the third.

In C++/C we have a helper pointer which is the name of the array. It points to the first element of the array. You don't have to use it. These two pointers are the same array1 == &array1[0] .

So you should call reverseArray as this: reversArray(array1,size,newArray); Now since reverseArray take COPIES of the pointers, there is no need to worry about array1,and newArray changing in main().

It will help you understand what is going on if you completely change the names of the variables in reverseArray eg.

int reverseArray(int* inputMemory, int aLength, int* outputMemory)
{
   // your reverse code here...
}

Note that you need to adjust your declaration as well, to be int reverseArray(int*,int,int*); [NOTE : I slight disagree with sfuo here, because he has hard coded the size 5 into the declaration and I think that is not good practice, this is better.]


Then you need to write some code that loop over the items and reverses them. I suggest that you use size. You want to copy from inputMemory[0],inputMemory[1]... inputMemory[size-1] to outputMemory[size-1],outputMemory[size-2],... outputMemory[0]


For 5 items you COULD do it individually, but don't!! Use a loop.

Hope that helps...

After an hour of stressing out, I figured out the error. Now I will try to implement what you are trying to say Stu, into my program. It's been a long day, and I hope I can get this done. Thanks a lot, I really am grateful.


Edit : I forgot to mention that I need to return a pointer to the new Array. Sorry I didn't get that out sooner.

For this you need to use the new operator and when you are done with it make sure to delete the allocated memory or you will run into problems later.

#include <iostream>
using namespace std;

int* reverseArray(int*, int);

int main()
{
	const int size = 5;
	int array1[size] = {10, 20, 30 , 40, 50};
	int* numptr = array1;

	for( int i = 0; i < size; i++ )
		cout << numptr[i] << endl;
	cout << endl;

	numptr = reverseArray(array1, size); //assigns numptr to the memory location of the return

	for( int i = 0; i < size; i++ )
		cout << numptr[i] << endl;

	delete[] numptr; //remember to delete the block of memory allocated before pointing to something else otherwise you will have a leak

	numptr = 0; //point numptr to 0 until you need to assign it again later

	//add pause if needed
	return 0;
}

int* reverseArray(int* in, int size)
{
	int *newArray = new int[size]; //allocate memory using the new operator

	for( int oldIndex = size-1, newIndex = 0; oldIndex >= 0; oldIndex--, newIndex++ )
		newArray[newIndex] = in[oldIndex];

	return newArray;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.