5. Array, String and Structure
5. Array, String and Structure
an array is a variable that can store multiple values of the same type. For example,
Suppose a class has 27 students, and we need to store the grades of all of them. Instead of creating
27 separate variables, we can simply create an array:
double grade[27];
Array Declaration
dataType arrayName[arraySize];
For example,
int x[6];
syntax to access array elements
array[index];
1
for (int i = 0; i < 5; ++i)
{
cin >> numbers[i];
}
cout << "The numbers are: ";
for (int n = 0; n < 5; ++n)
{
cout << numbers[n] << " ";
}
return 0;
}
Output
Enter 5 numbers:
11
12
13
14
15
The numbers are: 11 12 13 14 15
C++ Array Out of Bounds
If we declare an array of size 10, then the array will contain elements from index 0 to 9.
However, if we try to access the element at index 10 or more than 10, it will result in Undefined
Behaviour.
Multidimensional Arrays
we can create an array of an array, known as a multidimensional array. For example:
int x[3][4];
2
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
cin >> numbers[i][j];
}
}
cout << "The numbers are: " << endl;
// Printing array elements
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
cout << "numbers[" << i << "][" << j << "]: " << numbers[i][j] << endl;
}
}
return 0;
}
Output:
Enter 6 numbers:
1
2
3
4
5
6
The numbers are:
numbers[0][0]: 1
numbers[0][1]: 2
numbers[0][2]: 3
numbers[1][0]: 4
numbers[1][1]: 5
numbers[1][2]: 6
Strings
String is a collection of characters. There are two types of strings commonly used in C++
programming language:
Strings that are objects of string class (The Standard C++ Library string class)
C-strings (C-style Strings)
C-strings
In C programming, the collection of characters is stored in the form of arrays. This is also supported
in C++ programming. Hence it's called C-strings.
C-strings are arrays of type char terminated with null character, that is, \0 (ASCII value of null
character is 0).
How to define a C-string?
char str[] = "C++";
In the above code, str is a string and it holds 4 characters.
Although, "C++" has 3 characters, the null character \0 is added to the end of the string
automatically.
Alternative ways of defining a string
char str[4] = "C++";
3
char str[] = {'C','+','+','\0'};
char str[4] = {'C','+','+','\0'};
Like arrays, it is not necessary to use all the space allocated for the string. For example:
char str[100] = "C++";
Example 1: C++ String to read a word
C++ program to display a string entered by user.
#include <iostream>
using namespace std;
int main()
{
char str[100];
cout << "Enter a string: ";
cin >> str;
cout << "You entered: " << str << endl;
cout << "\nEnter another string: ";
cin >> str;
cout << "You entered: "<<str<<endl;
return 0;
}
Output
Enter a string: C++
You entered: C++
Enter another string: Programming is fun.
You entered: Programming
Notice that, in the second example only "Programming" is displayed instead of "Programming is
fun". This is because the extraction operator >> works as scanf() in C and considers a space " " has a
terminating character.
Example 2: C++ String to read a line of text
C++ program to read and display an entire line entered by user.
#include <iostream>
using namespace std;
int main()
{
char str[100];
cout << "Enter a string: ";
cin.get(str, 100);
cout << "You entered: " << str << endl;
return 0;
}
Output
Enter a string: Programming is fun.
You entered: Programming is fun.
To read the text containing blank space, cin.get function can be used. This function takes two
arguments. First argument is the name of the string (address of first element of string) and second
argument is the maximum size of the array.
string Object
In C++, you can also create a string object for holding strings.
Unlike using char arrays, string objects has no fixed length, and can be extended as per your
requirement.
Example 3: C++ string using string data type
#include <iostream>
4
using namespace std;
int main()
{
// Declaring a string object
string str;
cout << "Enter a string: ";
getline(cin, str);
cout << "You entered: " << str << endl;
return 0;
}
Output
Enter a string: Programming is fun.
You entered: Programming is fun.
In this program, a string str is declared. Then the string is asked from the user.
Instead of using cin>> or cin.get() function, you can get the entered line of text using getline().
getline() function takes the input stream as the first parameter which is cin and str as the location of
the line to be stored.
Passing String to a Function
Strings are passed to a function in a similar way arrays are passed to a function.
#include <iostream>
using namespace std;
void display(char *);
void display(string);
int main()
{
string str1;
char str[100];
cout << "Enter a string: ";
getline(cin, str1);
cout << "Enter another string: ";
cin.get(str, 100, '\n');
display(str1);
display(str);
return 0;
}
void display(char s[])
{
cout << "Entered char array is: " << s << endl;
}
void display(string s)
{
cout << "Entered string is: " << s << endl;
}
Output
Enter a string: Programming is fun.
Enter another string: Really?
Entered string is: Programming is fun.
Entered char array is: Really?
5
Structure
Structure is a collection of variables of different data types under a single name.
Declaration of a structure
struct Person
{
char name[50];
int age;
float salary;
};
structure variable
Person bill;
accessing members of a structure
bill.age = 50;
Example: C++ Structure
C++ Program to assign data to members of a structure variable and display it.
#include <iostream>
using namespace std;
struct Person
{
char name[50];
int age;
float salary;
};
int main()
{
Person p1;
cout << "Enter Full name: ";
cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;
return 0;
}
Output
Enter Full name: Amit Kumar
Enter age: 27
Enter salary: 1024.4
Displaying Information.
Name: Amit Kumar
Age: 27
Salary: 1024.4
6
Example: Pointers to Structure
#include <iostream>
using namespace std;
struct Distance
{
int feet;
float inch;
};
int main()
{
Distance *ptr, d;
ptr = &d;
cout << "Enter feet: ";
cin >> ptr->feet;
cout << "Enter inch: ";
cin >> (*ptr).inch;
cout << "Displaying information." << endl;
cout << "Distance = " << (*ptr).feet << " feet " << (*ptr).inch << " inches";
return 0;
}
Output
Enter feet: 4
Enter inch: 3.5
Displaying information.
Distance = 4 feet 3.5 inches
Union:
Union is a user-defined datatype. All the members of union share same memory location. Size of
union is decided by the size of largest member of union. If you want to use same memory location
for two or more members, union is the best for that. Unions are like structures.
#include<iostream>
using namespace std;
union result
{
int marks;
char grade;
float percent;
};
int main(){
result r;
r.marks=56;
cout<<"marks="<<r.marks<<endl;
r.grade='A';
cout<<"grade="<<r.grade<<endl;
r.percent=60.65;
cout<<"percent="<<r.percent;
return 0;
}Output:
marks=56
grade=A
percent=60.65