1/21/2015

Custom Build System for Python : Sublime-Text-3


# Result ( For Linux ):- 
    # Allow you to run Python Script Directly, Just hit CTRL+B
    # Debug your Python script easily using IPython Terminal/Console.
        # So, You have a Python Script and there is something that you want to check out after completion of Python Script. then you need to Hit SHIFT+CTRL+B keys.
        # So that Sublime runs build+run command, So far we have defined that first we want to execute our script using IPython interactive environment and then we want to take a look at Objects/Variables/results/output generated by our Python script.



# Sublime 3: Custom Build System Script for Python :-


{
    "shell_cmd": "xterm -hold -e python \"$file\"",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python, source.py",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "xterm -fg green -hold -e ipython -i \"${file}\""
        }
    ]
}

1/16/2015

range() function in C++ : Inspired from Python

# What is range() function, and how it can help you ?
    # A sequence of numbers from starting point to end point ( End point may be undefined, so you have a Infinite range), and optional step parameter.
    # You can generate a sequence integers ,
        example:-
            range(1, 10)  # Exclusive ranges == [start, end, step)
            [1, 2, 3, 4, 5, 6, 7, 8, 9]

            range(1, 10)  # Inclusive ranges == [start, end, step]
            [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    # You are not limited to generate ranges of integers, you can also generate ranges from  char / float ( * )  data type.
        example:-
            range('a', 'z', 1)    # Exclusive ranges == [start, end, step)
            a b c d e f g h i j k l m n o p q r s t u v w x y

            range('A', 'z', 1 )    # Inclusive ranges = [start, end, step]
            A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z


    # More Examples :- ( Exclusive ranges )
        std::vector<char> v = rangeE('a', 'z', 1  );
            a b c d e f g h i j k l m n o p q r s t u v w x y

        std::vector<char> v = rangeE('A', 'z', 3);
            A D G J M P S V Y \ _ b e h k n q t w

        std::vector<int> v = rangeE(0, 100, 4);
            0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96

        std::vector<float> v = rangeE<float, float>(10.3, 345.3, 0.4);
            10.3 10.7 11.1 ,... 343.497 343.897 344.297 344.697 345.097

        std::vector<int> v = rangeE(10, 100);
            10 11 12 13 ,... 97 98 99

        std::vector<float> v = rangeE<float, float> (0, 100, 0.5);
            0 0.5 1 1.5 2 ,... 97 97.5 98 98.5 99 99.5

        std::vector<char> v = rangeE('A', 'z', 1  );
            A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y

    # More Examples :- ( Inclusive ranges )
        std::vector<char> v = rangeI('a', 'z', 1  );
            a b c d e f g h i j k l m n o p q r s t u v w x y z

        std::vector<char> v = rangeI('A', 'z', 3);
            A D G J M P S V Y \ _ b e h k n q t w z

        std::vector<int> v = rangeI(0, 100, 4);
            0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100

        std::vector<float> v = rangeI<float, float>(10.3, 345.3, 0.4);
            10.3 10.7 11.1 ,... 345.097 345.497 345.897 346.297


        std::vector<int> v = rangeI(10, 100);
            10 11 12 13 ,.. 97 98 99 100

        std::vector<float> v = rangeI<float, float> (0, 100, 0.5);
            0 0.5 1 1.5 2 ,... 98 98.5 99 99.5 100 100.5

## range() function using C++
### Exclusive ranges

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

template<class T=int, class U=int>
std::vector<T> rangeE(T start, T stop, U step=1){
    std::vector<T> v;

    while(1) {
        if (start >= stop) {
            break;
        }
        v.push_back(start);
        start += step;
    }
    return v;
}

int main(int argc, char const *argv[])
{
    // std::vector<char> v = rangeE('a', 'z', 1  );
    // std::vector<char> v = rangeE('A', 'z', 3);
    // std::vector<int> v = rangeE(0, 100, 4);
    // std::vector<float> v = rangeE<float, float>(10.3, 345.3, 0.4);
    // std::vector<int> v = rangeE(10, 100);
    std::vector<float> v = rangeE<float, float> (0, 100, 0.5);
    for(auto i: v){
        cout << i << ' ';
    }
    cout << '\n';
    return 0;
}

### Inclusive ranges

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

template<class T=int, class U=int>
std::vector<T> rangeI(T start, T stop, U step=1){
    std::vector<T> v;

    while(1) {
        if (start >= stop+1) {
            break;
        }
        v.push_back(start);
        start += step;
    }
    return v;
}

int main(int argc, char const *argv[])
{
    // std::vector<char> v = rangeI('a', 'z', 1  );
    // std::vector<char> v = rangeI('A', 'z', 3);
    // std::vector<int> v = rangeI(0, 100, 4);
    // std::vector<float> v = rangeI<float, float>(10.3, 345.3, 0.4);
    // std::vector<int> v = rangeI(10, 100);
    std::vector<float> v = rangeI<float, float> (0, 100, 0.5);
    for(auto i: v){
        cout << i << ' ';
    }
    cout << '\n';
    return 0;
}



# Tests :-

## Does our range() function can be directly used in other programs?
## What about its performance?
## Does it comparable to native range() function in C++?

### Implementing and using native range() function in C++11
iteration = 10000000; Language = C++11
# Write output to Console
ssp@ssp-pc ~/Programming/CPP $ xterm ./ExclusiveRangeDef
10.5701301098
10.5873568058
10.4189360142
10.7807810307
10.10414505

# Don't print output to console/Terminal/Command Prompt, Just save that into a Text FILE.
iteration = 10000000; Language = C++11
ssp@ssp-pc ~/Programming/CPP $ xterm -e "./ExclusiveRangeDef > dumploop.txt"
0.556797981262
1.02536702156
1.04401111603
0.961767911911
0.982132911682
0.971391916275
0.934329032898
1.09931206703
0.961050033569
0.937591075897


iteration = 10000000*10; Language = C++11
# Don't print output to console/Terminal/Command Prompt, Just save that into a Text FILE.
ssp@ssp-pc ~/Programming/CPP/RangeInCPP $ xterm -e "./ExclusiveRangeDef > dumploop.txt"
6.75697207451
6.55503702164
8.64361310005
5.96445202827
7.79801106453
6.90669894218
6.0758459568
6.74977111816
7.17069101334
7.01367592812



### Implementing and using range() Function with another function in C++11

iteration = 10000000; Language = C++11
ssp@ssp-pc ~/Programming/CPP $ xterm ./ExclusiveRange
10.5833230019
10.6844108105
10.6519501209
10.6691529751
10.6650300026

# Don't print output to console/Terminal/Command Prompt, Just save them into a Text FILE.
iteration = 10000000; Language = C++11
ssp@ssp-pc ~/Programming/CPP $ xterm -e "./ExclusiveRange > dumploop.txt"
0.753065824509
1.19918298721
1.18582081795
1.1427628994
1.60647511482
1.0098259449
1.18290019035
1.12054014206
1.10526990891
1.14365291595

iteration = 10000000; Language = C++11; Implementation = using C++ Template
ssp@ssp-pc ~/Programming/CPP/RangeInCPP $ xterm -e "./ExclusiveRangeTemp > dumploop.txt"
1.16944003105
0.979794979095
1.31697702408
0.782207012177
1.13583612442
1.56683707237
1.01916909218
1.18384790421
0.986218214035
1.383425951

# Don't print output to console/Terminal/Command Prompt, Just save that into a Text FILE.
iteration = 10000000*10; Language = C++11
ssp@ssp-pc ~/Programming/CPP/RangeInCPP $ xterm -e "./ExclusiveRange > dumploop.txt"
8.63241100311
8.56675696373
6.39435505867
8.33173489571
8.47799301147
8.60728192329
8.34282588959
7.66121196747
7.68087077141
8.34091496468


# Testing our built function within nested loop
# iteration = 1000x1000; Language = C++11
# Code :-

    for(auto i: rangeE(0, 100, 2)) {
        for(auto j: rangeE(0, 100, 2)) {
            cout << i << ' ' << j << endl;
        }
    }

ssp@ssp-pc ~/Programming/CPP/RangeInCPP $ xterm -e "./ExclusiveRangeTempNest0 > dumploop.txt"
1.52887296677
1.60038113594
1.56652903557
1.78646588326
1.50389409065
1.59423518181
1.56246495247
1.57362604141
1.58079385757
1.56350493431

# iteration = 1000x1000; Language = C++11
# Code :-

    for(int i=0; i< 1000; i+=2) {
        for(int j=0; j< 1000; j+=2) {
            cout << i << ' ' << j << endl;
        }
    }
    cout << endl;

ssp@ssp-pc ~/Programming/CPP/RangeInCPP $ xterm -e "./ExclusiveRangeDefNest0 > dumploop.txt"
1.5027050972
1.65471196175
1.49789905548
1.7509188652
1.52278089523
1.58404016495
1.52930808067
1.54360198975
1.54617094994
1.54985117912


# Built-in Python  range() function tests :-

iteration = 10000000; Language = Python2.7
ssp@ssp-pc ~/Programming/CPP $ xterm ./ExclusiveListPY.py
11.4798099995
11.4038779736
11.5232269764
11.460515976
11.6050000191

# Don't print output to console/Terminal/Command Prompt, Just save them into a Text FILE.
iteration = 10000000; Language = Python2.7
ssp@ssp-pc ~/Programming/CPP $ xterm -e "./ExclusiveListPY.py > dumploop.txt"
2.33868694305
2.60927009583
2.50296115875
2.53171110153
2.93597483635
2.12743091583
2.47753596306
2.4461350441
2.69060111046
2.41603589058

# NOTE :- testing of programs is done with Python Script, that will tell you execution time taken by a program.
# PC conf :-
Intel CORE 2 DUO ( 2GB RAM )
LinuxMint 17 - x64


1/11/2015

Custom Build System for C++11 : Sublime-Text-3






# Why sublime-text?
    # Because, it is lightweight, portable, and runs on Win/Linux/MacOS.
    # and it is really awesome.
    # it is more flexible and extensible.
    # You can turn your Sublime-text editor into a FULL featured IDE.
    # I really use it for C/C++/HTML/JS/CSS/Python/Java, and it is great.
    # Sublime-Text Editor is more real, because it allows you to customize almost everything.
    # For Compiled languages, you can use pre-built BUILD SYSTEM or create your own.
    # Snippet, that is a great idea and that would be great if you learn, how to write snippet and use them.

# Why we are here?
    # What is Build-system in Sublime-Text Editor?
        # Build-system allows you to write and build programs directly ( Just hit, CTRL+b ) and if you want to run build binary ( Just hit, SHIFT+CTRL+b )
        # Build System ( RUN, BUILD ) basically allows you to run Two COMMANS using their shortcut key, that really does not matter which command you want to invoke with CTRL+b and CTRL+SHIFT+b.



    # What about pre-built BUILD SYSTEM for C++
        # Hmm, that is great, but we want some more functionality ( I want to pass some command line parameters ) , this is why we want to write our own Custom BUILD SYSTEM for C++.

    # Sublime-Text Build System Script for C/C++
    ## When, You want to use Flags like, -std=c++11
    #### C++11.sublime-build ( FOR LINUX )

{
    "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++, source.cpp",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" -std=c++11 && \"${file_path}/${file_base_name}\""
        }
    ]
}
    ####

    ## When, you want to run your program in Terminal ( xterm ) instead of sublime-Text console, Becaue you can pass input using Sublime-Text Console.
    ## NOTE, that you may not be able to see terminal because your program won't wait for your response.
    ## So, you need to stop your C/C++ program from being exiting automatically.
    #### C++11.sublime-build

