diff --git a/.gitignore b/.gitignore index 378eac2..5acb669 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build +.vscode diff --git a/1_constants_variables_and_types/CMakeLists.txt b/1_constants_variables_and_types/CMakeLists.txt new file mode 100644 index 0000000..22ef3d4 --- /dev/null +++ b/1_constants_variables_and_types/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.10) + +project(cvt) + +add_compile_options(-Wall -Wextra -pedantic -Werror) + +add_executable(cvt main.cpp) diff --git a/1_constants_variables_and_types/main.cpp b/1_constants_variables_and_types/main.cpp new file mode 100644 index 0000000..b5af414 --- /dev/null +++ b/1_constants_variables_and_types/main.cpp @@ -0,0 +1,376 @@ +#include + +bool bglob{true}; +bool * pbglob{&bglob}; +bool & rbglob{bglob}; +static bool sbglob{false}; +const bool cbglob{false}; +const bool *pcbglob{&cbglob}; +const bool &rcbglob{cbglob}; +const static bool csbglob{true}; + +char cglob{50}; +char * pcglob{&cglob}; +char & rcglob{cglob}; +static char scglob{'t'}; +const char ccglob{true}; +const char *pccglob{&ccglob}; +const char &rccglob{ccglob}; +const static char cscglob{'y'}; + +int iglob{123}; +int * piglob{&iglob}; +int & riglob{iglob}; +static int siglob{234}; +const int ciglob{345}; +const int *pciglob{&ciglob}; +const int &rciglob{iglob}; +const static int csiglob{567}; + +float fglob{1.23}; +float* pfglob{&fglob}; +float & rfglob{fglob}; +static float sfglob{12.3}; +const float cfglob{2.34}; +const float * pcfglob{&cfglob}; +const float & rcfglob{fglob}; +const static float csfglob{23.4}; + +double dglob{3.45}; +double* pdglob{&dglob}; +double & rdglob{dglob}; +static double sdglob{34.5}; +const double cdglob{4.56}; +const double * pcdglob{&cdglob}; +const double & rcdglob{dglob}; +const static double csdglob{45.6}; + +long lglob{123456789}; +long* plglob{&lglob}; +long & rlglob{lglob}; +static long slglob{987654321}; +const long clglob{456789123}; +const long * pclglob{&clglob}; +const long & rclglob{lglob}; +const static long cslglob{123789465}; + +template +void print_variable_info(const T& var, const std::string& name = "") { + std::string prefix = (name.empty()) ? "" : ("[" + name + "] "); + + std::cout << "\n"; + std::cout << prefix << "Value: " << var << "\n"; + std::cout << prefix << "Address: " << &var << "\n"; // Q: why it won't print address of var with type "char"? what is needed to do? + std::cout << prefix << "Type: " << typeid(var).name() << "\n"; + std::cout << prefix << "Sizeof: " << sizeof(var) << std::endl; +} + +int main() +{ + + bool bloc{true}; + bool* pbloc{&bloc}; + bool & rbloc{bloc}; + static bool sbloc{false}; + const bool cbloc{true}; + const bool *pcbloc{&cbloc}; + const bool &rcbloc{cbloc}; + const static bool csbloc{true}; + + char cloc{50}; + char* pcloc{&cloc}; + char & rcloc{cloc}; + static char scloc{'t'}; + const char ccloc{true}; + const char *pccloc{&ccloc}; + const char &rccloc{ccloc}; + const static char cscloc{'y'}; + + int iloc{123}; + int* piloc{&iloc}; + int & riloc{iloc}; + static int siloc{234}; + const int ciloc{345}; + const int *pciloc{&ciloc}; + const int &rciloc{iloc}; + const static int csiloc{567}; + + float floc{1.23}; + float* pfloc{&floc}; + float & rfloc{floc}; + static float sfloc{12.3}; + const float cfloc{2.34}; + const float * pcfloc{&cfloc}; + const float & rcfloc{floc}; + const static float csfloc{23.4}; + + double dloc{3.45}; + double* pdloc{&dloc}; + double & rdloc{dloc}; + static double sdloc{34.5}; + const double cdloc{4.56}; + const double * pcdloc{&cdloc}; + const double & rcdloc{dloc}; + const static double csdloc{45.6}; + + long lloc{123456789}; + long* plloc{&lloc}; + long & rlloc{lloc}; + static long slloc{987654321}; + const long clloc{456789123}; + const long * pclloc{&clloc}; + const long & rclloc{lloc}; + const static long cslloc{123789465}; + + print_variable_info(bglob,"bglob, bool global var"); + print_variable_info(pbglob,"pbglob, bool global pointer to bglob"); + print_variable_info(rbglob,"rbglob, bool global ref to bglob"); + + print_variable_info< bool>(cbglob,"cbglob, bool const global var"); + print_variable_info(pcbglob,"pcbglob, bool const global pointer to bglob"); + print_variable_info< bool>(rcbglob,"rcbglob, bool const global ref to bglob"); + + print_variable_info(sbglob,"sbglob, bool static global var"); + print_variable_info(csbglob,"csbglob, bool const static global var"); + + std::cout << "---------------------------------------------------------------------" << "\n" ; + + print_variable_info(cglob,"cglob, char global var"); + print_variable_info(pcglob,"pcglob, char global pointer to cglob"); + print_variable_info(rcglob,"rcglob, char global ref to cglob"); + + std::cout << "\n[cglob, char global var] Address: " <(&cglob) << "\n"; + std::cout << "[pcglob, char global pointer to cglob] Value: " <(pcglob) << "\n"; + + print_variable_info(ccglob,"ccglob, char const global var"); + print_variable_info(pccglob,"pccglob, char const global pointer to cglob"); + print_variable_info(rccglob,"rccglob, char const global ref to cglob"); + + std::cout << "\n[ccglob, char const global var] Address: " << static_cast(&ccglob) << "\n"; + std::cout << "[pccglob, char const global pointer to ccglob] Value: " << static_cast(pccglob) << "\n"; + + print_variable_info(scglob,"scglob, char static global var"); + print_variable_info(cscglob,"cscglob, char const static global var"); + + std::cout << "---------------------------------------------------------------------" << "\n"; + + print_variable_info(iglob,"iglob, int global var"); + print_variable_info(piglob,"piglob, int global pointer to iglob"); + print_variable_info(riglob,"riglob, int global ref to iglob"); + + print_variable_info(ciglob,"ciglob, int const global var"); + print_variable_info(pciglob,"pciglob, int const global pointer to iglob"); + print_variable_info(rciglob,"rciglob, int const global ref to iglob"); + + print_variable_info(siglob,"siglob, int static global var"); + print_variable_info(csiglob,"csiglob, int const static global var"); + + std::cout << "---------------------------------------------------------------------" << "\n"; + + print_variable_info(fglob,"fglob, float global var"); + print_variable_info(pfglob,"pfglob, float global pointer to fglob"); + print_variable_info(rfglob,"rfglob, float global ref to fglob"); + + print_variable_info(cfglob,"cfglob, float const global var"); + print_variable_info(pcfglob,"pcfglob, float const global pointer to fglob"); + print_variable_info(rcfglob,"rcfglob, float const global ref to fglob"); + + print_variable_info(sfglob,"sfglob, float static global var"); + print_variable_info(csfglob,"csfglob, float const static global var"); + + std::cout << "---------------------------------------------------------------------" << "\n"; + + print_variable_info(dglob,"dglob, double global var"); + print_variable_info(pdglob,"pdglob, double global pointer to dglob"); + print_variable_info(rdglob,"rdglob, double global ref to dglob"); + + print_variable_info(cdglob,"cdglob, double const global var"); + print_variable_info(pcdglob,"pcdglob, double const global pointer to dglob"); + print_variable_info(rcdglob,"rcdglob, double const global ref to dglob"); + + print_variable_info(sdglob,"sdglob, double static global var"); + print_variable_info(csdglob,"csdglob, double const static global var"); + + std::cout << "---------------------------------------------------------------------" << "\n"; + + print_variable_info(lglob,"lglob, long global var"); + print_variable_info(plglob,"plglob, long global pointer to lglob"); + print_variable_info(rlglob,"rlglob, long global ref to lglob"); + + print_variable_info(clglob,"clglob, long const global var"); + print_variable_info(pclglob,"pclglob, long const global pointer to lglob"); + print_variable_info(rclglob,"rclglob, long const global ref to lglob"); + + print_variable_info(slglob,"slglob, long static global var"); + print_variable_info(cslglob,"cslglob, long const static global var"); + + std::cout << "---------------------------------------------------------------------" << "\n"; + +print_variable_info(bloc,"bloc, bool local var"); + print_variable_info(pbloc,"pbloc, bool local pointer to bloc"); + print_variable_info(rbloc,"rbloc, bool local ref to bloc"); + + print_variable_info(cbloc,"cbloc, bool const local var"); + print_variable_info(pcbloc,"pcbloc, bool const local pointer to bloc"); + print_variable_info(rcbloc,"rcbloc, bool const local ref to bloc"); + + print_variable_info(sbloc,"sbloc, bool static local var"); + print_variable_info(csbloc,"csbloc, bool const static local var"); + + std::cout << "---------------------------------------------------------------------" << "\n"; + + print_variable_info(cloc,"cloc, char local var"); + print_variable_info(pcloc,"pcloc, char local pointer to cloc"); + print_variable_info(rcloc,"rcloc, char local ref to cloc"); + + std::cout << "\n[cloc, char local var] Address: " <(&cloc) << "\n"; + std::cout << "[pcloc, char local pointer to cloc] Value: " <(pcloc) << "\n"; + + print_variable_info(ccloc,"ccloc, char const local var"); + print_variable_info(pccloc,"pccloc, char const local pointer to cloc"); + print_variable_info(rccloc,"rccloc, char const local ref to cloc"); + + std::cout << "\n[ccloc, char const local var] Address: " <(&ccloc) << "\n"; + std::cout << "[pccloc, char const local pointer to cloc] Value: " <(pccloc) << "\n"; + + print_variable_info(scloc,"scloc, char static local var"); + print_variable_info(cscloc,"cscloc, char const static local var"); + + std::cout << "---------------------------------------------------------------------" << "\n"; + + print_variable_info(iloc,"iloc, int local var"); + print_variable_info(piloc,"piloc, int local pointer to iloc"); + print_variable_info(riloc,"riloc, int local ref to iloc"); + + print_variable_info(ciloc,"ciloc, int const local var"); + print_variable_info(pciloc,"pciloc, int const local pointer to iloc"); + print_variable_info(rciloc,"rciloc, int const local ref to iloc"); + + print_variable_info(siloc,"siloc, int static local var"); + print_variable_info(csiloc,"csiloc, int const static local var"); + + std::cout << "---------------------------------------------------------------------" << "\n"; + + print_variable_info(floc,"floc, float local var"); + print_variable_info(pfloc,"pfloc, float local pointer to floc"); + print_variable_info(rfloc,"rfloc, float local ref to floc"); + + print_variable_info(cfloc,"cfloc, float const local var"); + print_variable_info(pcfloc,"pcfloc, float const local pointer to floc"); + print_variable_info(rcfloc,"rcfloc, float const local ref to floc"); + + print_variable_info(sfloc,"sfloc, float static local var"); + print_variable_info(csfloc,"csfloc, float const static local var"); + + std::cout << "---------------------------------------------------------------------" << "\n"; + + print_variable_info(dloc,"dloc, double local var"); + print_variable_info(pdloc,"pdloc, double local pointer to dloc"); + print_variable_info(rdloc,"rdloc, double local ref to dloc"); + + print_variable_info(cdloc,"cdloc, double const local var"); + print_variable_info(pcdloc,"pcdloc, double const local pointer to dloc"); + print_variable_info(rcdloc,"rcdloc, double const local ref to dloc"); + + print_variable_info(sdloc,"sdloc, double static local var"); + print_variable_info(csdloc,"csdloc, double const static local var"); + + std::cout << "---------------------------------------------------------------------" << "\n"; + + print_variable_info(lloc,"lloc, long local var"); + print_variable_info(plloc,"plloc, long local pointer to lloc"); + print_variable_info(rlloc,"rlloc, long local ref to lloc"); + + print_variable_info(clloc,"clloc, long const local var"); + print_variable_info(pclloc,"pclloc, long const local pointer to lloc"); + print_variable_info(rclloc,"rclloc, long const local ref to lloc"); + + print_variable_info(slloc,"slloc, long static local var"); + print_variable_info(cslloc,"cslloc, long const static local var"); + + std::cout << "---------------------------------------------------------------------" << "\n"; + + unsigned char ucloc{'k'}; + + iloc=floc; + std::cout << "iloc <- 12.34\n" << "iloc=" << iloc << "\n"; + std::cout << "-------\n"; + dloc=iloc; + std::cout << "dloc <- 12\n" << "dloc=" << dloc << "\n"; + std::cout << "-------\n"; + iloc=cloc; + std::cout << "iloc <- 'k'\n" << "iloc=" << iloc << "\n"; + std::cout << "-------" << "\n"; + iloc=1000; + cloc=iloc; + ucloc=iloc; + std::cout << "cloc <- 1000\n" << "cloc=" << (int)cloc << "\n"; + std::cout << "ucloc <- 1000\n" << "ucloc=" << (int)ucloc << "\n"; + std::cout << "---------------------------------------------------------------------\n\n";; + + unsigned char * puchar{&ucloc}; + std::cout << "*puchar <- ucloc \n" << "*puchar=" << int (*puchar) << "\n"; + *puchar=*puchar +1; + std::cout << "increment *puchar \n" << "*puchar=" << int (*puchar) << " \nucloc=" << (int)ucloc << "\n"; + + std::cout << "---------------------------------------------------------------------\n"; + + auto adloc{345.567}; + + std::cout << "Variable adloc= " << adloc << "\n"; + std::cout << "Type of adloc is " << typeid(adloc).name() << "\n"; + + // To learn the most common mistakes and know how compilation errors look like try the next: + // - create two, global and local, variables with the same name; + // - change a value of a constant; + // - create a reference without assigning the value; + // - passing the address of a variable of a specific type to a pointer of a different type; + // - print a value of nullable pointer; + // - pass some value (for example, 0, 1, 2 ... whatever) to the pointer; + // - pass a value to a variable of a different type (for example, pass "some_string" to int variable). + + std::cout << "---------------------------------------------------------------------" << std::endl; + + std::cout << "Global iglob befor definition of local variable = " << iglob << "\n"; + int iglob{2378}; + std::cout << "iglob after definition 'int iglob{2378}' of local variable = " << iglob << "\n"; + //the compiler doesn't generate any error. Local variable just shadow the global variable + + //icglob=18; + //error: assignment of read-only variable ‘icglob’ + + //int &riglob_new; + //ERROR: ‘riglob_new’ declared as reference but not initialized + + // unsigned int * uipointer = &iglob; + //error: invalid conversion from ‘int*’ to ‘unsigned int*’ + + //std::cout << endl << "---------------------------------------------------------------------" << endl; + + //int * npointer{nullptr}; + //There is no compilation error + // try{ + // std::cout << "Value of nullptr pointer = " << *npointer << endl; + // } + // catch (...){ + // std::cout << "Error with nullptr pointer" << endl; + // } + //ERROR: Segmentation fault + + //std::cout << endl << "---------------------------------------------------------------------" << endl; + + //int * somepointer; + + //somepointer=56; + //std::cout << "somepointer value = " << somepointer << ", value from pointer = " << *somepointer << endl; + //error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive] + + ///std::cout << endl << "---------------------------------------------------------------------" << endl; + + //iglob="Some string."; + //ERROR: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive] + + + std::cout << std::endl; + return 0; +} \ No newline at end of file diff --git a/1_some_title/CMakeLists.txt b/1_some_title/CMakeLists.txt deleted file mode 100644 index e69de29..0000000 diff --git a/2_another_title/CMakeLists.txt b/2_another_title/CMakeLists.txt index e69de29..8b13789 100644 --- a/2_another_title/CMakeLists.txt +++ b/2_another_title/CMakeLists.txt @@ -0,0 +1 @@ + diff --git a/CMakeLists.txt b/CMakeLists.txt index 55c0477..3e06409 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,7 @@ +cmake_minimum_required(VERSION 3.10) + project(basecamp) -add_subdirectory(1_some_title) +add_subdirectory(1_constants_variables_and_types) add_subdirectory(2_another_title) # add_subdirectory(3_...) diff --git a/README.md b/README.md index c578785..7cf1928 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -BASECAMP FOLDER STRUCTURE +## BASECAMP FOLDER STRUCTURE + + - `1_constants_variables_and_types` - learning of behavior and memory allocation of constants, variables, types, pointers and references \ No newline at end of file