$29
Objectives:
1. Pointers
2. Address-of Operator
3. Dereferencing Operator
4. Structs
5. Pointer to Structs
6. Pass-by-value vs Pass-by-reference
1. Pointers
Every variable we declare in our code grabs a space in the memory. So it has an address in memory where it resides. A pointer is a memory address. By means of pointer variable we can access the address and the value at that address.
2. Address-of Operator
Consider the following lines of code:
intmain()
{
inta = 10;
cout<< a <<endl;
cout<< &a << endl;
return0;
}
Output:
10
0x7ffccbbcd804
The first cout is quite straightforward. It is simply printing the value of a. But what is being printed by the second cout. It is the address of the variable a. The operator ‘&’ is used to get the address of the variable a. Hence it is referencing operator or address-of operator.
1
CSCI 2270 – Data Structures
Recitation 2, September 2019
3. Dereferencing operator
Consider the following lines of code
intmain()
{
inta = 10;
• ∗ here denote p is pointer variable int∗ p = &a;
cout<< a <<endl; cout<< p <<endl;
//∗ is used to dereference p
cout<< ∗p << endl;
return0;
}
Output:
10
0x7ffccbbcd804
10
This code is a bit trickier than the previous one. Let’s see what is happening in the code. Note now we are storing the address-of a in another variable p. Note the type of p. It is int* instead of just int. int* p tells that p is a pointer variable(* denote it as pointer) and it will store the address of a int variable.
The second cout prints p, the address of variable a. Now what the third cout is doing? Remember p has the address of a. By putting a ‘*’ before p we are accessing the value stored at the position addressed by p. Hence * is dereferencing the address p.
4. Structs
Why struct?
There are some instances wherein you need more than one variable to represent a complete object.
As a student of CU, you should provide name, email, birthday, address to the college.
You could do like this:
std::stringname;
std::stringemail;
intbirthday;
std::stringaddress;
2
CSCI 2270 – Data Structures
Recitation 2, September 2019
But, the problem with this is you have 4 independent variables that is not grouped.
What is a struct?
C++ allows us to declare an aggregated(grouped) user defined data type. This user defined data type can in turn hold multiple variables of different data types. (Imagine like an array that holds multiple values of different data types )
structstudent
{
std::stringname;
std::stringemail;
intbirthday;
std::stringaddress;
};
5. Pointer to a struct
We can have address (pointers) to any type of variables even for structures.
structDistance
{
intfeet ;
intinch ;
};
intmain()
{
Distance d;
• declare a pointer to Distance variable Distance∗ ptr;
d.feet=8; d.inch=6;
//store the address of d in p
ptr = &d;
cout<<"Distance="<< ptr−>feet << "ft"<< ptr−>inch << "inches"; return0;
}
Output
Why don’t you try this one yourselves?
You may be wandering about ‘− >’. Recall to access members of a struct variable we use ‘.’ operator (e.g. d.foot = 8). When we have a pointer to a struct variable to use the member variables we will use − > operator.
3
CSCI 2270 – Data Structures
Recitation 2, September 2019
6. Pass-by-value vs Pass-by-reference
Pass by Value
#include <iostream>
usingnamespacestd;
voidadd2( intnum)
{
num = num + 2;
}
intmain()
{
inta = 10;
add2(a) ;
cout<< a;
}
What do you think the output will be? 12?
To your surprise it will be just 10. When we pass a variable as argument to a function in the system stack the function creates the local copy of the variable and performs the operation on that local copy. The caller function (main) has no knowledge of that local copy. Hence the change is local to the callee function (add2).
Pass by Pointers
#include <iostream>
usingnamespacestd;
voidadd2( int∗ num)
{
∗num = ∗num + 2;
}
intmain()
{
inta = 10;
add2( &a );
cout<< a;
}
In this case we are passing the address of a. The function will again create a local copy of the address. However since both of this addresses are same they will refer to to the variable a. Hence changing the value at the pointer will change the value of a and that change will be persisted.
4
CSCI 2270 – Data Structures
Recitation 2, September 2019
Pass by Reference
#include <iostream>
usingnamespacestd;
voidadd2(int&num)
{
num = num + 2;
}
intmain()
{
inta = 10;
add2( a );
cout<< a;
}
In C++, we can pass parameters to a function either by pointers or by reference.
In both the cases, we get the same result.
When you pass a parameter by reference, the parameter inside the function is an alias to the variable you passed from the outside. When you pass a variable by a pointer, you take the address of the variable and pass the address into the function.
Now examine the following code and try to find why it is persisting the change?
voidadd2( inta[], intlen)
{
for( inti= 0;i<len;i++)
a[i]+= 2;
}
intmain()
{
inta[] = { 1, 2, 3};
add2( a, 3);
for( inti= 0;i< 3;i++)
cout<< a[i] << endl;
}
Is the previous one similar/different to the one given below?
voidadd2( int∗ a, intlen)
{
for( inti= 0;i<len;i++)
a[i]+= 2;
}
intmain()
{
5
CSCI 2270 – Data Structures
Recitation 2, September 2019
inta[] = { 1, 2, 3};
add2( &a[ 0], 3);
for( inti= 0;i< 3;i++)
cout<< a[i] << endl;
}
Tip!
Pass By Value
If I send a fax to someone to sign, he creates a local copy i.e prints it and then signs. The person makes changes to his local copy that he printed out(The signature won’t be reflected in my original copy). He has to scan it and send it back, for me to see his signed document.
Pass by reference
Now consider I send my address to the person who is required to sign. The person comes to my place and then signs. In this case the person is modifying my original document.
Quiz
1. Consider an array int a[] = {1, 2, 3}. What is the output for the following?
a. cout << a+2;
b. cout << *(a+2);
c. cout << *a;
d. cout << *a[0];
2. How come we can pass an array name as an argument to a function and still be able to persist the change ?
Exercise
Your zipped folder for this Lab will have a main.cpp, swap.cpp and swap.h files. Follow the TODOs in these files to complete your Recitation exercise!
6