CSC 218 Lecture Slides [Derived Types_array_pointer_vector_string]
CSC 218 Lecture Slides [Derived Types_array_pointer_vector_string]
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]
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
3.
retType funcName (dataType arrayName[]) {
Array
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
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.
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.
*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
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