Skip to main content

Learn C++ Compound Types


\

Arrays 

Array is a fixed size collection of similar data type items. Arrays are used to store and access group of

data of same data type.
Arrays can of any data type. Arrays must have constant size. Continuous memory locations are used to store

array.
It is an aggregate data type that lets you access multiple variables through a single name by use of an

index. Array index always starts with 0.

Example for Arrays:

int a[5]; // integer array
char a[5]; // character(string) array

In the above example, we declare an array named a. When used in an array definition, the subscript operator

([]) is used to tell the compiler how many variables to allocate. In this case, we’re allocating 5

integers/character. Each of these variables in an array is called an element.

Types of Arrays:

# One Dimensional Array
# Two Dimensional Array
# Multi Dimensional Array 

Array declaration
int age [5];

Array initialization
int age[5]={0, 1, 2, 3, 4, 5};

Accessing array
age[0]; /*0_is_accessed*/
age[1]; /*1_is_accessed*/
age[2]; /*2_is_accessed*/

2 Two Dimensional Array

Two dimensional array is combination of rows n columns.
Array declaration
int arr[2][2];

Array initialization
int arr[2][2] = {{1,2}, {3,4}};

Accessing array
arr [0][0] = 1;
arr [0][1] = 2;
arr [1][0] = 3;
arr [1][1] = 4;

3 Multi Dimensional Array

//C++ programming language allows programmer to create arrays of arrays known as multidimensional arrays.


For example:
float a[2][4][3];


Pointer to an Array


Please go through pointers chapter first to understand this
An array name is a constant pointer to the first element of the array. Therefore, in the declaration:
double balance[50];

balance is a pointer to &balance[0], which is the address of the first element of the array balance. Thus,

the following program fragment assigns p the address of the first element of balance:
double *p;
double balance[10];
p = balance;

It is legal to use array names as constant pointers, and vice versa. Therefore, *(balance + 4) is a

legitimate way of accessing the data at balance[4].

Passing Array To Function

We can pass entire Arrays to functions as an argument.
For eg.

#include
void display(int a)
{
  int i;
  for(i=0;i < 4;i++){
    cout << a[i];
  }
}
int main(){
  int c[]={1,2,3,4};
  display(c);
  //Passing array to display.
  return 0;
}

Return array from functions

C++ does not allow to return an entire array as an argument to a function. However, You can return a pointer

to an array by specifying the array's name without an index.
If you want to return a single-dimension array from a function, you would have to declare a function

returning a pointer as in the following example:

int * myFunction()
{
int c[]={1,2,3}
.
.
.
return c
}



Please Guys take note that all the c++ tutorial will be updated slowly






















Comments

Popular posts from this blog

How To install Kali Linux (nethunter) On Android (Without root) via proot-distro and install Kali Linux Xfce Desktop On Android using Termux

What is kali linux? Kali Linux is a Debian-derived Linux distribution designed for digital forensics and penetration testing. It is maintained and funded by Offensive Security , This tutorial isn't only about learning how to install Kali Linux. Okay let's run down what we will be doing                        Table Of Content •Installing Kali Linux Terminal •Installing Kali Linux Desktop •Installing Default packages  •Creating vncserver port •Closing vncserver port •Closing multiple vncserver ports  •Reseting Kali Linux •Uninstalling Kali Linux  Okay!!!! Let's Begin Requirements: • Termux  : Click Here To Download Termux   Installation:   Please you can also copy and paste the commands below    The First thing we have to do is to make sure our repositories are up to date, to do that type the command  pkg update   It is essent...

How To install Windows 10 in Termux(Without rooting your Android phone)

   Hello guys what's up, Today we are going to see how easily we can run Windows 10 Operating System on Android Phone just by running few commands on Termux and if you will like this post I will also show you how to run the Windows 8.1 and Kali Linux GUI(Without root) on Android using Termux only in an upcoming post so just comment down below if you are interested. This method is quite interesting as this will make you understand how powerful a Linux terminal can be so without wasting any more time let’s start this tutorial. Below you will find all the resources you need and their download links so Download all the files, install, and copy it to your smartphone's Internal or External Storage. Requirements: •ZArchiver : Click Here To Download ZArchiver • Termux  : Click Here To Download Termux • AVNC :  Click Here To Download AVNC • Windows 10 : Click Here To Download The Windows 10 File After download the windows 10 file you need to Install termux and AVNC server...

Geegpay Writing Challenge: The Story of Amina

  Geegpay Writing Challenge Prompt: Life throws us lemons, and sometimes it's hard to make lemonade out of them. Amina, a 25-year-old aspiring photographer, is struggling to make ends meet while juggling freelance gigs and caring for her ageing mother. She feels invisible, undervalued, and on the verge of giving up. But then, an unexpected encounter with a renowned artist reignites her passion and inspires her to take control of her own narrative. Write a 300-word short story from Amina's perspective, showing her journey from despair to determination. Write about the challenges she faces, the things she learns about herself and how she finds the strength to turn things around.     Life throws us lemons, and sometimes it's hard to make lemonade out of them. My Story Starts Here:   Amina was walking down a crowded street with a heavy bag of cameras on her shoulder and a lot on her mind. 25 years old, she was feeling crushed by the pressures of life. She was struggling...