1

I have to use pointers to copy values of one array to another. The problem is I'm not allowed to use'[ ]' operators, which makes this more difficult for me. Here is my attempt:

#include <iostream>
using namespace std;

void cpyia(int old_array[],int new_array[],int length){

int *p1 = old_array;

int *p2 = new_array;

int *x = p2;

for(int i=0 ; i<length ; i++){

    p2 = x;
    p2 = p2 + i;        
        p2 = p1 + i;
}

for(int i=0; i<5; ++i){
     cout << p2[i] << endl;
}

}

  int main() {


int a[5]={1,2,3,4,5};

int b[5];

cpyia(a, b, 5);


}

An easier way to do it would be to put p2[i] = p1[i] in the loop, but I cant do that. Any help is appreciated.

4
  • 2
    Your code has a number of flaws it looks. Also there's not any value copied, you're just doing some point arithmetic gymnastics. Commented Aug 23, 2015 at 12:46
  • "An easier way to do it would be to put p2[i] = p1[i] in the loop, but I cant do that." Besides I can't see why you can't do that, I suppose you've been asked to use something like *p2 = *p1;. Commented Aug 23, 2015 at 12:50
  • 1
    @Jai: p[i] may be written as *(p + i). Commented Aug 23, 2015 at 14:43
  • @Jarod42: I would love to see the teacher's face if the OP did this... :) Commented Aug 23, 2015 at 15:33

3 Answers 3

4

The standard way of implementing your function is as follow:

for(int i = 0; i < length; ++i)
    *new_array++ = *old_array++;

To be a bit more explicit, it's the same as:

void cpyia(int old_array[],int new_array[],int length){
    int *p1 = old_array;
    int *p2 = new_array;

    for(int i=0 ; i<length ; i++){
        *(p2+i) = *(p1+i);
        // another way: *(p2++) = *(p1++);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

In real code, you would use std::copy before even thinking about rewriting such a simple thing yourself.

Here is a complete example:

#include <iostream>
#include <algorithm>

void cpyia(int old_array[],int new_array[],int length){
    std::copy(old_array, old_array + length, new_array);
}

int main() {
    int a[5]={1,2,3,4,5};
    int b[5];

    cpyia(a, b, 5);

    // test results:
    for (int index = 0; index < 5; ++index)
    {
        std::cout << a[index] << " <-> " << b[index] << "\n";
    }
}

However, your question says that you are "not allowed to use" something, which sounds a lot like a homework assignment. In that case, you could look at possible implementations of std::copy to get an idea of how to do it. Here is one way:

void cpyia(int old_array[],int new_array[],int length){
    int* first = old_array;
    int* last = old_array + length;
    int* d_first = new_array;

    while (first != last) {
        *d_first++ = *first++;
    }
}

Comments

0
#include<iostream>
using namespace std;

int main() {

const int size = 5;
int arr1[size] = { 4,21,43,9,77 };
int arr2[size];

int *ptr_a = arr1;
int *ptr_b = arr2;
for (int i = 0; i < size; i++)
{
*(ptr_b + i) = *(ptr_a + i);
cout << *(ptr_b + i) << " ";

}
}

Comments

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.