0% found this document useful (0 votes)
37 views38 pages

Understanding Arrays, Strings, and Pointers in C++

The document provides an overview of arrays, strings, and pointers in C++. It explains the structure and declaration of arrays, including one-dimensional and two-dimensional arrays, as well as the manipulation of strings using the C++ string library. Additionally, it covers the concept of pointers, their declaration, and usage in accessing memory addresses and values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views38 pages

Understanding Arrays, Strings, and Pointers in C++

The document provides an overview of arrays, strings, and pointers in C++. It explains the structure and declaration of arrays, including one-dimensional and two-dimensional arrays, as well as the manipulation of strings using the C++ string library. Additionally, it covers the concept of pointers, their declaration, and usage in accessing memory addresses and values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

C hapter Four

Array, Strings and pointer

1
Array
a group of similar types of elements
that have contiguous memory
location.
a container that encapsulates fixed size
arrays. array index starts from 0.
 An array is a consecutive group of memory
locations

 The memory locations in the array are


known as elements of array
 total number of elements in the array is call
2 the length of the array
Cont’d …
size of an Array is “static”. Once you have created an
“array”, you cannot change the number of elements it
holds.
 If you create an array with 42 occurrences, then it will always have 42
elements.

an array is a group of contiguous memory locations


that all have the same name and same type.
 This declares that x is an array of char variables.
char x[];
 This declares that y is an array of int variables.
int x[5];

4
Cont’d …
 Array is a collection of variables that can hold
value of same type and reference by
common name .
 Its is a derived data Structure
 Introduction Array indexing is always start
from zero
and highest address corresponds to last
element

5 CS_Dept
@ DDIT
One Dimensional
 Array
a group of elements having the same datatype
and same name. Individual elements are referred
to using common name and unique index of the
elements.
 The array itself is given name and its elements are
referred to by their subscripts. an array is denoted
as follows

Declaration :
• Data_type array_name [size of array ];

Eg:
• Int num[10];

8 CS_Dept @ DDIT
Array Visualization
Specifies an array of
variables of type int

int primes[10]; // An array of 10 integers

The name of
the array
index values

primes[9]
primes[0] primes[1] primes[2] primes[3] primes[4]
Example:
Notice how the first element is the “Zeroth.”
“Z” is the first element, with a subscript of “0”
x[0]
 To change an element, you x[1]
assign it this way. x[2]
X[3] = ‘Y’; x[3]
 This changes the value of the x[4]
array location, not the x[5]
subscript value. x[6]
 If your array is declared with a
x[7]
data type of char, then you x[8]
must use single quotes.
 Double quotes are used for String.
1
3
Declaring an Array Variable
 Array declarations use square brackets.

Example:
int c[4]= {1, 2, 3, 4};
or
int c[4];
c= {1, 2, 3, 4};
 Syntax:datatype label[size];
Example:
 Int prices [7];
 String names[5];
Cont’d …
 You can use an “Initializer List” enclosed in
braces to quickly insert values in your array:
int n[] = { 12, 44, 98, 1, 28 };

 Because this list has 5 values in it, the


resulting array will have 5 elements.

 If we had a char array, it would be


like this: char m[] = { ‘t’, ‘I’, ‘M ’,
‘ Y’ };

9
Accessing Array Elements
 To access an item in an array, type the name
of the array followed by the item’s index in
square brackets.

Example:- the
expression:-
names[0];
 will return the first
element in the names
array

Filling an Array:-
Assign values to
compartments:
Example:
 Write a program that display elements of one
dimensional array.

Out Put
10
300
20
400
900
Exercise
What is the value of c after the
following code segment?

Cout<<“ Result: “<<c[j];


}
Output:
2-Dimensional Arrays
 The arrays we've used so far can be thought
of as a single row of values.
 Each element of the 2-D array is accessed by
providing two indexes:
 a row index and a column index

 called as a an array of arrays

0 1
0 8 4
1 9 7
2 3 6
• value at row index 2, column index
0 is 3
Cont’d …
• A 2-d array is an array in which each element is itself
an array

• It have a number of rows and columns

 i.e: int num[4][3];

size of 2-D array

 Total bytes= no of rows*no of columns*size of(base


type)
 We declare a 2D array two sets of square
brackets: double height[20] [55];
2
 This 2D array has 20 rows and 55 columns
7
Initialize Two Dimensional Array in C++
the general form to initialize values to two
dimensional array in C++

Sintax:
data_type array_name[row_size][column_size]
= { {comma_separated_value_list} };

Example:- declaring and initializing values to two


dimensional array named arr of type int, containing
5 row and 2 column i.e., dimension of this array is
5*2

int arr[5][2] = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };

1
5
Cont’d …

1
6
Part - Two
C++ String

3
CS_Dept @ DDUIOT
3
Strings
Strings are the combination of characters.
In other words, you can think strings as an
array of characters.
• Examples: “hello”, “high school”, “H2O”.

C++ has a <string> library

Include it in your programs when you wish


to use strings: #include <string>

In this library, a class string is


defined and implemented

1
8
Declaration of strings
The following instructions are all equivalent.
They declare x assign the string “high school”
to it:

 string x= “high school”;


 string x;
