ERROR copying array - pointers

closed account (ivDwAqkS)
I was trying to copy one array to another one of the same size, but when I execute the program all the array values that are supposed to be copied appeared to be 0 when it should be 10, could someone help m e what am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cstdlib>
using namespace std;

int a[10];
int b[10];

void print(int *, int);
void zeros (int *, int);
void copy_array (int *, int *, int);

int main(){
    int n;
    cout << "Enter the time of trials: ";
    cin >> n;

    zeros(a,n);
    print(a, n);

    cout << endl << "it should copy all elements of 'a': " << endl << endl;
    copy_array(a, b, n);

    return 0;
}

void print (int *p, int n){
    while(p < a + n){
        cout << *p << " ";
        *p++;
    }
}

void zeros(int *p, int n){
    while (n--> 0){
        *(p++)=10; // I set 10 for every element
    }
}

void copy_array (int *p1, int *p2, int n){
    while (n-- > 0){
        cout << *p2 << " ";
        *p1++=*p2++;
    }
}
Last edited on
Please do not post the same question in more than one forum. Check your syntax in your copy_array() function.
closed account (ivDwAqkS)
could you tell me what is the ERROR?
You should be assigning from p1 TO p2:
*p2++=*p1++;
closed account (ivDwAqkS)
LMAOOOOOOOOOOOOO THANK YOU!!!!!!!!!!!!!!!!!!! MY FAULT I DIDNT SEE IT :D appreciate it!
Topic archived. No new replies allowed.