{
    "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++, source.cpp",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" -std=c++11 && \"${file_path}/${file_base_name}\""
        }
    ]
}
    ####

    # When, you want to run your program in a separate terminal process, that won't exit automatically even if program exists itself or you can tell xTerm, not to close itself.
    # FINAL Version : filename: C++11.sublime-build
    # PATH : $USER_HOME/.config/sublime-text-3/Packages/User/
    # FOR LINUX Operating System
    #### C++11.sublime-build

{
    "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++, source.cpp",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" -std=c++11 && xterm -fg green -bg black -hold -e \"${file_path}/${file_base_name}\""
        }
    ]
}
    ####

    # FINAL Version : filename: C++11.sublime-build
    # PATH : %sublime-text-3%\Packages\User\
    # For Windows:-
        # NOTE, Ensure that GNU GCC Compiler is in your System PATH environment variable.
        # If you want to check whether g++/gcc BINARY is in your system PATH.
             # use  which   Command for LINUX (  which -a g++ )
             # use  where   Command for Windows  (  where gcc )

        # So if you are a Windows user and you might want to use some external Terminal Emulator Programs for Windows, just like, ( mintty, console2, cmder, conemu ) .
        # I recommend you to use mintty terminal emulator for windows that works just as  xterm   in Linux.

        # Command Example:-

