Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tornikegomareli/dbe613caee0202491670216745686cc0 to your computer and use it in GitHub Desktop.
Save tornikegomareli/dbe613caee0202491670216745686cc0 to your computer and use it in GitHub Desktop.
Swap Two Array's Address Helping the double Pointer
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void Init(int* Arr, int Size)
{
for (int i = 0; i < Size; i++)
{
*(Arr + i) = rand() % 100;
}
}
void Print(int* Arr, int Size)
{
for (int i = 0; i < Size; i++)
{
cout << *(Arr + i) << " ";
}
}
void Swap(int **board, int **new_board) {
int *temp = *board;
*board = *new_board;
*new_board = temp;
}
int main()
{
const int Size = 3;
int Array1[Size];
int Array2[Size];
int*N = Array1;
int*M = Array2;
int**Nptr = &N;
int**Mptr = &M;
Init(N, Size);
Print(N, Size);
cout << "\n";
Init(M, Size);
Print(M, Size);
cout << "\After Swap " << endl;
Swap(Nptr, Mptr);
Print(N, Size);
cout << "\n";
Print(M, Size);
cin.get();
cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment