Skip to content

Instantly share code, notes, and snippets.

@SproutSeeds
Created July 24, 2020 03:01
Show Gist options
  • Save SproutSeeds/1e4812d7a90385bdb7b3f3d2e0dee24d to your computer and use it in GitHub Desktop.
Save SproutSeeds/1e4812d7a90385bdb7b3f3d2e0dee24d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <ctime>
using namespace std;
void fillArray(int arr[], int length){
for(int i = 0; i < length; ++i){
arr[i] = rand()%10; //0-9
}
}
void printArray(int arr[], int length){
for(int i = 0; i < length; ++i){
cout << "arr[" << i << "] = " << arr[i] << endl;
}
cout << endl;
}
int main(){
srand(time(NULL));//Seeding a random number generator
int size;
cout << "How big is your array?";
cin >> size;
int *arr1 = new int[size];
int *arr2;
char hello[] = "Hello World";//Arrays identifiers are similar to pointers
char *str = hello; //Shallow Copy. Use strcpy for a deep copy of c_strings
fillArray(arr1, size);
cout << "Arr1:" << endl;
printArray(arr1, size);
arr2 = arr1;//SHALLOW COPY OF ARR1 INTO ARR2
cout << "Arr2:" << endl;
printArray(arr2, size);
fillArray(arr2, size);//CHANGE VALUES TO ARR2
cout << "Arr1 after Arr2 gets new values" << endl;
printArray(arr1, size); //VALUES HAVE CHANGED IN ARR1
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment