Starting from:
$35

$29

Data Structures Recitation 2 Solved

    2. Pointers


To understand pointers, we investigate how a C++ program operates on a Computer’s memory. For a C++ program, a computer memory is like a succession of memory cells, each one byte in size, and each with a unique address (hexa-decimal). These single byte memory cells are ordered such that datatypes with larger memory footprint (integer, float, long etc..) occupy memory cells that have consecutive addresses.

This way, an integer (4 bytes) can be stored in contiguous memory addresses starting from 1330 to 1333. And the unique memory address for this integer is identified as 1330.

For an array of integers of size 10 stored in contiguous memory addresses starting from 1401 to 1440, the array location is easily identified by its unique memory address 1401. The 0th indexed element of the array is stored in the memory block 1401-1404, the 1st indexed element in 1405-1408 and so on. It is here we need Pointers.

A pointer is a variable which stores a memory address of a variable. In the above example of an array, a pointer can be declared to store the address of the array (1401). Using this address, the C++ program can access the contents of the array by calculating the relative position from the starting address (1401).

2. Address-of Operator (&)

For the curious, you can get the physical memory address location of any variable by prefixing the variable with an ampersand (&) operator. This is called an Address-of or Referencing operator. The address as shown in the output is in a hexadecimal format.

Filename: address_of.cpp







1
CSCI 2270 – Data Structures

Recitation 2, Spring 2022




















Output










3. Dereferencing operator (*)


As we have mentioned in the section “Pointers”, they are just variables which are used to store the address of other variables. Colloquially, the Pointers are said to “point to” the variable whose address they store.

It gets its name as Pointers can be used to directly access the variable whose address it points to.

This is done by prefixing the pointer variable with the dereferencing operator (*).

Pointer variables are declared differently compared to regular variables. We know that we can fetch the address of a variable by prefixing it with an ampersand (&), and we have now learnt that the pointers store the addresses of a variable. The code below shows how the address of a is stored in a pointer variable p, and q.

For the keen eye, one can notice that both the pointers p and q “point to” the address of the same variable a. Thus, it is possible to have multiple pointers “point to” the SAME variable.

















2
CSCI 2270 – Data Structures

Recitation 2, Spring 2022







































Output














For better understanding, visual representation of the above program is shown below. The data within the box is the data stored in the specific memory location. The variables a, p, and q on accessing provides the data within the memory, and the characters below the boxes represent the physical memory address location.











3
CSCI 2270 – Data Structures

Recitation 2, Spring 2022



a    p    q

*p    *q


0x7fffc7497564    0x7fffc7497568    0x7ffc7497570





4. Structs


Sometimes, to represent a real-world object, simple datatypes are not enough. To represent a Student, we need some (if not all) of the following: age, gender, date-of-birth, name, address etc.

You could write code as follows:









This becomes increasingly complex if we would like to store multiple Student representations. One way to solve this is by using aggregated(grouped) user-defined datatype called Struct. This datatype can hold different datatypes grouped under a common variable of type Struct.












5. Pointer to struct

Pointers apply not only to simple datatypes, but aggregated datatypes as well.

We can have addresses (pointers) to any type of variable, even for structures. The following code shows how pointers can point to an aggregated datatype, as well as ways to assign values to struct and accessing them directly, as well as through a pointer.

Notice the cout statements. To access members of the struct variable we use dot (.) operator, and to access the members via a pointer variable, we use -> (dash followed by a greater than sign).







4
CSCI 2270 – Data Structures

Recitation 2, Spring 2022











































Output


Variable access: Distance= 8ft 6inches

Pointer access: Distance= 8ft 6inches




Exercise

Create a struct named BodyInfo with the weight and height values of a person. Write a C++ program to take inputs of height (in meters) and weight (in Kgs) from the user as arguments and assign those values to a BodyInfo variable. Also, print the individual values, the body mass index (BMI) of that person in the standard output.

The BMI is defined as the body mass divided by the square of the body height and is expressed in units of kg/m^2, resulting from mass in kilograms and height in meters.

Constraint: You can only use pointer access for calculation and printing the values.



5
CSCI 2270 – Data Structures

Recitation 2, Spring 2022





References

    • https://www.cplusplus.com/doc/tutorial/pointers/




























































6

More products