mintty --hold always -e g++ sample.cpp -o sample.exe

        # One, more TIP for windows user( Optional for Linux Users)
            # Sometimes you might want to supply some input to your program.
            # There are actually two ways, first, You are going to type each and every required value, but for sublime, let me tell you one thing, Sublime-Text-3 console is not Interactive, means it will not ask you for any input, this just runs and show you the output.
            # BUT you have second option, When you want to automate your compiled program, OK, we are going to write every required query for proper functioning of our program in a separate text file and tell Sublime-Text-3 to take input data from this file Instead of STDIN FILE.

{
    "shell_cmd": "g++ -std=c++11 \"${file}\" -o \"${file_path}/${file_base_name}.exe\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++, source.cpp",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" -std=c++11 && mintty --hold always -e \"${file_path}/${file_base_name}.exe\""
        }
    ]
}





# Take a look :-


# Download Sublime Text Editor :-
    http://www.sublimetext.com/
    http://www.sublimetext.com/3


# References :-
    http://sublime-text-unofficial-documentation.readthedocs.org/en/latest/reference/build_systems.html

1/09/2015

C++ _ Scope of any Identifier : Talks to you



# Scope ?
    # In C++ before using any variables, functions, classes and types or compound types needs to be declare first somewhere in your C++ program.
    # Where you declare or define them, decides their visibility.
    # Visibility means whether you can use that everywhere or somewhere in your program.

# GLOBAL variables ?
    # A list of variables that can be used anywhere in your program without even declaring and defining them are GLOBAL variables.

# LOCAL variables ?
    # Those who exists in some defined code blocks.
    # outside that code block they does not exists.
    # sometimes local variables are used to protect GLOBAL variables.

 # In C++ you can declare a identifier once within a single scope.
 # In C++ visibility of Identifiers is influenced by curly brackets that is used to group more than one C++ statements together.
# This means you can create a new scope/world for your new/old variables.

 # Namespace ?
    # Namespace is a C++ Identifier that used to group some code functionality within a single name of identifier.
    # You must be familiar with  std  that is a namespace in C++ that holds all
standard C++ functions/routines, Global variables, Classes.

    # while printing something to Console/Terminal you use :-
        std::cout << "Hello world!" << endl;

    # This is how you can access any function or routine from std namespace even without importing anything from there.


# If you know Java/C#, than you may be familiar with this concept.


# Importing selected Identifiers from any Namespace ?


# Importing everything from selected Namespace into current scope ?

# scope matters while importing anything from any Namespace.



# Static , Global, and Local variables ?

  
# References :-
    http://www.cplusplus.com/doc/tutorial/namespaces/

C++ Arrays : Talks to you



# What is Array?
    # As you know that a variable can only store value one value at a time.
    # sometimes you want to create a lot of variable with the same type, So do you going to create each of them or Why do not you look at array? What if offers for you?
    # An Array is a series of elements of the same type placed in contiguous memory locations that can be easily accessed whenever wanted.

# Why Array was implemented ?
    # Instead of creating a lot of variables that will take more memory that will be better if we can declare an array in such a manner that can hold our all variables contents just using a single identifier name.


# How to declare an Array?
    # How to declare an Array using C++?
        # Let me declare an Array, so that i can put content there.
       
        type array_name[ array_size ];
       
        int foo[5];
       
        # We just declare an static array that can hold 5 integer values.

# How to declare and initialize an Array?
    # How to initialize Array using C++?
       
        int foo [5] = { 16, 2, 77, 40, 12071 };
        # Explicity initialize all elements of array
       
        int bar [5] = { 10, 11, 12};
        # remaining Array elements will be initialize to ZERO.
       
        int bar [5] = {};
        # all Array elements will be initialize to ZERO
       
        int foo[] = { 16, 7, 25, 40, 12909};
        # Compiler will assume that length of Array will be number of initialized elements.
       

        # New Syntax:- both are same
        int foo[] = { 10, 11, 12, 13, 14 };
        int foo[] { 10, 11, 12, 13, 14 };

# How to access values of an Array?
    # You need Index number of array element that you want to access.
    # Numbering starts from ZERO.
   
    int foo [3] = { 10, 11, 12};
   
    # means :-
    foo[0] = 10;
    foo[1] = 11;
    foo[2] = 12;

# What about Multidimensional Array?
    # Multidimensional Arrays are nothing more than Array of Arrays.
    # Means, an Array element will be an Array itself.
   
    int jimmy [3][5];
    # just declared a multidimensional array.
   
    [ 0x0 ] [ 0x1 ] [ 0x2 ] [ 0x3 ] [ 0x4 ]
    [ 1x0 ] [ 1x1 ] [ 1x2 ] [ 1x3 ] [ 1x4 ]
    [ 2x0 ] [ 2x1 ] [ 2x2 ] [ 2x3 ] [ 2x4 ]
   
    # Alternative Syntax for accessing Array elements:-
    ## Both are same:-
   
    int jimmy [3][5];
    int jimmy [15];    // 3 * 5 == 15


