Copy from an array to another

Hi all,

This seems to be an easy problem but I can't work out and I don't know why. Here is my code :
1
2
3
4
5
6
7
8
void copy(int a[], int n, int copied[]) //n is the number of elements
{
	int i;
	for(i=0 ;i<n;++i)
	{
		copied[i]=a[i];
	}
}


The output of sort[anynumber] is always -858994360.

Help me please. Thanks a lot.
Last edited on
I think this syntax will send a copy of the destination array to the function, so this copy will be filled and not the one you use.
You must arrays of ints as variables of type int* in the function declaration for it to work as intended.

Oh, and there is the memcpy function that is standard : http://www.cplusplus.com/reference/clibrary/cstring/memcpy/
It still doesn't work
If I change to
 
void copy(int* a[], int n, int* copied[])

There is this error: error C2664: 'sort' : cannot convert parameter 1 from 'int [100]' to 'int *[]'

I declared the int a[100] in the main function.

And the output of memcpy is still -858994360
Last edited on
Nothing wrong with your function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

void copy(int a[], int n, int copied[])
{
	int i;
	for(i=0 ;i<n;++i)
	{
		copied[i]=a[i];
	}
}

int main()
{
    int arr1[3] = {2, 8, 1};
    int arr2[3];
    
    copy(arr1, 3, arr2);
    std::cout << arr2[0] << ' ' << arr2[1] << ' ' << arr2[2] << std::endl;
}
Haha, sorry guys. I don't know why but when I delete all and rewrite, it works :P:P.
Topic archived. No new replies allowed.