Basic Variables


Declaring and initialising variables ?



    
    
    
    

                    
0x0061ff10
0x0061ff14 20 int age;
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack

Output:

age: 20



    
    
    

                    
0x0061ff10
0x0061ff14 20 int age;
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack

Output:

age: 20



    
    
    

                    
0x0061ff10
0x0061ff14 ??? int age;
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack

Output:

age: 895727


Uninitialised variables causes undefined behaviour in a program as the variable may take on any random value.

Declaring and initialising variables...?



    
    
    
    

                    
0x0061ff08
0x0061ff0c 3 int prime;
0x0061ff10 2 int even;
0x0061ff14 1 int odd;
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack


    
    
    
    
    
    

                    
0x0061fef4
0x0061fef8 398154 long batchNum;
0x0061fefc
0x0061ff00 10 int quantity;
0x0061ff04 52.05 double calories;
0x0061ff08
0x0061ff0c 102.6 float mass;
0x0061ff10 "apple" string fruit;
0x0061ff14
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack

Note:

Different data types may have different sizes and take up different amounts of room in memory.

char: 1 byte

bool: 1 byte

int: 4 bytes

float: 4 bytes

string: 8 bytes

double: 8 bytes

long: 8 bytes

Assigning variables ?



    
    
    
    
    

                    
0x0061ff10
0x0061ff14 1 int id;
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack

Output:

id: 1

id: 2



    
    
    
    
    
    

                    
0x0061ff04
0x0061ff08 1111111 int phoneNumber;
0x0061ff0c 21 int age;
0x0061ff10 "Bob" string name;
0x0061ff14
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack

References ?



    
    
    
    
    
    

                    
0x0061fefc
0x0061ff00 20 int bobsAge;
0x0061ff04 "Bob" string bobsName;
0x0061ff08
0x0061ff0c 20 int age;
0x0061ff10 "Bob" string name;
0x0061ff14
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack

Note: Without the use of references



    
    
    
    
    

                    
0x0061ff08
0x0061ff0c 20 int age; int &bobsAge
0x0061ff10 "Bob" string name;
0x0061ff14 string &bobsName
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack

Note: With the use of references



    
    
    
    
    
    
    
    

                    
0x0061ff0c
0x0061ff10 "Bob" string name;
0x0061ff14 string &bobsName
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack

Output:

name: "Bob"

bobsName: "Bob"

name: "Bob Jones"

bobsName: "Bob Jones"

Pointers ?



    
    
    
    

                    
0x0061ff04
0x0061ff08 0x0061ff14 int *aliceHomePhone
0x0061ff0c
0x0061ff10 0211234567 int bobMobilePhone
0x0061ff14 1234567 int bobHomePhone
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack


    
    
    
    
    
    
    
    
    

                    
0x0061ff08
0x0061ff0c 0x0061ff14 int *prime;
0x0061ff10
0x0061ff14 3 int odd;
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack

Output:

addr in prime var: 0x0061ff14

*prime val: 3

odd val: 5

addr in prime var: 0x0061ff14

*prime val: 5



    
    
    
    
    
    
    
    
    

                    
0x0061fefc
0x0061ff00 0x0061ff10 string *favCol;
0x0061ff04
0x0061ff08 "blue" string colour2;
0x0061ff0c
0x0061ff10 "red" string colour1;
0x0061ff14
0x0061ff18 0x0061ff2d rtn addr
0x0061ff1c 0 rtn val
Stack


Output:

addr in favCol var: 0x0061ff10

*favCol val: "red"

addr in favCol var: 0x0061ff08

*favCol val: "blue"