H Pointer
H Pointer
Cecia Chan
Cindy Li
Brian Mak
int x;
4 = 1;
(x + 10) = 6;
cout << ++++++x << endl; // ANSI C++ Ref. Section 5.3.2
cout << x++++++ << endl; // ANSI C++ Ref. Section 5.2.6
{ kccecia, lixin, mak }@cse.ust.hk COMP2011 (Spring 2024) p.4
lvalue & rvalue: Return-by-Reference vs. Return-by-Value
1 requires x to be passed-by-reference
2 modify x by incrementing it by 1
3 returns x (with its new value) by reference
4 the returned x is an lvalue
1 requires x to be passed-by-reference
2 saves the current value of x in some temporary local variable
3 modify x by incrementing it by 1
4 returns the old value of x in the local variable by value
5 the returned value is an rvalue
{ kccecia, lixin, mak }@cse.ust.hk COMP2011 (Spring 2024) p.5
Analogy: Website Has an lvalue and an rvalue Too!
Question: Can you see the difference between PBV and PBR?
{ kccecia, lixin, mak }@cse.ust.hk COMP2011 (Spring 2024) p.8
Part II
What is a Pointer?
int main() {
int x = 10; int* y = &x; // y now contains the address of x
cout << "x = " << x << ’\t’ << "address of x = " << &x << endl;
cout << "y = " << y << ’\t’ << "address of y = " << &y << endl;
return 0;
}
Pointer to Structure
/* File: point.h */
struct Point
{
double x;
double y;
};
struct Date
{
unsigned int year;
unsigned int month;
unsigned int day;
};
struct Student_Record
{
char name[32];
unsigned int id;
char gender;
Dept dept;
Date entry;
};
// Global constants for department names
const char dept_name[ ][30]
= {"Computer Science", "Electrical Engineering", "Mathematics"};
{ kccecia, lixin, mak }@cse.ust.hk COMP2011 (Spring 2024) p.29
Example: sort-student-record.cpp Again
1 #include "student-record.h" /* File: sort-student-record.cpp */
2 #include "student-record-extern.h"
3
4 int main()
5 {
6 Student_Record sr[] = {
7 { "Adam", 12000, ’M’, CSE, { 2006 , 1 , 10 } },
8 { "Bob", 11000, ’M’, MATH, { 2005 , 9 , 1 } },
9 { "Cathy", 10000, ’F’, ECE, { 2006 , 8 , 20 } } };
10
11 Date d; // Modify the 3rd record
12 set_date(&d, 1980, 12, 25);
13 set_student_record(&sr[2], "Jane", 18000, ’F’, CSE, &d);
14
15 sort_3SR_by_id(sr);
16 for (int j = 0; j < sizeof(sr)/sizeof(Student_Record); j++)
17 print_student_record(&sr[j]);
18 return 0;
19 }
20 /* g++ -o sort-sr sort-student-record.cpp student-record-functions.cpp
21 student-record-swap.cpp */
{ kccecia, lixin, mak }@cse.ust.hk COMP2011 (Spring 2024) p.30
Example: student-record-swap.cpp Again
1 /* File: student-record-extern.h */
2
3 void print_date(const Date*);
4 void print_student_record(const Student_Record*);
5
6 void set_date(Date* x, unsigned int, unsigned int, unsigned int);
7 void set_student_record(Student_Record*, const char[],
8 unsigned int, char, Dept, const Date*);
9
10 void swap_SR(Student_Record*, Student_Record*);
11 void sort_3SR_by_id(Student_Record sr[]);
Dynamic Memory/Objects
Allocation and Deallocation
int main()
{
int x = 5; // Local int variable
char s[16] = "hkust"; // Local char array
return 0;
}
program codes
(global)
static data
stack
heap
pointer variable.
2 Always check whether a pointer is a nullptr before using
it.
int main()
{
int* ip = create_and_init(10);
cout << *ip << endl;
return 0;
}
memory leak
new int
int main()
{
Date a = { 2006 , 1 , 10 }; Date b = { 2005 , 9 , 1 };
swap(a, b); return 0;
}
head nullptr
’m’ ’e’ ’t’
The first and the last node of a linked list always need special
attention.
For the last node, its next pointer is set to nullptr to tell that
it is the end of the linked list.
We need a pointer variable, usually called head to point to the
first node.
Once you get the head of the linked list, you get the whole list!
{ kccecia, lixin, mak }@cse.ust.hk COMP2011 (Spring 2024) p.52
Basic Operations on a Linked List
1 /* To create a node */
2 ll_node* p = new ll_node;
3
4 /* To access/modify the data in a node */
5 cout << p->data;
6 cout << (*p).data;
7
8 cin >> p->data;
9 p->next = nullptr;
10
11 /* To set up the head of a linked list */
12 ll_node* head = nullptr; // An empty linked list
13 head = p; // head points to the node that p points to
14
15 /* To delete a node */
16 delete p; // Dangling pointer
17 p = nullptr; // Reset the pointer for safety reason
Common operations:
Create a new linked list.
Search data in the list.
Delete a node in the list.
Insert a new node in the list.
For all these operations, again special attention is usually
needed when the operation involves the first or the last node.
head nullptr
’m’ ’e’ ’t’
1 new_cnode ’a’ 3
head nullptr
’m’ ’e’ ’a’ ’t’
prev current
1
3
head nullptr
’m’ ’e’ ’a’ ’t’
head nullptr
’m’ ’a’ ’t’
root
10
8 15
5 9 12 17
Array as a Pointer
xp + N == &x + sizeof(<type>) × N.
xp - N == &x - sizeof(<type>) × N.
head nullptr
’m’ ’e’ ’t’
int* x delete [ ] x
int** x;
x
x[0] x[0][0] x[0][1] x[0][2] x[0][3]
Further Reading 0:
Relation between Dynamic 2D Array &
Pointer
x &x[0] &x[0][0]
0x14ea5010 0x14ea5010 0x14ea5030
Further Reading I:
Arguments for the main( ) Function
"Adam"
12000
MALE
CSE
2006
Student_Record* srp[3];
JAN
10
&sr[0] "Bob"
11000
MALE
&sr[1] MATH
2005
SEP
&sr[2] 1
"Cathy"
10000
FEMALE
ECE
2006
SEP
20
// Function declarations
btree_node* create_btree_node(int data);
void delete_btree(btree_node*& tree) ;
void print_btree(const btree_node* tree, int depth = 0);