CHAPTER - 4 Compound Data Types
CHAPTER - 4 Compound Data Types
▪ Arrays
▪ Character Sequence
▪ Pointers
▪ Dynamic Memory
▪ Data Structures
2
Introduction
➢An array is a collection of elements of the same data type stored in
contiguous memory locations.
➢ Each compound data type has its own unique properties as well.
syntax
data_type array_name[size];
Example:
4
int numbers[5]; // Declares an integer array with 5 elements.
…continued
…continued
➢ Array Initialization:
Data-type array-name[expression]={elemnt1,elmnt2,…elm-n};
Eg:- int num[5]={0,1,2,3,4};
Float v[4]={0.5,1,1.5,-4,};
Char sex[2]={‘M’ , ’F’};
Char name[5]={‘R’ , ‘ a ’ , ‘ v ’ , ‘i’ , ‘ c ’}; 6
Accessing array elements
➢ How can we insert lists of arrays ?
➢ Coping Arrays:
const int SIZE=10
int x [SIZE] ;
int y [SIZE] ;
for (int i = 0 ; i < SIZE; i++) // Loop to do copying, one element at a time
x[i] = y[i];
7
Introduction to Array
➢ For example 1 , an array to contain 5 integer values of type int called
foo could be represented like this
1
0
11
One Dimensional Array
➢ It stores elements in a single dimension. And, in this array a single
specification is required to describe elements of the array.
➢ The diagram below shows that it arranged all the elements in row wise
in a single dimension, one after other.
Example
❖int A[10];
An array of ten integers
❖Char str[20];
❖ int a[ 100 ], b[ 27 ] ;
Array Initialization:
➢ double temperature [5] = {12.3 , 7.5 , 65 , 72.1, 87.5 };
temperature [ 4 ] 87.5
Index
16
Array Initialization:
➢ int N[ ] = { 1, 2, 3, 4, 5 };
In 1-D arrays it is optional to specify the size of the array. If size is omitted
during initialization then the compiler assumes the size of array equal to the
number of initializers.
➢ int N[5] = { 0 } ; // the first element of the array is being initialized to 0.
➢ int B[20] = {2, 4, 8, 16, 32};
Unspecified elements are guaranteed to be zero .
If not enough initializers, rightmost elements become 0 .
➢ int C[4] = {2, 4, 8, 16, 32};
Error — compiler detects too many initial values .
17
➢We can’t copy the elements of one array to another array by simply
assigning it.
Q? At what index is
the maximum located
22
Multidimensional arrays
▪ A multidimensional array is an array with more than one dimension.
▪ It is the homogeneous collection of items where each element is
accessed using multiple indices.
▪ A 2D array also falls under the category of a multidimensional array.
▪ MD array can have any number of dimensions.
Data type array_name [size 1][size2]….[size n]
where,
Data_type: Type of data to be stored in the array.
Array_Name: Name of the array.
size1, size2,…, sizeN: Size of each dimension.
23
Multidimensional arrays
➢ The size of an array is equal to the size of the data type multiplied by
➢type array_Name [ x ][ y ];
#include<iostream.h>
void main()
{
int SomeArray[5][2] = {{0,0},{1,2}, {2,4},{3,6}, {4,8}}
for ( int i=0; i<5; i++)
for (int j = 0; j<2;j++) {
cout<<"SomeArray["<<i<<"]["<<j<<'']:'';
cout<<endl<<SomeArray[i][ j];}
}
11
…continued
#include<iostream.h>
void main()
{
int v[3][4] = {{1,2,3},{5,6,7}, {9,10,11}}
for ( int i=0; i<3; i++)
for (int j = 0; j<4;j++) {
cout<<v[i][ j]<<“ “;
cout<<endl;}
}
12
Omitting the Array Size:
➢ If a one-dimensional array is initialized, the size can be omitted
as it can be found from the number of initializing elements:
➢ Note however:
int x[][] = { {1,2}, {3,4} } ; // error
is not allowed and must be written
13
int x[2][2] = { {1,2}, {3,4} } ;
Character Sequence/String
➢ String i s a sequence of character in which the last character
is the null character ‘\0’.
✓ The null character indicates the end of the string.
✓ Any array of character can be converted into string type by
appending this special character at the end of the array sequence.
✓ Hence if a string has n characters then it requires an n+1 element array
(at least) to store it.
➢ Eg:- char-data-type array-name[exp];
char s1[] = "example“ means |e|x|a|m|p|l|e|\0|
char s2[20] = "another example“ means
|a|n|o|t|h|e|r||e|x|a|m|p|l|e|\0|?|?|?|?|
14
…continued
➢ The real power of pointers is seen when they are used to point to
dynamically allocated variables.
➢ Dynamic variables are used just like ordinary static variables except:
➢ They are not declared, so they have no identifiers like static variables do.
➢They are created during run time, not when the program is compiled.
35
…continued
➢ Storage for these variables comes from an area of memory called the
free store or the heap.
➢ The creation of new dynamic variables is called memory allocation
and the memory is called dynamic memory.
➢ C++ uses the new operator create a dynamic variable.
▪ Memory in the C++ program is divided into two parts:
▪ Stack: All variables declared inside any function take up the stack's
memory.
▪ Heap: It is the unused memory of the program and can be used to
allocate the memory at runtime dynamically.
21
…continued
Syntax
➢ new returns a pointer (or memory address) to the location where the
data is to be stored.
22
Dynamic Array
➢ Allocate entire arrays with the new operator called dynamic arrays.
➢ This allows aprogram to ask for just the amount of memory space it
needs at run time.
Example:
int * myIntPtr;
myIntPtr [1]=135;
23
…continued
➢ The new operator gets memory from the free store (heap).
delete[] p;
40
…continued
Dangling pointer
➢ When you apply delete to a pointer variable, the dynamic
variable it is pointing to is destroyed.
➢ At that point, the value of the pointer variable is undefined,
which means that you do not know where it is pointing, nor what the
value is where it is pointing.
➢ These undefined pointer variables are called dangling pointers.
41
Data Structures
Data structures are ways to organize and store data efficiently to
facilitate operations like searching, sorting, and data manipulation.
➢ Examples:
• Student record: student id, name, major, gender, start year,
…
• Bank account: account number, name, currency, balance, …
• Address book: name, address, telephone number, …
Nested Structure
struct GradeRec {
float percent;
char grade;
};
struct StudentRec{
string lastName;
string firstName;
int age;
GradeRec courseGrade;
};
48
Exercise
50