Lecture 5- DMA of 2d arrays- Intro to functions
Lecture 5- DMA of 2d arrays- Intro to functions
• A pointer to a
pointer “Holds
the address of
another
pointer.”
▪ It is declared
using to asterisk,
int **ptr
58 58 58
Pointer to Pointer
• A pointer to a pointer works just like a normal pointer — you can
perform indirection through it to retrieve the value pointed to.
• And because that value is itself a pointer, you can perform indirection
through it again to get to the underlying value.
int value = 5;
• This works just like a standard dynamically allocated array, except the
array elements are of type “pointer to integer” instead of integer
Array
How to dynamically allocate 2D Arrays
• Basic Idea:
1. Allocate an array of pointers (first dimension), → just like we did in previous slide
2. Make each pointer point to a 1D array of the appropriate size (2nd Dimension)
0
1
A 2
3
4
Let’s put it together
• Our dynamic two-dimensional array is a dynamic one-dimensional array of
dynamic one-dimensional arrays!
int **array = new int*[10]; // allocate an array of 10 int pointers — these are our rows
0 1 2 3 4
▪ array[0] is an array of length 1,
▪ array[1] is an array of length 2,
0
▪ etc…
1
A 2
3
4
Memory Deallocation
• Deallocating a dynamically allocated 2d array using
this method requires a loop as well, i.e,
• Each row must be deleted individually
• Be careful to delete each row before deleting the array
pointer.
Step 1:
for(int i=0; i<6; i++)
delete [ ] array[i];
Step 2:
delete [ ] array;
• These functions can be called from your main program or from other
functions
• This means that all of the code necessary to get a task done doesn't
have to be in your main program
80
Functions: Defining Them...
82
Functions: Calling pow...
83
Introduction to function parameters and
arguments
• Parameters vs Arguments
• In common usage, the terms parameter and argument are often
interchanged.
• A function parameter (sometimes called a formal parameter) is a
variable declared in the function declaration:
void square(int x); // declaration (function prototype) -- x is a
parameter
1. Pass by Value
2. Pass by Reference
3. Pass by Address
Passing arguments by value
• When the function call is executed,
Pointer: Reference:
x 9 x
9
ref
ref
int x = 9;
int x=9;
int &ref = x;
int *ref;
ref = &x;
int m = 10;
int &j = m; // j is a reference variable
cout << “value of m = “ << m << endl;
//print 10
j = 18;
cout << “value of m = “ << m << endl;
// print 18
Reference Variables
• First, when passing a large struct or class to a function, pass by value will
make a copy of the argument into the function parameter.
• when passing arguments by value, the only way to return a value back to the
caller is via the function’s return value.
Example of call by reference:
void convert (float inches, float & mils);
int main() {
float in; //local variable to hold # inches
float mm; //local variable for the result
cout <<“Enter the number of inches: “;
cin >>in;
convert (in, mm); //function call
cout <<in <<“ inches converts to “ <<mm <<“mm”;
return 0;
}
void convert (float inches, float & mils) {
mils = 25.4 * inches;
}
92
Example of call by reference:
void swap (int & a, int & b);
int main() {
int i=7, j = -3;
cout <<"i and j start off being equal to :" <<i
<<" & " <<j <<'\n';
swap(i,j);
cout <<"i and j end up being equal to :" <<i
<<" & " <<j <<'\n';
return 0;
}
void swap(int &c,int&d) {
int temp = d;
d = c;
c = temp;
}
93
What kind of args to use?
94
What kind of args to use?
• Use a call by value:
pointer.
▪ The function can then dereference the pointer to access or change the