Skip to main content

C++ Arrays, functions, pointers and references.

In the previous section, I have discussed about Built-in Data Types in depth.

Now, let's taste the flavors of Variables and literals, with a in depth introduction.

Variables

A Variable provide us with named memory storage, i.e. a storage in our so called RAM with a particular name (usually we give the name), We can write, modify and use this storage with the help of associated name (variable). Each variable in C++ is associated with some specific data type, like Built-in data types that we previously discussed. 

There are mainly two values that are associated with any variable.

  1. RValues.
  2. LValues.
Let's see what all these means.
RValues - We know that a variable has some data associated with it, and we can read, write this data, this data that we read and write is termed as RValue. You can also think of RValues as Read Values or as a simple trick from me to you, you can think of as the right side values of assignment opeartor(=) .

LValues - We Also know that these variables are given some memory addresses by the computer, so, the memory address which is associated with the data of the variable is LValue. You can also think of LValue as location value.

for example - 

#include<iostream>
using namespace std;
int main()
{
    int a = 10;
    cout << a << endl; // 10 is the RValue of a integer variable
    cout << &a << endl; // The LValue of a integer variable
}

See output here.
In the output of this program, you will see that the lvalue is written in hexadecimal form, actually it is address of a memory location and due to very large size of the memory it is not easy to represent in bit representation.
Now, Why I used ampersand (&) is an another question and I will discuss it in the pointers section.

Now let's discuss about literals.

Literals

Literals or Constant Literals are nothing but the values that exists in your program, To be clear think of the above example here 10 is a literal but 'a' is not a literal. Just like variables which has some specific type, literals also have an associated type. for example constant literal 10 is an integer literal (getting it or not? if not read the para more than ones) or 3.14 is a double literal. (why not float? we will see). We can write down these literals in Octal, decimal or hexadecimal notation (Already discussed in Number systems).
Take the following example -

#include<iostream>
using namespace std;
int main()
{
    int a = 10; // Decimal representation of integer literal 10
    int b = 012; // Octal representation of integer literal 10
    int c = 0xa; // Hexadecimal representation of integer literal 10
    cout << "Value of a : " << a << endl;
    cout << "Value of b : " << b << endl;
    cout << "Value of c : " << c << endl;
}

see output here. Here, I have shown you three different configurations for representing integer literals.

To tell the compiler that the value we providing is an octal number we write a preceding "0" to the number, and for the hexadecimal values we write preceding "0x" to the numbers.

( I have not written return 0 in the above program as my compiler (mingw) allow me to skip it in the main function/ but you should write and test in your compiler)

Now let's see some of the other literals that we use with modifiers.

#include<iostream>
using namespace std;
int main()
{
    // Some example of integer literals
    long int a = 10L; // without L or l it will be treated as integer literal only.
    unsigned int b = 10U; // without U or u it will be treated as signed integer literal only.
    unsigned long int c = 10UL; // without UL or ul it will treated as signed integer only.
    
    // Some examples of float literal
    // I have given you example in the blog that 3.14 is double why?

    float x = 3.14; /* To make it a float literal F or f must be written like this 
                        3.14F or 3.14f 
                        Otherwise, it will be treated as double only.
                    */
    return 0;
}

If want to explore you can try to mix hexadecimal and octal with float and double.

Now, let's resume the things back again with Derived Data types.


Derived Data Types

Just like Built-in Data types, Derived Data types are provided by C++ in ready to use manner.
The reason for it to call them as derived data types is that, these are derived from built-in data types.
List of Derived Data Types - 
  1. Array.
  2. Function.
  3. Pointer.
  4. References.

Let's discuss them in the same fashion.

1. Arrays - Array is a collection of items of same data type. The benefit of array is we don't need to define individual name to every item that we need to use, We can name them just under a single name and can access the elements by means of their indices. The elements of the array occupies the contiguous memory locations. i.e. if the first element of an array is at memory address 1 and then second element will be 2 (assuming size of data type in bytes is 1) .
For example - int a; is just a single integer while int a[10] is an array that can contain 10 integers.(Remember not to initialize more values than the size of the array)
You can modify that values of an array as per your needs but you can't modify the size of an static array( size = Number of elements). There is a concept of Dynamic Arrays, but that is for future discussion.
(Important note - In C++ indexing of elements start from 0 onward i.e. first element is accessed by typing array[0], and so on.)
Now, let's see a program on how to declare and initialize an array.

syntax of array - <datatype> <name>[<size of array>];

#include<iostream>
using namespace std;
int main()
{
    /* You can define arrays of any data type you want */
    char alphabets[26] = {'a','b','c','d','e','f','g','h','i', // Array of data type character
                        'j','k','l','m','n','o','p','q','r','s',
                        't','u','v','w','x','y','z'};
    /*
    I defined all alphabets using single name
    But without array we could have used 26 different names
    which is a tiring task
    */                        
    return 0;
}

The below program 3 ways of declaring an array.

#include<iostream>
using namespace std;
int main()
{
    int array1[10]; // By mentioning the size of the array.

    int array2[] = {1,2,3,4,5,6,7,8,9,10}; // By giving the elements directly

    int *array3 = new int[10]; // Array is created in heap memory.

    return 0;
}

(new is an operator used for dynamic allocation, we will see it in pointers too).

Till now, we just have seen 1d array i.e. a linear array. But in c++ you can define arrays of any dimension, You can relate 2 dimensional array like int array[n][m]; with a matrix of n rows and m columns. Below is a small example.

#include<iostream>
using namespace std;
int main()
{
    int Array2[2][2] = {            // 2 dimensional array
        {1, 2},  
        {3, 4}
    };
    int Array3[2][2][2];            // 3 dimensional array 
    return 0;
}

Important Note - Actually it does not matter what are the dimensions of the array, it still occupies contiguous memory locations.


2. Function - Functions can be considered as a block of a program in which there is a mixture of loops, statements and logics is present to perform a task, The main criteria of using a function is that we can use a defined function any number of time as we want, or we can include them in our own separate header file and use those function in some other ".cpp" files..

Syntax of a function - <return type> <name of function> (parameters){ body }

Now, this syntax is looking little scary so let's take an example of a function which gives us the maximum of 3 numbers.


#include<iostream>
using namespace std;
int findingMax(int a, int b, int c)
{
    if(a >= b && a >= c)          // If "a" is greater than both "b" and "c" then "a" is greatest
    {
        return a;                 // return value of "a" if above condition is true
    }


    if(b >= c)   
        return b;                    /*
                                    if above condition is false than it means
                                    "a" must not be the greatest so we can exclude that
                                    */
        
    
    return c;                       /*
                                    If we have not found maximum till now just return 
                                    "c"
                                    */
}
int main()
{
    int a = 10;
    int b = 20;
    int c = 15;
    int d = findingMax(a,b,c);
    
    cout << d << endl;
    
    return 0;
}

See, results here.

Here we have written two functions one is findingmaxi() and another in main() function. Till now we just know that main() is the most important function.

Now, let's talk about what is actually happening, In main function we defined three variables "a", "b" and "c" with some values, and an integer "d" which is assigned to findingMax(a,b,c);, 

Here, in int d = findingMax(a,b,c); the findingMax is the name of the function and "a", "b" and "c" are parameters or actual arguments that we sent provide to over function, Now, we go the definition of our findingMax() function, Here the function is written as

int findingMax(int a, int b , int c){}, here int is the return type of this function i.e. when it, executes it return a value of type integer, which in turn is stored in variable "d" in main function. Parameter written during definition, must be provided with their data types as well. The parameters, we write during definition are referred to as formal parameters or arguments. After that whole logic is written inside the curly braces.

Please, try your best to understand the logic so that you get some pump. 

You can create function with void type too. Let's take the function with void type.


#include<iostream>
using namespace std;
void findingMax(int a, int b, int c)
{
    if(a >= b && a >= c)
    {
        cout << a << endl;
        return;
    }

    if(b >= c)
    {
        cout << b << endl;
        return;
    }

    cout << c << endl;
}
int main()
{
    int a = 10;
    int b = 20;
    int c = 15;
    findingMax(a,b,c);
    return 0;
}

See execution here.

If you look the output we got is same. But the difference is here is void does not returning anything to the main function, i.e. whatever, happening in findingMax does not provide any knowledge to main.


3. Pointers - Pointer is exactly the same things as it is named. Pointer points to the address of some other object (not necessarily a variable). Ok, but, what is the need of pointers? 

Pointers are extensively used in the application of data structures like linked list, trees etc.( We will cover them after we complete our c++ tutorial). Pointers are used to dynamically allocate memory, Pointer are also used to indirectly manipulate objects which are assigned to them

Now, what does this mean.

Till now we have seen that we can declare variables by just writing data types and and names, but those variables that we create are called automatic variables because they are automatically allocated and de-allocated through the stack - memory, but wait we have still not used our heap memory, what if we can utilize that space also, Yes, We can utilize it also by the help of pointers and new operator or in C we use malloc() function. 

But, wait, there's a fact that we can't directly access the variable present in heap memory, through our program,(What?😕), We can't access them directly but, Yes we can access them indirectly, otherwise just creating them doesn't make any sense. We can access them with the help of pointers i.e. we can store the address of the heap memory objects in our pointers and just by using them we can directly reach to that memory address. Now, As we are accessing pointers directly, it must be clear in our mind that pointer in this example is present in our stack memory, It can present in heap also but then we need one more pointer to point to heap memory pointer, i.e. pointer to a pointer.

Syntax - <data type> *<name>;

Let's see a program on pointer to an object.

#include<iostream>
using namespace std;
int main()
{
    int b = 10; // automatic variable in stack memory
    int *a;     // It is automatic too
    a = &b;     // "a" points to the address of b, but both are present in stack only.


    int *c = new int;       // with new we create objects in heap .
    *c = 123;

    // To find out the address of a pointer we dereference it or simply
    // Write it down by removing one of its preceeding (*);
    // And to find out address of a stack variable we simply use ampersand.
    // Remember a is equal to address of b here. (watch output)

    cout << "Address of b is : " << &b << endl;
    cout << "Address of *a : " << a << endl;

    cout << "Address of c : " << c << endl;

    delete c;
    
    return 0;
    
}

For accessing the address of an variables we use ampersand in front of it. like  I did when I am assigning the address of "b" to pointer "a" and while printing it too.

You can even create arrays (or anything you want) in heap memory like this.

int *array = new int[size];

but make sure to delete the it when it is not in use, for deleting an array write delete[] <name>;

We will discuss pointer arithmetic and use of pointers in parameter passing in later slides.

4. References - References, sometimes referred to as an alias, because, these serves as an alternative name for the other objects. Just like pointers, these are also used to indirectly manipulate the object but the syntax is bit different. Application of references are we can pass parameters by reference, overloading of operators etc.

Syntax - < datatype > &<name> = <Rvalue>

Important things to be remembered - 

  1. RValues is nothing but another object and must be of the same type of the reference object (except for const references).
  2. Once a reference is defined to one object it cannot be referenced to any other object. Yes the value of the object to which it is referencing can change.
  3. A reference must be initialized in the same step, where it is declared.
Let's see a program

#include<iostream>
using namespace std;
int main()
{
    int a = 10;

    // double &x = a;              // Wrong
    const double &x = a;        // Right

    int b = 10;
    int c = 20;

    int &d = b;                 // d is referencing to b
    d = c;                      // The value of bs change but d will still reference to b

    return 0;
    
}

Actually, the line double &x = a; and const double &x = a; are both not correct as but const double &x = a; will not generate compilation error because, there is an implicit conversion working here, and these conversion takes place in two steps -

  1. double temp = a; assigning the value of "a" to any temporary variable.
  2. const double &x = temp; referencing to that particular temporary variable.

which means x is not referencing on a itself but rather a temporary object that is created by compiler implicitly(internally without our knowledge). But, it is only possible when there is a conversion between two datatypes is applicable for example if you try to reference a double type to a string type, you will get invalid reference error.

This, is sufficient for now, I will be discussing more of these derived types in later slides.

In next slide we will discuss the User Defined Data Types.😊

 Stay tuned for the upcoming content. For any query leave a comment below.


Click the Subscribe button at the top, to follow my every post regarding c++. 

References -
  • Wikipedia.
  • hilite.com for code snippets.
  • ideone.com

Comments

Popular posts from this blog

Introduction to Computer Science.

 Before directly jumping deeply in c++, let's first start by creating the roots of computer science. In this post we will answer the following question. What is Computer and How it works? What is Program and Operating system? Low Level vs High Level Language? Compiler v/s Interpreter? By Knowing All these basic concepts You will be able to understand the upcoming more complex concepts easily. Let's start answering the above questions. What is Computer and How it works? What is computer? If I answer briefly,  what is computer?  then it is just a calculator used for doing simple calculations. If I have to answer where is computer used?, then I could probably say everywhere. We are surrounded by computer, for example mobile, smartwatches, and personal computer (obviously).  The below image is the first mechanical computer that we used for calculation.                                        Abacus How computer works? The computer's internal functionality is a whole separate bra

C-style strings vs String Class.

Here, we are going to learn about, what are C-Style Strings and what is String class . So, let's start by giving you the introduction. C-Style String is nothing but, an array of characters, and from the term array we can surely assume that these are static in size i.e. the size of these C-Style strings cannot be increased or decreased. String Class is a Built-in class that provide us much more functionality than C-Style String. C-STYLE STRINGS Before learning the String class, which is full-fledged feature of C++, we should rather start by taking a look at some important things about C-Style String. C-Style Strings are actually array of characters that are terminated by a null character "\0". If you are confused with, why null character? The reason is that, It helps us to define, upto which index we have some useful data present in our character array, and after null character there may or may not be some garbage values of characters. Let's first start by showing a

Algorithms, Pseduocodes and FlowCharts.

Some Basic Knowledge. C++ is a general purpose programming language, created by Bjarne Stroustrup and was firstly released in  1985. It is an extension to C language, Reason for that is if you are familiar with C language than you must be familiar with the fact that there are no classes, templates etc. present in C language. Due to which when classes are added to C language it is named as "C with Classes", but C++ is considered as default name now.  Extra thing to remember - In "C++" the symbol "++" after C is post increment symbol and therefore, "C++" means that In C additional features are added. Now, let's define, what are algorithms? Algorithms are nothing but set of rules that must be followed to accomplish some task. (Now, What that mean?😕) let's take an example of swapping two numbers, i.e. if a = 10 and b = 20 after swapping a = 20 and b = 10. How will you write the procedure for this?  (There are many such algorithms). Accordin