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

lab11-pointers

This document is a lab guide for CpSc 1111 focused on understanding pointers in C and C++. It includes exercises on pointer operations, the relationship between arrays and pointers, and passing pointers as function parameters. Students are required to fill out an answer sheet based on guided self-exploration rather than submitting a program.

Uploaded by

Abdullah Emam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

lab11-pointers

This document is a lab guide for CpSc 1111 focused on understanding pointers in C and C++. It includes exercises on pointer operations, the relationship between arrays and pointers, and passing pointers as function parameters. Students are required to fill out an answer sheet based on guided self-exploration rather than submitting a program.

Uploaded by

Abdullah Emam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CpSc 1111 Lab 11

Pointers
Overview
Pointers are an integral part of C and C++. While they may be the cause of many headaches, they allow for a great deal of
control and flexibility. Because they are low-level constructs, they require a solid understanding of the memory model
behind it all. Not every language has pointers, but what you learn about memory management will apply to most every
language.
Unlike most of your previous labs this semester, this one will not require you to write and submit a program. This lab is
adapted from a lab used elsewhere, and it involves a good amount of guided self-exploration. We’ll give you the right
questions to ask along the way. Many of the questions will be open-ended, so when answering them, be sure to ask if you
are not 100% sure what’s going on! Please use the answer sheet provided . You will submit the answer sheet to the handin
page.
The code snippets here are screenshots, so you won’t be able to copy and paste them (sorry!) but they’re short enough to
type.
This lab will give you the opportunity to:
• Understand and use pointers and pointer operations
• Discover the relationship between arrays and pointers
• Explore passing pointers as function parameters

Download the answer sheet from the lab web page and fill in the answers using MS Word so that you can submit that .doc
file to the handin page when you are finished.

Warm Up
First, let’s practice with pointers to basic C data types and some related pointer operations. We’ll see referencing,
dereferencing, and arithmetic operations.
1. Compile and run this program:

2. Suppose after the declaration of the variables on line 4, we have the following:
Symbol Table:
Name Type Address
integer1 int 0x7fff8c1ff994
p1 int * 0x7fff8c1ff988
p2 int ** 0x7fff8c1ff980
Memory Chunk (address on the left, data on the right):
0x7fff8c1ff994
0x7fff8c1ff988
0x7fff8c1ff980

1
a. Fill in the above memory chunk table to reflect the changes that lines 6, 7, and 8 cause.

b. In we substitute lines 10-12 with the following two statements, then what will be the output of the program?

(*p1)++;
printf(“integer1= %d\n”, *p1);

c. Will the output be the same if we substitute the above two lines with the following two statements?

integer1++;
printf(“integer1= %d\n”, *p1);

d. Will the output be the same if we substitute the above two lines with the following two statements?

*p1++;
printf(“integer1= %d\n”, *p1);

e. Explain why the above outputs are the same or different from each other.

3. Consider the following program and answer the questions below.

What does variable p2 represent? Will this program successfully compile without warnings? Why or why not?
(Try it!)

2
Arrays and Pointers
Now let’s look at the relationship between pointer notation and array notation. (Hint: they both accomplish the exact same
thing!)
4. Compile and run the following program and answer the corresponding questions.

a. What is the output of the program?

b. What does array_of_integers evaluate to? If you dereference it, what value do you get? Try it.

c. Suppose we change lines 10-11 to the following:


for (i = 0;i < 10; i++)
*(ptr_of_array + i) = i;

Will the output change? Why or why not?

d. Suppose we change lines 13-14 of the original program to the following statements:
for (i = 0;i < 10; i++)
printf(“%d\n”, *(ptr_of_array + i));

Will the output change? Why or why not?

e. Suppose we change lines 10-11 of the original program to the following statements:

for (i = 0;i < 10; i++)


*(ptr_of_array++) = i;

Will the output change? Why or why not?

Now, does the variable ptr_of_array have the same value as the variable
ptr_of_first_element? Why or why not?

3
Functions and Pointers
In this part, you will get to play with pointers as function parameters and practice C’s version of pass-by-reference.
5. What is the output of the following code?
Why? (i.e. show what’s going on in memory)

You might also like