# What if i want to pass my Array as a function parameter?
    # To accept an array as parameter for a function, the parameters can be declared as the array type, but with empty array brackets, omitting the actual size of the array.

    int procedure ( int arg[] );
    # This function expects an Array of type Int.


    # You can call this function as
    int myarray[40];
    procedure( myarray );
   
   
# Is there any standard library to play with Array?
    # In C++ array are inherited from  C  language.
    # C++ has an alternative array type as a standard container. it is a type template ( a Class template) defined in header  <array>.
   
    # If you are using standard C++ array container then they give you some powerful methods that you can use with them.

 

# References :-
    http://www.cplusplus.com/doc/tutorial/arrays/

C++ Pointers & References : Talks to you




# Introduction :-
    # For a C++ program, the memory of a computer is like a succession of memory cells, each one byte in size, and each with a unique address.
    # These single byte memory cells are ordered in a way that allows data representations larger than one byte to occupy memory cells that have consecutive addresses.
    # When a variable is declared, the memory needed to store it value is assigned a specific location in memory ( its memory address )
    # Operating system decides the memory locations/addresses where your C++ store its data if needed.
    # So that will be great if using C++ we can find out the memory address of particular variable at run-time in order to access data cells that are at a certain position relative to it.
    # This is where, Pointers comes in field.
    # of-course pointer is just like other variables in C++ that can store some data, but that matters what you are going to store, In case of pointers we are going to store memory addresses.
    # Pointer is a Special Kind Of C++ Variable that can store run-time memory addresses for a particular variable.
   

# Pointers in C++?
    # Pointers are C++ variables and are special because compiler knows some special rules about them.
    # Basically we are going to store memory address in a given pointer.

# Why use Pointers?
    # http://stackoverflow.com/questions/162941/why-use-pointers

# Address of Operator?
    # This Address of Operator ( & ) used to obtain the address of a variable.
   
    foo = &myvar;
    # I have a simple C++ variable named  myvar  and i want to obtain its run-time address.
   
    # NOTE:- they can also create a function for that but they create an operator for it because it is more handy then a function.
        # You can Imagine ( & ) Address-Of-Operator as a function if that helps you to understand more.
       
        foo = &myvar;
       
        foo = getAddress(myvar);
        # Both will do the same thing, both will return the address of variable 'myvar'
        # Do not confuse here, getAddress() is s user-defined function that do exactly same as what Address-Of-Operator do.
        # OK now we have a special variable named 'foo' , actually this is a pointer variable with a special tag that tells compiler, it is a pointer not a normal variable, now compiler knows how to deal with it.

# De-reference Operator in C++ ?
    # This De-reference Operator ( * ) used to obtain the contents of a Pointer/Memory address.
   
    baz = *foo;
    # get contents from  foo  Pointer Variable and copy them into variable  baz.
   
    baz = getContents(foo);
    # Again, here  getContents() is a user-defined function that do exactly same as what De-reference Operator does.


# How to Declare Pointers in C++?
    # Pointers are special C++ variables because they have different properties according to their corresponding type definition.
    # Declaration of Pointer
   
    type * name;



   
    declarePointer(type, name);
    # NOTE that, declarePointer() is a user-defined function that does exactly same as that Dereference-Operator when used with type and variable name.
    # So, actually  (  *  ) itself able to create Pointers and for obtaining their contents, this knows whether you want to define a pointer variable or you want to obtain Pointer contents.
   

    # Examples :-
    int * number;
    char * character;
    double * decimals;
    # We defined three Pointer variables with different data type they points to. But all of them are pointers and all of them are likely going to take same amount of memory ( the size in memory of a pointer depends on platform where the program runs.)
   
    # When you use De-reference Operator (  *  ), it know about the type of Pointer Variable and will read only those number of bytes that it occupy.
    # for example, if a Simple C++ Char variable takes 1bytes of memory space, then while we De-reference its pointer than we will only  get the content of 1bytes not more than that.

    # NOTE :-
    As in the case with most variables, unless you initialize a pointer it will contain a random value. You do not want a random memory
    address to be accessed so you initialize a pointer to  NULL.
    NULL  is a value that can be checked against and one that can not be a memory address.
   
    # Declaring a Pointer with NULL value :-
    PointedType * PointerVariableName = NULL;  // declaring pointer and initialize NULL value
   
    So, declaring a pointer to an integer would be:-
      int *pInteger = NULL;

    # For a type  T  , T*  is the type 'pointer to T'
      means, a variable of type  T*  can hold the address of an object of type T.
      char c = 'a';   // lets understand this as :-
      {
        name: 'c',
        value: 'a',
        tags: {
          isPointer: false,
          type: 'char',
          address: '0x45ab78'
        }
      }

      char* p = &c;  // p  holds the address of variable c
      {
        name: 'p',
        value: '0x45ab78',
        tags: {
          isPointer: true,
          isConstant: false,
          type: 'char',
          address: '0x9078bc'
        }
      }

      int* pi;   // pointer to int
      char** ppc;   // pointer to pointer to char
      int* ap[15];   // array of 15 pointers to ints
      int (*fp)(char*);   // pointer to a function, taking a  char*  argument; returning a  int
      int* f(char*);   // function taking a  char*  argument; returning a pointer to int
   
     
    # NOTE:- Why we need to declare a data type for pointer variable/normal variable?
      Every data type in C/C++ takes defined number of bytes in memory. for example:-  char  date type variable use 1byte of memory.
      what if we have memory address of a char variable, So we can say that => start from this memory address and read this number of bytes.
      In case of  int  variable, we need to read only 1 byte of memory from the pointer (memory address).

