1
#include <iostream>
#include <cstring>
#include <cstdlib>



using namespace std;
void printArray(int* arr, int size) {
    cout  << "Printing the array..." << endl;
    for (int index = 0; index < size; index++) {
        cout << arr[index] << endl;
    }
}

void populateArray(int* arr, int size) {
    for (int index = 0; index < size; index++) {
        arr[index] = index * 10 + 1;
    }
}

int main() {
    int size = 2;
    int* arr = new int[size];   
    populateArray(arr, size);
    size_t newSize = size * 2;
    int* newArr = new int[newSize];
    memcopy(newArr, arr, size);
    populateArray(newArr, newSize);
    return EXIT_SUCCESS;
}

Even after including cstring , I am getting 'VarArr.cpp:25:27: error: ‘memcopy’ was not declared in this scope' error while compiling. What am I missing here ?

T.C.
  • 133,968
  • 17
  • 288
  • 421
sakthisundar
  • 3,278
  • 3
  • 16
  • 29

2 Answers2

6

The correct name is memcpy, not memcopy.

Shoe
  • 74,840
  • 36
  • 166
  • 272
4

You made a typo and meant to use memcpy, but you should prefer to use the C++ facilities instead.

std::copy(arr, arr + size, newArr);

You seem to be forgetting to delete your new'd arrays, which isn't a problem since the program ends right there, but it's a good habit to remember to do this.

Let the standard library manage your memory for you. Use a container like std::vector or a smart pointer like std::unique_ptr.