简体   繁体   中英

Copying one int array to another C++

Was trying to write a program which converts the value`s from one assigned array to another unassigned one. The code i wrote:

#include "stdafx.h";
#include <iostream>;

using namespace std;

int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int j[10];

int copy_array(int *p1, int n);
int *p2, *p2;

int main() {

    for (int l = 0; l < 10; l++) {
        cout << a[l] << endl;
    }

    copy_array(a, 10);

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

    system("PAUSE");
    return 0;
}

int copy_array(int *p1, int n) {
    while (n-- > 0) {
        *p1 = *p2;
            *p1++;
        *p2++;
    }
}

Im using the Microsoft visual studio platform and the error i got was "There is no context in which this conversion is possible". Why i cant use this int convert path? how can i fix and connect the 2 arrays using int type conversion(if its possible)?

What i tried was manipulating the local function copy_array so it makes the conversion using the addresses of the j[10] array integers, but this gave me another error. Any support and advice would be appreciated.

You don't need p2 to be global.

Just add parameter to copy_array .

like this:

void copy_array(int *p1, int *p2, int n) {
    while (n-- > 0) {
        *p1 = *p2;
        p1++;
        p2++;
    }
}

and call like this:

copy_array(j, a, 10);

Also: to print the copy you just do:

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

These are some notes on your code:

  1. you have redundant p2 declaration: int *p2, *p2; . Also you need to initialize it. so make it: int *p2 = j; (in fact, you don't actually need to use this global variable - you can achieve the same effect by passing j as necessary).
  2. Inside your copy function, your assignment should be in reverse: *p2 = *p1; not *p1 = *p2; - the right-hand side is assigned to the left hand side.
  3. When printing j , you do not need j[i] = &p2; which alters j 's contents.
  4. It is better to define the arrays inside the function not in the general scope.

Correct them and your code should work fine.

However, You do not need pointers to do this at all.

Consider the following code and compare it to yours:

#include <iostream>
using namespace std;  

void copy_array(int [], int [], int);
void print_array(int [], int);

int main() {

    int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int j[10];

    print_array(a,10);
    copy_array(a, j, 10);
    print_array(j,10);
    return 0;
}

void copy_array(int s[], int d[], int n) {
    for (int i = 0; i < n; i++)
        d[i] = s[i];   
}    // s for source & d for destination

void print_array(int arr[], int n) {
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << "\n\n";
}

I want to build on @Shadi's answer, which you should upvote, and make the code more C++-idiomatic.

  • In C++, we don't need to explicitly return 0; from main; it is implied, if you haven't returned anything else.
  • It's better to use names in a similar scheme for similar variables. Specifically, i and j are common variable names for integer scalars, eg counters - not arrays. I'd suggest you use a and b for the arrays, or values and copy_of_values etc.
  • The C++ standard library has an array-like container class named std::vector . It's not exactly the same as an array; for example, it uses dynamically-allocated memory, and can grow or shrink in size. The reason you might want to use it is that it allows you to perform plain assignment, and use other standard library facilities with it.

Thus Shadi's program becomes:

#include <iostream>
#include <vector>

void print_vector(const std::vector<int>& vec);

int main() {
    std::vector<int> a { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    std::vector<int> b;

    print_vector(a);
    b = a;
    print_vector(b);
}

void print_vector(const std::vector<int>& vec) {

    // this next line uses syntax from the 2011 version of
    // the C++ language standard ("C++11").
    for(int x : vec) {
        std:cout << x << " ";
    }
    cout << "\n\n";
}
  • You can also avoid the loop in print_vector entirely, using std::for_each or std::for_each_n , but that would require some knowledge of iterators and lambda functions, which may be a bit advanced for a beginner, so I won't go into that. But better yet, you could define a out-streaming operator for std::vector 's, as seen here , with which you could write std::cout << a; and have that work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM