CPlusPlus arrays and strings

arrays and strings

image credit:http://wikipedia.org

CPlusPlus arrays and strings

CPlusPlus arrays and strings provide greater flexibility to store similar categories or types of values like integers, characters, decimals or even strings(to be discussed later)
When we were using the variables in our previous post, what we were concluding was:
A variable hold single value of certain type(s). With the facility of arrays in C++, we can store multiple values of same type which was not possible with normal variables. The values are stored in indices of an array as shown below

// header file for input and output
#include <iostream>

// namespace to point towards methods
using namespace std;

int main()
{

// myArray of type integer holding 10 numbers
int myArray[10] = {1,2,3,4,5,6,7,8,9,10};

cout << endl;
// element at zeroeth index
cout << myArray[0];

return 0;
}

Output 1

Strings

An object that contains sequence of words is known as string. String plays a very important role in C++ programming.
There are two types of strings om C++
C language type string
or
C++ language type modern string

Both arrays and strings occupy adjacent areas of memory. The array length is determined at the design time but the string can change its length during the execution of a program.

/*In computer programming, a precompiled header is a (C or C++) header file that is compiled
into an intermediate form that is faster to process for the compiler. Usage of precompiled
headers may significantly reduce compilation time, especially when
applied to large header files, header files that include many other header files,
or header files that are included in many translation units.
*/
#include “stdafx.h”
// for input and output
#include <iostream>
// for our string variable
#include <string>

using namespace std;

int main ()
{
string MyStr;

cout << “What is your name ? “;
cin >> MyStr;
cout << “Hello ” + MyStr;
cout<< ” “;

return 0;
}

Output

What is your name? Adeel
Hello Adeel

C-string can not contain 0 as a character in them. C-strings are essentially null terminated strings; hence the 0 contained in them is considered as end of the string. They can not hold 0 as character.

Important points
Windows is written in C++
Mac is written with Objective C but its kernel is in C and all IO PnP subsystem is Embedded C++
Linux uses C language, other applications are written in C++

Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.
Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.