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

Chapter 5

Chapter 5 discusses references and pointers in C++, explaining how variables are associated with names, types, and memory addresses. It details the use of the reference operator '&' to obtain variable addresses and how references serve as aliases for variables. Additionally, the chapter covers pointer declarations, operations, and the relationship between pointers and arrays, concluding with examples and exercises to reinforce the concepts.

Uploaded by

Tesfalegn Yakob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter 5

Chapter 5 discusses references and pointers in C++, explaining how variables are associated with names, types, and memory addresses. It details the use of the reference operator '&' to obtain variable addresses and how references serve as aliases for variables. Additionally, the chapter covers pointer declarations, operations, and the relationship between pointers and arrays, concluding with examples and exercises to reinforce the concepts.

Uploaded by

Tesfalegn Yakob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

References and Pointers

Chapter 5
References and Pointers in C++
We already know that a variable declaration associates three fundamental attributes to the
variable: its name, its type, and its memory address. For example, the declaration int n;
associates the name n, the type int, and the address of some location in memory where the
value of n is stored. Suppose that address is 0x0064fdf0. Then we can visualize n like
this:

Let n holds the number of section B students. I.e. int n = 66; this will be like follows in
memory.

In C++, you can obtain the address of a variable by using the reference operator &, also
called the address operator. The expression &n evaluates to the address of the variable n.
Let's see the program output for the above example.
int main()
{ int n=66;
cout << "n = " << n << endl; // prints the value of n
cout << "&n = " << &n << endl; // prints the address of n
}
Output:
n = 66
&n = 0x0064fdf0
References
A reference is an alias or synonym for another variable. It is declared by the syntax
type& ref-name = var-name;
where type is the variable’s type, ref-name is the name of the reference, and var-name
is the name of the variable. For example, in the declaration
int& rn=n; // rn is a synonym for n
rn is declared to be a reference to the variable n, which must already have been declared.
Let's write.

Page 1
References and Pointers

Output:

The two identifiers n and rn are different names for the same variable; they always have the same
value.
Like constants, references must be initialized when they are declared. But unlike a constant, a
reference must be initialized to a variable, not a literal:
int& rn=66; // ERROR: 66 is not a variable!
(Some compilers may allow this, issuing a warning that a temporary variable had to be
created to allocate memory to which the reference rn can refer.)
n=66;
int& rn=n; // right

Output:

The hexadecimal value in the output is the address of the value in the memory. They are
all the same because they all refer to the value of n. This address is the memory address
where 66 is stored in memory. You will never gate the same output for memory address

Page 2
References and Pointers

for the same program executed in different computers. Why do you think?

Pointers

C++ pointers are easy and fun to learn. Some C++ tasks are performed more easily with
pointers, and other C++ tasks, such as dynamic memory allocation, cannot be performed
without them.

As you know every variable is a memory location and every memory location has its
address defined which can be accessed using ampersand (&) operator which denotes an
address in memory. Consider the following which will print the address of the variables
defined: please have a look at the following program of your section.

When the above


code is compiled
and executed, it
produces result
something as
follows:

Note the difference between the addresses. It is two bytes. I.e 0x22fefc to 0x22fdfe. It is
the size of the data type int. Those address can be easily accessed using pointers.

A pointer is a variable whose value is the address of another variable. 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;

Page 3
References and Pointers

Here, type is the pointer's base type; it must be a valid C++ type and var-name is the
name of the pointer variable. The asterisk you used to declare a pointer is the same
asterisk that you use for multiplication. However, in this statement the asterisk is being
used to designate a variable as a pointer. Following are the valid pointer declaration:

int *ip; // pointer to an integer


double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character

The actual data type of the value of all pointers, whether integer, float, character, or
otherwise, is the same, a long hexadecimal number that represents a memory address.
The only difference between pointers of different data types is the data type of the
variable or constant that the pointer points to.

Using Pointers in C++:

There are few important operations, which we will do with the pointers very frequently.
(a) we define a pointer variables (b) assign the address of a variable to a pointer and (c)
finally access the value at the address available in the pointer variable. This is done by
using unary operator * that returns the value of the variable located at the address
specified by its operand. Following example makes use of these operations:

Output:

Page 4
References and Pointers

The second line on the output is not the same if you run in other computer. it is because it
is the particular location of the variable in the memory.

Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable in
many cases. For example, a pointer that points to the beginning of an array can access
that array by using either pointer arithmetic or array-style indexing. Consider the
following program:

When the above code is compiled and executed, it produces result something as follows:

Page 5
References and Pointers

However, pointers and arrays are not completely interchangeable. For example, consider
the following program:

It is perfectly acceptable to apply the pointer operator * to var but it is illegal to modify
var value. The reason for this is that var is a constant that points to the beginning of an
array and can not be used as l-value.

Pointer Arrays

Before we understand the concept of array of pointers, let us consider the following
example, which makes use of an array of 3 integers:

Output:

Page 6
References and Pointers

There may be a situation, when we want to maintain an array, which can store pointers to
an int or char or any other data type available. Following is the declaration of an array of
pointers to an integer:

int *ptr[MAX]; // MAX represents the maximum size of the array

This declares ptr as an array of MAX integer pointers. Thus, each element in ptr, now
holds a pointer to an int value. Following example makes use of three integers which will
be stored in an array of pointers as follows:

Output:

You can also use an


array of pointers to character to store a list of strings as follows: Don't worry about strings
u will see in the next portion. But see how we can see pointers in strings.

Page 7
References and Pointers

Output:

Exercises:
1) What is the difference between arrays and pointers?
2) What is the advantage of pointers?
3) Give five examples of valid pointer declarations?
4) Write a C++ program that accepts five values of an array, prints their value and memory
address to the screen in ascending order.
5) What is the scope of the variable MAX in the above program and what is the advantage of the
keyword const?

Page 8

You might also like