x=“high school”;

1 CS_Dept
9 @ DDIT
Operations on strings
Concatenation
Let x and y be two strings
To concatenate x and y, write:
x+y;
string x= “high”; O utp
string y=z;“school”;
string z=highschool
ut
z=x+y; z= highschool was
cout< < “z= “< <z<< e fun
ndl;
z =z+“ was fun”;
cout< < “z= “< <z<< e
2
ndl;
0
C++ String
Example
#include
<iostream>
using namespace
std;
int main( ) {
string s1 =
"H ello";
char ch[] = {
} 'C', '+',
'+'}; Output:
Hello
string s2 =
C++
string(ch);
C++ String Compare
Example
#include
< iostream> O utpu
#include t:
<cstring> What is my
using favorite fruit?
namespace std; apple
int main (){ What is my
char key[] = favorite
"mango"; char fruit? banana
buffer[50]; What is my
do { favorite fruit?
cout<<"What is my favourite mango
fruit? "; cin>>buffer; Answer is
}
correct!!
while (strcmp(key,buffer) != 0);
C++ String Concat Example
#include
<iostream> Outpu
#include t:
Enter the key
<cstring> using string:
namespace std; Welcome to
Enter the buffer
int main()
string: C++
{ Programming.
char key[25], Key = Welcome to
buffer[25]; C++
Programming.
cout < < "Enter Buffer = C+
the key string: +
"; Programmi
[Link](key, ng.
cout
25); < < "Buffer = " < <
buffer<<endl;
cout < < "Enter return 0;
the buffer
} string: "; [Link](buffer,
C++ String Copy Example
#include
Output
<iostream> :
#include Enter the key
string: C++
<cstring> using
Program
namespace std; Key = C++
int main(){ Program Buffer =
C++ Program
char key[25],
buffer[25];
cout
cout <
<< < "Enter
"Key = "<< key < <
the key string: ";
endl; cout < < "Buffer = "<<
[Link](key,
buffer<<endl; return 0;
25);
}
strcpy(buffer,
C++ String Length Example
#include
<iostream>
#include
<cstring> using
namespace std;
int main(){
char ary[] = "Welcome to C++
Programming"; cout < < "Length of
String = " < <
strlen(ary)<<end
l;
return 0;
}
Part Three
Pointers

1
C++ Pointers
🞂 ​ A pointer is a variable, it is also known as
locator or indicator that points to an address
of a value.
🞂 ​A pointer is a variable whose value is the
address of another variable with the same
type.
🞂 ​ Like any variable or constant, you must
declare a pointer before you can work
with it.
🞂​ The general form of a pointer variable
declaration is type * var-name;
Cont’d …
// pointer to an integer
Example: int *ip;
// pointer to a double
double *dp;
//pointer to a float
float *fp;
char *ch; // pointer to character

🞂​ Apointer is a variable that holds the memory


address of
another variable of same type.
• This memory address is the location of another
variable where it has been stored in the
memory.
🞂 ​ It
supports dynamic memory allocation
schedules.
3
Cont’d …
Advantage of pointer
🞂​ Reduces the code and improves the
performance.
 It is used to retrieving strings, trees etc.

and used with arrays, structures and


functions.

🞂 ​ We can return multiple values from


function using pointer.

🞂 ​ It makes you able to access any memory


location in the computer's memory.
Cont’d …
Symbols used in pointer
Symbol Name Description
 & • Determine the
• Address
(ampersand address of a
operator
sign) variable.
 ∗ • Indirection • Access the value of
(asterisk operator an address.
sign)


Declaring a pointer
 The pointer in C++ language can be
declared using (asterisk symbol).
Exampl int ∗ a; //pointer to
e: char ∗
int
c; //pointer to char
Cont’d …
 A pointer is a variable which holds the
memory address of another variable.
 used for sorting the address of a memory
cell.
 We can use the pointer to reference this
memory cell.

3
1
Cont’d …
Address of operator(&)
🞂​ It is a unary operator that returns the
memory
address of its operand.
🞂​ Here the operand is a normal variable.
Eg. int x =
10; int *ptr =
&x;

🞂​ Now ptr will contain address where the variable


x is stored in memory.
9
Cont’d …
🞂 ​ The “address of ” operator (&) gives the memory
address of the variable.
🞂 ​ Usage: &variable name;

Example:
int a=100;
// to gets the value, use the variable name

cout<<a; //prints 100


//To get the memory address, add the address
operator before the variable name

cout<<&a; //prints 1024


10
Cont’d …

Example:
int a=100;
int *p=&a;
cout<<a<<

”<<&a<<e
ndl;
//
prints
a=100

and
3 &a=1
4
Cont’d …
Dereference operator (*)
• It is a unary operator that returns the value stored at
the
address pointed to by the pointer.
• Here the operand is a pointer variable.

🞂 ​ Example:
int x = 10;
int *ptr = &x;
cout<< ptr;
// address
stored at ptr
will be
displayed
cout<<*ptr
3
5
;
Cont’d …
🞂 ​ Example:

3
6
Cont’d …

Output: value1=5 and value2 = 20


3
7
Question

3
8

You might also like