/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <iostream>
using std::cout;
using std::endl;
//=============================================
// this function does nothing because it only has
// local copies of the values passed to it
//=============================================
void sum(int myInt, int val) {
myInt += val;
}
//=============================================
// this function has a return value of the two integers
// since we are passing by value, this function can not
// change the value of the original variables
//=============================================
int returnSum(int myInt, int val) {
return(myInt+val);
}
//=============================================
// this function takes a reference to the first integer
// since the myInt is a reference,
// this function can change the value of the variable
// that myInt references
//=============================================
void sumRef(int& myInt, int val) {
myInt+=val;
}
//=============================================
// this function takes a pointer to the first integer
// since myInt is a pointer, we can change the value
// of the variable myInt points to.
// this is not safe since you could cast the type
// of a different variable to int* and this would work
// the reference is preferable because it will not allow
// passing a reference to the wrong type
//=============================================
void sumPtr(int* myInt, int val) {
*myInt+=val;
}
//=============================================
//=============================================
int main(int argc, char* argv[])
{
int x = 5;
int y = 10;
int& intRef = x;
int* intPtr;
char z = 'a';
char* cPtr;
cout<<"x : "<<x<<endl;
cout<<"y : "<<y<<endl;
sum(x, y);
cout<<"sum(x, y) : "<<x<<endl;
cout<<"intRef (refer. to x): "<<intRef<<endl;
cout<<"uninitialized intPtr: "<<*intPtr<<endl;
intPtr = &y;
cout<<"initialized intPtr to y: "<<*intPtr<<endl;
intRef+=y;
cout<<"intRef : "<<intRef<<endl;
x = returnSum(x, y);
cout<<"returnSum(x, y) : "<<*intPtr<<endl;
sumPtr(intPtr, y);
cout<<"sumPtr(intPtr, y) : "<<y<<endl;
sumRef(intRef, y);
cout<<"sumRef(intRef, y) : "<<x<<endl;
intPtr = &x;
cout<<"initialized intPtr to x: "<<*intPtr<<endl;
cout<<"x : "<<x<<endl;
cout<<"y : "<<y<<endl;
cout<<"intRef (refer. to x): "<<intRef<<endl;
// example why pointer may be a horrible idea because of lack of type safety
// there is only one byte for a char while an int takes 4.
// you may not see immediate damage because of data alignment as discussed
// in lectures, this type of operation could be disastrous.
cPtr = &z; // cPtr "points to" z which is a char
intPtr = (int*)cPtr; // here we cast cPtr to int* which is a cheat for convenience
*intPtr = x;
return 0;
}