# Pointers as Function arguments :-
    
 
# Pointers of C++ Arrays ?
    # Arrays work very much like pointers to their first elements, and actually, an array can always be implicitly converted to the pointer of the proper type.
   
    int myarray [20];
    int * mypointer;


   
    # The following assignment operation would be valid
    mypointer = myarray;
        # mypointer  and  myarray would be equivalent and would have very similar properties. The main difference being that mypointer can be assigned a different address, whereas myarray can never be assigned anything and will always represent the same block of 20 elements of type  int .

 
# Pointer Initialization
    # At the time of Declaring Pointers you can also Initialize them.
   
    int * myptr = &myvar;
   
    int myvar;
    int * myptr;
    myptr = &myvar;
   

# Pointer Arithmetic in C++ ( increment, decrement, etc.. )?
    # Arithmetic Operations with Normal C++ variables
   
    int i = 0;
    i++
    cout << i << endl;    // prints   1
   
    # Pointer Arithmetic Operations with special Pointer Variable
   
    int myvar = 10;
    int * i = &myvar;      // This is a pointer variable of type Integer
    // let i = 0x454545454     this is a memory address of variable  myvar
    i++;    // Increase pointer to one level.
    cout << hex << i << endl;  // prints  0x454545458
    // Because pointer variable i is of type int, so when we increase pointer variable one unit then it actually incremented by sizeof(myvar) to make sure that pointer will point to next address not in middle of any memory address.  
   
    char * mychart;   //     sizeof(char)
    short * myshort;  //     sizeof(short)
    long * mylong;    //     sizeof(long)
   
    *p++     // same as  *(p++)  increment pointer, and De-reference un-incremented address
    *++p     // same as  *(++p)  increment pointer, and De-reference incremented address
    ++*p     // same as  ++(*p)  De-reference pointer, and increment the value it points to
    (*p)++   // De-reference pointer, and post-increment the value it points to.

   
