Skip to content

Instantly share code, notes, and snippets.

@rgbatty
Created April 2, 2013 15:17
Show Gist options
  • Save rgbatty/5293023 to your computer and use it in GitHub Desktop.
Save rgbatty/5293023 to your computer and use it in GitHub Desktop.
Practices implementing pointers
// PointerPractice.cpp
// Simple program to practice the use of pointers
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
using std::setfill;
//My Defs
void printIntro();
void basicPtrTests();
void valueExchange(int x, int y); //passes by Value
void addressExchange(int* x, int* y); //passes by address
void refExchange(int& x, int& y); //passes by reference
//Basic Pointers excercise Defs
void bpExercise();
int add(int x, int y);
void swap(int* x, int* y);
void dec(int& x);
void inc(int& x);
//Dynamic Array Exercise Defs
void dArrayExercise();
int main()
{
cout << setw(45) << "~~~~Welcome to Pointer Practice!~~~~" << endl;
cout << "------------------------------------------------------------" << endl;
cout << "Begin basic Ptr Tests..." << endl;
cout << "------------------------------------------------------------" << endl;
basicPtrTests();
cout << "------------------------------------------------------------" << endl;
cout << "Begin bp Exercise..." << endl;
cout << "------------------------------------------------------------" << endl;
bpExercise();
cout << "------------------------------------------------------------" << endl;
cout << "Begin dynamic array exercise..." << endl;
cout << "------------------------------------------------------------" << endl;
dArrayExercise();
cout << "------------------------------------------------------------" << endl;
}
void basicPtrTests()
{
int a(5), b(10); //regular ints
int* ptrA = &a; //ptrA is being initialized to b's address (& symbol). Can only hold addresses, not values;
int* ptrB = &b;
int derefA = *ptrA; //Dereferencing (*) ptrA to display its data. Dereferencing a ptr returns the datatype it was pointing to.
cout << "Initial Values of A & B: " << setw(11) << a << setw(16) << b << endl;
cout << "Ptrs set to A & B addresses : " << setw(13) << ptrA << setw(15) << ptrB << endl;
cout << "DerefA (stores val at ptrA's loc): " << setw(1) << derefA << endl;
*ptrA = 7; //sets the value ptrA is pointing to (a's location) to 7
cout << "Changed value of A using a ptr: " << setw(4) << a << endl;
valueExchange(a, b); //doesnt work due to scope issues
cout << "A & B after value exchange: " << setw(8) << a << setw(16) << b << endl;
addressExchange(ptrA, ptrB);
cout << "A & B after addExch using ptrs: " << setw(5) << a << setw(14) << b << endl;
addressExchange(ptrA, ptrB);
cout << "Same operation using A & B ref op: " << setw(1) << a << setw(16) << b << endl;
refExchange(a, b);
cout << "A & B after refExchange with ints: " << setw(1) << a << setw(14) << b << endl;
}
void bpExercise()
{
int num1, num2, result;
cout<< "Enter two numbers seperated by a space: " << endl;
cin >> num1 >> num2;
cout << "\nNum1 is " << num1 << " , num2 is " << num2 << endl;
result = add(num1, num2);
cout << "Result after adding is " << result << endl;
int* pNum1 = &num1;
int* pNum2 = &num2;
swap(pNum1, pNum2);
cout << "Result after swapping is " << num1 << " and " << num2 << endl;
dec(num1);
cout << "Result after decrementing num1 is " << num1 << endl;
inc(num2);
cout << "Result after incrementing num2 is " << num2 << endl;
}
void dArrayExercise()
{
int size(0), score(0);
cout << "Please enter a class size: ";
cin >> size;
int* classArray = new int[size];
for(int i = 0; i < size; i++)
{
cout << "Enter a score: ";
cin >> score;
classArray[i] = score;
}
for(int i = 0; i < size; i++)
{
cout << "Score[" << i << "]: " << classArray[i] << endl;
}
}
void valueExchange(int x,int y) //passes in two values, switches them, but deletes the vars after (due to scope)
{
int temp;
temp = x;
x = y;
y = temp;
}
void addressExchange(int* x, int* y) //passes in two ptrs, and switches the values each point to
{
int temp;
temp = *x; //temp is equal to the value pointed to by x
*x = *y; //the value pointed to by x is equal to the value pointed to by y
*y = temp; //temp is equal to the value pointed to by y
}
void refExchange(int& x, int& y) //passes in two addresses, and switches the values at those
{
int temp;
temp = x; //sets temp to the address of x
x = y; //sets the address of x to the address of y
y = temp; //sets y to the address stored in temp
}
int add(int x, int y)
{
return x + y;
}
void swap(int* x, int* y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void dec(int& x)
{
x--;
}
void inc(int& x)
{
x++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment