clang-markdown

Migrate from https://cmakerhk.wordpress.com/2019/06/10/advance-c-c-lang-topics/,https://cmakerhk.wordpress.com/2019/07/02/c-string-object-vs-c-style-string/,https://cmakerhk.wordpress.com/2019/07/03/pointer-and-address/


// Here we can see that more than one variables
// are being used without reporting any error.
// That is because they are declared in the
// different namespaces and scopes.
#include
using namespace std;

// Variable created inside namespace
namespace first
{
int val = 500;
}

// Global variable
int val = 100;

int main()
{
// Local variable
int val = 200;

// These variables can be accessed from
// outside the namespace using the scope
// operator ::
cout << first::val << ā€˜\nā€™;

return 0;
}

C++[] ā€“> namespace is like a named-scope, so you can use it kind of like everywhere

CNU library

https://www.gnu.org/software/libc/manual/html_node/Getopt.html

More about C/C++ library

https://www.tutorialspoint.com/c_standard_library/index.htm

https://www.tutorialspoint.com/cpp_standard_library/index.htm


Two way to manipulate string.

std::string

String Object C-Style Sting
#include "string" #include "cstring" / "string.h"
string myStr = "Hello World"; char myCharr[20] = "Hello World";
str1 = str2; strcpy(charr1, charr2);
str1 += str2; strcat(charr1, charr2);
str.size() strlen(charr)
getline(cin, str); cin.getline(charr, 20); //20 - max len

int myVar = 8; // variable myVar store value
&myVar; //address of myVar
int *myPtr;
myPtr = &myVar; // pointer myPtr store address of myVar
*myPtr; //value of the address pointed by myPtr

myVar &myVar
*myPtr myPtr
value => 23 address => 0x1234
Dynamic array

int size; cin >> size;
int * pz = new int [size]; // dynamic binding, size set at run time
ā€¦
delete [] pz; // free memory when finished

1 2
void swapr(int & a, int & b) // use references { int temp; temp = a; // use a, b for values of variables a = b; b = temp; } swapr(wallet1, wallet2); // pass variables
void swapp(int * p, int * q) // use pointers { int temp; temp = *p; // use *p, *q for values of variables *p = *q; *q = temp; } swapp(&wallet1, &wallet2); // pass addresses of variables
void swapv(int a, int b) // try using values { int temp; temp = a; // use a, b for values of variables a = b; b = temp; } swapv(wallet1, wallet2); // pass values of variables