# Pointer with const ?
    # If you can access the content of a variable using pointers then you can also modify the content.
    # I want a read-only pointer so that i can read the content but i can not modify the content.
   
    int x;
    int y = 10;
    const int * p = &y;
    x = *p;    // OK, you can read the content where pointer points to.
    *p = x;    // ERROR, pointer points to content in const-qualified manner.


    # Pointers are variables, too, and hence the  const  keyword that is relevant to variables is relevant to pointers as well. However, pointers are a special kind of variable that contains a memory address and are also used to modify a block of data in memory.

      # there are three cases:-
      # when, the DATA, a pointer is pointing to is constant,
         and can not be changed, yet the address contained in the pointer can be changed,

         means, the pointer can also point elsewhere.

        g++> int HoursInDay = 24;
        g++> const int* pInteger = &HoursInDay;
        g++> cout << pInteger << endl;
            0x7fff07efe7d0
        g++> cout << &HoursInDay << endl;
            0x7fff07efe7d0
        g++> cout << *pInteger << endl;
        24
        g++> int MonthsInYear = 12;
        g++> pInteger = &MonthsInYear;
        g++> cout << *pInteger << endl;
        12
        g++> cout << pInteger << endl;   // pointer contained address will be changed..
            0x7fff07efe7d4
        g++> *pInteger = 13;
        [Compile error - type .e to see it.]
        g++> .e
        <stdin>: In function ‘int main()’:
        <stdin>:19:11: error: assignment of read-only location ‘* pInteger’
        g++> .u
        [Undone '*pInteger = 13;'.]
        g++> int* PAnotherPointerToInt = pInteger;
        [Compile error - type .e to see it.]
        g++> .e
        <stdin>: In function ‘int main()’:
        <stdin>:19:29: error: invalid conversion from ‘const int*’ to ‘int*’ [-fpermissive]


      # when, the ADDRESS contained in the pointer is constant and can not be changed, yet  the data pointed to can be chaged.
        g++> int DaysInMonth = 30;
        // defining a constant varaible pointer of type int
        // you can change the data conatined in the memory address, but you can not change the memory address
        // the pointer is pointed to..
        g++> int* const pDaysInMonth = &DaysInMonth;
        g++> *pDaysInMonth = 31;
        g++> int DaysInLunarMonth = 28;
        g++> pDaysInMonth = &DaysInLunarMonth;
        [Compile error - type .e to see it.]
        g++> .e
        <stdin>: In function ‘int main()’:
        <stdin>:15:14: error: assignment of read-only variable ‘pDaysInMonth’


      # when, Both the address contained in pointer and the value being pointed to are constant and cannot be changed (most restrictive variant):
        g++> int HoursInDay = 24;
        // So, pointer can only point to HoursInDay
        // You are not allowed to change the pointer address and pointer point to that DATA.
        g++> const int* const pHoursInDay = &HoursInDay;
        // If you try to change the DATA, pointer point to, you get an ERROR message.
        g++> *pHoursInDay = 25;
        [Compile error - type .e to see it.]
        g++> .e
        <stdin>: In function ‘int main()’:
        <stdin>:12:14: error: assignment of read-only location ‘*(const int*)pHoursInDay’
        g++> .u
        [Undone '*pHoursInDay = 25;'.]
        g++> int DaysInMonth = 30;
        // If you try to change the address the pointer contains..
        g++> pHoursInDay = &DaysInMonth;
        [Compile error - type .e to see it.]
        g++> .e
        <stdin>: In function ‘int main()’:
        <stdin>:13:13: error: assignment of read-only variable ‘pHoursInDay’



    int x;
        int *       p1 = &x;  // non-const pointer to non-const int
    const int *       p2 = &x;  // non-const pointer to const int
        int * const p3 = &x;  // const pointer to non-const int
    const int * const p4 = &x;  // const pointer to const int    


# Pointers to String literals?
    # String literals are arrays containing null-terminated character sequences.
    # String literals are arrays of proper array type to contain all its character plus the terminating null-character, with each of the elements being of type const char, as literals, they can never be modified.
   
    const char * foo  = "hello";
   
    # Because pointer  foo  points to a sequence of characters, And because pointers and arrays behave essentially the same way in expressions, foo can be used to access the characters in same way arrays of null-terminated character sequences are.
   
    *(foo+4)
    foo[4]
   
    g++> #include <iostream>
    g++> const char * foo = "hello";
    g++> std::cout << "Hello world!"<< std::endl;
    Hello world!
    g++> std::cout << foo << std::endl;
    hello
    g++> std::cout << *foo << std::endl;
    h
    g++> std::cout << *(foo+1) << std::endl;
    e
    g++> std::cout << *(foo+4) << std::endl;
    o
    g++> std::cout << foo[4] << std::endl;
    o
   

# Pointers to Pointers?
    # Pointers to Pointers, means you want to nest them, you are looking for a pointer that can pointer to a pointer instead of content.
   
    # Example :-
    int a = 492010;
    int * b = &a;
    int ** c = &b;
    cout << a <<  << b <<  <<  c  << endl;  => 492010  0x7fff11cb53fc  0x7fff11cb5400
    cout << a <<  << *b <<  <<  *c  << endl;  => 492010  492010  0x7fff11cb53fc
    cout << a <<  << *b <<  <<  **c  << endl;  => 492010  492010  492010
    cout << &a <<  << &b <<  <<  &c  << endl;  => 0x7fff11cb53fc  0x7fff11cb5400  0x7fff11cb5408
    cout << &a <<  << &b <<  <<  *&c  << endl;  => 0x7fff11cb53fc  0x7fff11cb5400  0x7fff11cb5400
   

# Void Pointer?
    # void type of pointer is a special type of pointer in C++.
    # void pointers are pointers that points to a value that has no type, if no type for a variable, means undetermined length and undetermined De-reference properties.
    # This gives void pointers a great flexibility, by being able to point to any data type.
    # But they define a great limitation for it, the data pointed by them can not be directly De-referenced.
    # So any address in a void pointer needs to be transformed into some other pointer type that points to a concrete data type before being De-referenced.
   
   

