0% found this document useful (0 votes)
3 views

CSC 218 Lecture Slides [Derived Types_array_pointer_vector_string]

Uploaded by

Habeeb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CSC 218 Lecture Slides [Derived Types_array_pointer_vector_string]

Uploaded by

Habeeb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CSC 218:

Computer Programming III (Scientific)


(3 Credits)

Amos O. Bajeh, PhD


Room 29, Ground Floor
CIS Building
Derived Types:

Arrays and Pointers


 Array
An array is a collection of items of the same type as a unit
under a common name
Type:Linear andMultidimensional Array
 Linear Array: an array which has only one dimension i.e. its
elements can be referenced using a single index number
X:
12 78 7 98 524

Index: 0 1 2 3 4
The elements of the array can be referenced using the name
and the index (position) of the elements:
1st element is X[0] 2nd element is X[1]
3rd element is X[2]4th element is X[3]
5th element is X[4] Thus, nth element X[n-1]
 Array
 Linear Array(cont’d)
In C/C++:
Linear Array declaration
Datatype arrayName[size];
e.g.: int x[10]

x:
0 1 2 3 4 5 6 7 8 9
 1-dimensional Array initialization
Datatype arrayName[] = {val1,val2,val2,…};
e.g.: int x[] = {23,5,9,12,89};
x: 23 5 9 12 89
0 1

0
1
2
 Array
 Muti-dimensional Array (cont’d): the general form for
declaring multidimensional array is as follows:

Datatype arrayName[size1][size2][size3]…[sizeN];
e.g.: int x[3][2]

 Initializing 2-dimensional array: each row in a


multidimensional array is placed in a pair of curly brackets

int x1[2][3] = {{5,2,31},{7,6,10}};


int x[3][2] = {{5,7},{2,6},{31,10}};
void myFunction(int *param) { . . . }

 Array
 Passing Array to function
 C++ doesn’t allow passing an entire array to a function
 Arrays are passed as pointers.
 Thus, arrays are passed by value to functions
 3 ways of declaring the formal parameters of the array
1.
returnType funcName (dataType *arrayName) {

}
2.
retType funcName (dataType arrayName[size]) {

}
void myFunction(int *param) { . . . }

 Array

 Passing Array to function (cont’d)

3.
retType funcName (dataType arrayName[]) {

See codes for examples


void myFunction(int *param) { . . . }

 Array

 Returning Array from function


 C++ doesn’t allow returning an entire array from functions
 Arrays can be returned by returning a pointer to the array
 Thus, the definition of a function that returns an array is:

returnType * funcName (list of params) {

Note:
 A pointer to a local variable can not be returned
 The returned variable can be declared as static
 Pointers
 A pointer is the memory address of a variable

1000
1001
1002
… : : :
9999
Variable declaration:
int x = 10;
x variable name
1002 10
Pointer
When address is stored in a variable, the variable is a pointer
variable
 Pointers
 Pointer Declaration
Datatype *pointerName;
Where datatype is any of the basic type or derived types,
pointerName is a valid identifier

Example:
int *p; //declare a pointer p of integer type

p = &x; //& is the address-of operator


x
1105

1105
p
 Pointers
 Pointer Dereferencing
 This implies accessing the value which a pointer points to.
 The format is:
*pointerName;
In this case, * is known as dereferencing operator

Example: *p;
Accesses the value of x which p points to i.e. 25.
x
1105 25

1105
p
 Pointers
 Dynamic variables
 These are variables that can be created and destroyed
during the cause of program execution
 They are created using the new operator and they are
unanimous
 The format is:
newdatatype ;
Where datatype is the type of the dynamic variable.
 The operator returns a pointer to the dynamic variable
Example: float *p;
p = new float;
This creates a dynamic variable and assign it to the pointer p
1105
p 1105
 Pointers
 Dynamic Memory Management
 The power of pointers lies in the ability to dynamically create
memory from the heap, use the memory and then dispose
the memory for other use i.e. recycle the memory.
 This is known as dynamic memory management
 The new operator dynamically allocates memory
 while the delete operator releases memory back to the heap
 The format for it use is:
delete pointerName
Example: int *p;;
p = new int; p

delete p; p X
 Pointers
 Operations on Pointers
 The Assignment operator
 Assigning one pointer to another changes where the LHS
pointer points to so that the two pointers now point to the
same location that the RHS pointer points to.

Example: int *p1, *p2;


p1 = new int; p1 13

p2 = new int; p2 88

p1 = p2; p1 13
p2
88
 Pointers
 The Assignment operator (cont’d)
 Assigning one dereferenced pointer to another dereferenced
pointer simply makes their separate pointing points to have
the same value with the value of the LHS overwritten with
the value of the RHS.

Example: int *p1, *p2;


p1 = new int; p1 13
p2 = new int; p2
88

*p1 = *p2; p1 88
p2
88
 Pointers
 Dynamic Array
 Ordinarily, an array is static
 Dynamic array are useful to solve problems where the size
of the array cannot be predetermined or estimated
 Pointers facilitate the creation of dynamic arrays
 The format is:
pointerVariable = new arrayDatatpe[size];
Example:
arrayA = new double[10];

arrayA
 The elements of the array can be referenced as usual using
the pointer name and index. E.g.
arrayA[3] is a reference to the 4th element
 Pointers
 Dynamic Array (cont’d)
 The name of an ordinary array is actually a pointer to the first
element of the array
 Thus, array name can be assigned to a pointer and treated
like a pointer

Example:
double arrayA[] = {45.3,7.9,66,96.7,88.5};
double *pointer;
arrayA

pointer = arrayA;
This makes pointer to also point to the first element and thus,
can be used to access the elements of the array
 Pointers
 Pointer Arithmetic: addition and subtraction
 The + and – operators can be used on pointers
 The + operator simply returns the pointer to the next
contiguous memory location
 While the – operator returns the previous contiguous
memory location
Example:
int *p1;
p1 = new int[5];
p1

(p1+1 ) (p1+2) (p1+3) (p1+4)


Assignment: Modify the code in class to demonstrate the use
of the subtraction operator on pointers
Derived Types:

Vector and String


 Vector
 A vector can be seen as an array that can grow and shrink in
size
 Ordinary arrays, once created, it length cannot be changed
 Vectors are similar to array but their length can be changed
during program execution

Vector Declaration
Basic syntax:
vector<datatype> vectorName;
E.g.: vector<int> v;
 Vector is actually a class defined in the standard header file
<vector>
 Thus, the file must be included in your program using:
#include <vector>
 Vector
 Use the built-in function member push_back( ) to add items
to a vector
E.g.:
v.push_back(29);
This will add 29 to the vector v

 Items of vectors can be accessed/referenced just like in


array:
E.g.: v[3]
is a reference to the fourth item in vector v
---------------------------------------------------------------------
 A vector of a given size can be declared using the following:
vector<datatype> vectorName(size );
E.g.: vector<double> vec(10);
 Vector
 Size and Capacity
 Size is the actual number of items in a vector
 The size of a vector can be determined using the member
function size( )
E.g.:
vector<float> vec(10);
vec.size() will return 10 as the size of the vector vec.

 Capacity is the number of elements for which memory is


reserved.
 The member function reserve( ) is used to reserve member
and thus, determines the capacity of vectors
E.g.: vec.reserve(50) will reserve memory for 50 elements
 Vector
 Size and Capacity (cont’d)
 The member function resize( ) can be used to change the
size of a vector
E.g.: vec.resize(100);
will change the size of vec to 100 and initialize the elements to
0.
 If the former size is smaller, the vector will be increased in
size.
 If the former size is greater, the remaining part of the vector
will be lost

You might also like