L2B/main.cpp
2024-09-20 00:56:59 -06:00

110 lines
No EOL
3.6 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <ostream>
#include <chrono>
int main(void) {
// Part 1
std::cout << "! ------------- [Part 1] -------------" << std::endl;
std::cout << "! Setting integer 1 and 2 to 4 and 5 respectively, directly"
<< std::endl;
int iNum = 4;
int iNum2 = 5;
std::cout << "! Creating pointer 1 and 2 with null pointer values"
<< std::endl;
int *pINum1 = nullptr;
int *pINum2 = nullptr;
std::cout << "! Setting pointer 1 and 2 to the memory addresses of integer 1 "
"and 2 respectively"
<< std::endl;
pINum1 = &iNum;
pINum2 = &iNum2;
std::cout << "Pointer 1 address: " << pINum1 << std::endl;
std::cout << "Integer 1 address: " << &iNum << std::endl << std::endl;
std::cout << "Pointer 2 address: " << pINum2 << std::endl;
std::cout << "Integer 2 address: " << &iNum2 << std::endl << std::endl;
std::cout << "Integer 1 value (dereferenced from pointer 1): " << *pINum1
<< std::endl;
std::cout << "Integer 2 value (dereferenced from pointer 2): " << *pINum2
<< std::endl;
// Part 2
std::cout << std::endl
<< "! ------------- [Part 2] -------------" << std::endl;
std::cout << "! Setting integer 1 value directly to 6" << std::endl;
iNum = 6;
std::cout << "Integer 1 value (directly): " << iNum << std::endl;
std::cout << "Integer 1 value (dereferenced from pointer 1): " << *pINum1
<< std::endl;
// Part 3
std::cout << std::endl << "------------- [Part 3] -------------" << std::endl;
std::cout << "! Setting value referenced by pointer 1 to a 7" << std::endl;
*pINum1 = 7;
std::cout << "Integer 1 value (directly): " << iNum << std::endl;
// Part 4
std::cout << std::endl << "------------- [Part 4] -------------" << std::endl;
std::cout << "! Setting pointer 2 address to the same as pointer 1"
<< std::endl;
pINum2 = pINum1;
std::cout << "Pointer 2 address: " << pINum2 << std::endl;
std::cout << "Value of pointer 2 dereference: " << *pINum2 << std::endl;
std::cout << "! Setting underlying value of pointer 2 to 8" << std::endl;
*pINum2 = 8;
std::cout << std::endl
<< "Integer 1 value (dereferenced from pointer 1): " << *pINum1
<< std::endl;
std::cout << "Integer 1 value (dereferenced from pointer 2): " << *pINum2
<< std::endl;
std::cout << "Integer 1 value (directly): " << iNum << std::endl << std::endl;
std::cout << "Integer 2 value (directly): " << iNum2 << std::endl;
// Part 5
std::cout << std::endl << "------------- [Part 5] -------------" << std::endl;
std::cout << "! Creating a double pointer with a null pointer value"
<< std::endl;
double *pDNum = nullptr;
// pDNum = &iNum; error: cannot convert int* to double* in assignment
// pDNum = pINum1; error: cannot convert int* to double* in assignment
std::cout << "! Creating a double with value 14.25" << std::endl;
double dNum = 14.25;
std::cout << "! Setting double pointer to the memory address of the double"
<< std::endl;
pDNum = &dNum;
std::cout << "Address of double (from double pointer): " << pDNum
<< std::endl;
std::cout << "Value of double (from double pointer): " << *pDNum << std::endl;
std::cout << "! Assigning the value pointed at by pointer 1 to the value "
"pointed at by the double pointer"
<< std::endl;
*pDNum = *pINum1;
std::cout << "Value of double (directly): " << dNum << std::endl;
std::cout << "Value of double (from double pointer): " << *pDNum << std::endl;
}