# Invalid Pointer or NULL Pointer?
    # because Pointers are just like normal C++ variables that can hold memory addresses, If memory address hold by a pointer does not exists or points to invalid element, means out pointer is Invalid.
    # for example, uninitialized pointers and pointers to non-existent elements of an array.
   
    int * p;    // uninitialized local variable ( isPointer: True )
    int myarray[10];
    int * q = myarray+20;  // Invalid pointer, element out of bounds
   
    # You can explicity point a pointer to nowhere. ( use of null pointer value )
    int * q = 0;
    int * p = nullptr;
    int * r = NULL;
    # all are same, means null pointer, points to nowhere.

# Pointers to Functions?
    # C++ allows operations with pointers to functions. The typical use of this is for passing a function as an argument to another function.
    # when you want a pointer points to a function just enclose function name in  parentheses () and and asterisk ( * ) is inserted before the name.
   
    int (*minus)(int, int);
   
    # function aliasing with the help of function pointers
    int (*minus)(int, int) = subtraction;
    # we want to create a pointer to a function named 'minus' that will points to pre-defined function named 'subtraction'.

   
   
    # References :-
        http://www.cplusplus.com/doc/tutorial/pointers/   

1/07/2015

C++ Functions : Talks to you




# What is a Function?
    # A function, so i have a list of C++ statements and i want to give them a name and whenever i am going to call that name those statements should be executed.
    # This is why, functions are re-usable block of code that can optionally take arguments.
    ## Does function arguments necessary?
        # No? they are optional, but they make functions more dynamic and powerful. 
   
# Why we need Functions?
    # When you write code then you might feel that THIS block of code of these number of statements are being repeated.
    # So now you are thinking that, Is there any technique that can help you, Because you are lazy ( or smart ) and you do not want to write those statements again and again.
    # Ok, I am going to pack these number of statements and let me think about a name that can characterize its property, means what it does and when anybody can call them.

# Types of functions in C++?
    # Function that return nothing.
    # Function that can return something.
    # Function that can return multiple values.
    # Recursive Functions ( sometimes i have to call myself. )


    # Function with some parameters.
    # Function with multiple parameters.

    # Over-loaded Functions ( Same name for function but different Signature)


    # Inline functions ( matters for compiler. )
    # Function with parameters values ( we got values for these parameters )
    # Function with parameters and some/all of them have some default value.


    # Function with references ( so they trust us, we got address for these parameters, now i can directly go there, If you do not mind we can also change the contents of that addresses that you give us. )





    # Function that return Memory Address ( Sorry, I can not return you any value but i can tell you where i put my results. )
    # Function that will promise you that you can trust them and give them references of parameters values, they will never try to change their contents.






# What is a C++ Function Template?
    # A block of C++ Code that will be rendered at compile time with supplied template parameters.
    # Functions with generic types, known as function templates.







# How C++ Function is different from C++ Function?
    # Function can return some value.
    # Template can not, this will be rendered with supplied template arguments.



# References :-
http://www.cplusplus.com/doc/

1/03/2015

very Basics of web elements

# you can imagine each HTML element as a baby ( Age: 0 year).
# and CSS rules as experiences for that baby when he becomes a boy ( Age: 10 year).
# and JavaScript is about the creativity, the way he thinks, the way he works and good parts
 and bad parts.

Logic:-
# So, you just define a HTML element inside your web page. that is nothing but more than a name which have its own HTML/CSS/JS code that is not visible to you, you can imagine this look/behavior as default, means there is nothing special, every person with the same HTML element will get the same rendering output, ( for same browser and same operating system.)

# OK, now you have a DEFAULT HTML element and you are going to select it using CSS techniques,
and you want to apply some cool and awesome CSS styles for this selected element ( Using CSS rules).
after completion of this step you will have a cool and great looking HTML element with your custom HTML content and custom CSS style. but till now you have not define any special behavior for that HTML element, OK we will do that using JavaScript.

# Now you can use JavaScript to make sure that your HTML element behaves as the same way as you expected, Its time to add some custom JavaScript code for this selected HTML element, and here we are going to select this HTML element using JavaScript code.

# Using JavaScript code you can define custom behavior for your HTML element, means you can attach user events with them and tell the Web Browser about them, Hello Browser, when this events happens you have to do this and for that event do this, if you have not detect this event than do nothing, just wait and wait until user close you or restart you. because JavaScript is a Event Driven language, means if there is any event occurs related to this element, do the job according to defined behavior. In JavaScript there is an Event loop and some default and custom event handlers code for that events, event loop start when JavaScript Engine initializes and runs until you close the Browser.

## So, this is what you want, with custom HTML + custom CSS + custom JavaScript code.