Computer Science, City University of
Semester B 2021-22
CS2310 Computer Programming
Copyright By PowCoder代写 加微信 powcoder
LT10: Pointer I
Memory and variable
Pointer and its operations
Call by pointer
Memory and Variable
Variable is used to store data that will be accessed by a program on execution.
Normally, variable will be stored in the main memory
A variable has four attributes:
Value – the content of the variable
Identifier – the name of the variable
Address – the memory location of the variable
Scope – the accessibility of the variable
Main Memory
Variable and Memory
void main(){
Identifier Value Address
Variable and Memory
Most of the time, the computer allocates adjacent memory locations for variables declared one after the other
A variable’s address is the first byte occupied by the variable
Address of a variable depends on the computer, and is usually in hexadecimal (base 16 with values 0-9 and A-F).
e.g. 0x00023AF0, 00023AF0
In C++, a pointer is a variable which designs to store the address of another variable. When a pointer store the address of a variable, we said the pointer is pointing to the variable
Pointer, like normal variable has a type, its type is determined by the type of variable it points to
Variable type int x; float x; double x; char x;
Pointer type int*Pointx; float*Pointx; double*Pointx; char*Pointx;
* and & operator
To declare a pointer variable, place a “*” sign before an identifier:
char *cPtr; //a character pointer
int *nPtr; //a integer pointer
float *fp; //a floating point pointer
To retrieve the address of a variable, use the “&” operator:
To access the variable a pointer pointing to, use “*” operator (dereference )
*nPtr=10;//x=10
y=*nPtr; //y=x
Address of variable x
reference vs. dereference
int x,y; //x and y are integer variables
void main(){
int *p1,*p2; /*p1 and p2 are pointers of integer typed */
p1=&x; /* p1 stores the address of variable x */
p2=&y; /* p2 stores the address of variable y */
*p1=5; /* p1 value unchanged but x is updated to 5 */
*p2=*p1+10; /*what are the values of p2 and y? */
Common operations
Set a pointer p1 point to a variable x
Set a pointer p2 point to the variable pointed by another pointer p1
Update the value of variable pointed by a pointer
Retrieve the value of variable pointed by a pointer
int x=*p2;
* operator will give the value of pointing variable (so that you can indirectly update/modify the pointing variable)
E.g., int x; int*p=&x; then using “*p” is equal to “x”;
& operator will give the address of a variable
Exercise: what are the errors?
char c=’a’;
char *ptr;
char c=’a’;
char *ptr;
ptr=&x; //error: ptr can only points to a char, but not int
ptr=c; //error: cannot assign a char to a pointer. A pointer can only store a location
ptr=&c; //correct
Exercise: What is the output?
int num=100;
int *ptr1;
ptr1=#
cout << num;
int num=100;
int *ptr1;
ptr1=#
cout << num; //print 40
Const pointer and pointer to const
If we add keyword const at the right-hand side of the ‘*’ sign, the declared pointer is a constant pointer
A constant pointer must be initialized in declaration
We cannot change the address that a constant pointer is pointed to
But we can still modify the value of the variable that the constant pointer is pointed to
int num=100;
int * const ptr1 = # //initialization
*ptr1=40; // value of num changes to 40
cout << num;
Const pointer and pointer to const
If we add keyword const at the left-hand side of the ‘*’ sign, this pointer is pointed to a constant value
This pointer can point to other constant values later. However, we cannot change the value that this pointer is pointed to
const int num1 = 100;
const int num2 = 150;
int const * ptr1; // or const int * ptr1
ptr1 = #
*ptr1 = 40; // illegal: cannot change the
// value of const int
ptr1 = &num2;
Pointer array
We can define a pointer array to manage multiple pointers. For example: int *n[5];
We can use the ‘*’ sign to change the value of the variable that each pointer (in the array) is pointed to
int *n[5];
int a[5] = {0}; // Initially all 0
for(int i = 0; i<5; i++){
n[i] = &a[i];
*n[i] = i;
} // The value of a[] changes to 0,1,2,3,4
Applications of pointer
Call by Pointer
Fast Array Access
Will be covered in later class
Dynamic Memory Allocation
Require additional memory space for storing value.
Similar to variable declaration but the variable is stored outside the program.
Call by pointer
Pass the address of a variable to a function
call by value cannot be used to update arguments to function
Consider the following function
Call by value
void f (char c1_in_f){
c1_in_f='B'; //c1_in_f=66
void main(){
char c1_in_main='A'; // c1_in_main =65
f(c1_in_main);
cout << c1_in_main;
Call by value
When calling f(), the value of c1_in_main (65 or 'A') is copied to variable c1_in_f
Inside f(), the value of c1_in_f is changed to 66 or 'B'.
c1_in_main remains unchanged (65 or 'A').
In call by value, there is no way to modify c1_in_main inside f(), unless we use the return statement.
Call by Pointer - Guideline
Add the ‘*’ sign to the function parameters that store the variable call by pointer
void cal(int *x, int y){
//x is call by pointer
//y is call by value
Add the ‘&’ sign to the variable when it needs to be call by pointer
cal(&result, factor);
Call by pointer
void f (char *c1_ptr){
*c1_ptr='B';
void main(){
char c1_in_main='A'; // c1_in_main =65
f(&c1_in_main);
cout << c1_in_main;
When f() is called, the following operation is performed
c1_ptr = &c1_in_main;
Call by pointer
void f (char *c1_ptr){
*c1_ptr='B';
void main(){
char c1_in_main='A'; // c1_in_main =65
f(&c1_in_main);
cout << c1_in_main; //print 'B'
Variable Variable type Memory location Content
c1_in_main char 3A8E 65
c1_ptr char pointer 4000 3A8E
Location of c1_in_main (location 3A8E) is assigned to c1_ptr1
c1_ptr = &c1_in_main;
Assign location 3A8E to c1_ptr
Call by pointer
void f (char *c1_ptr){
*c1_ptr='B';
void main(){
char c1_in_main='A'; // c1_in_main =65
f(&c1_in_main);
cout << c1_in_main; //print 'B'
Variable Variable type Memory location Content
c1_in_main char 3A8E 66
c1_ptr char pointer 4000 3A8E
c1_ptr='B'; //error
Reason: c1_ptr1 stores a location so it cannot store a char (or the ASCII code of a char)
c1_ptr points to location 3A8E (that is the variable c1_in_main).
*c1_ptr refers to the variable pointed by c1_ptr, i.e. the variable stored at 3A8E
Note the different meaning of *
void f (char *c1_ptr){
*c1_ptr='B';
void main(){
char c1_in_main='A'; // c1_in_main =65
f(&c1_in_main);
cout << c1_in_main; //print 'B'
The type of c1_ptr is char* (pointer to star)
dereference a pointer:
c1_in_main=‘B’
Call by value and call by pointer
In call by value, only a single value can be returned using a return statement
In call by pointer, the argument(s) can be a pointer which points to the variable(s) in the caller function
More than one variables can be updated, achieving the effect of returning multiple values
Call-by-Value
Call-by-Pointer
Pass value as parameter.
int add(int *m, int *n)
c = *m + *n;
void main()
int a=3, b=5,c;
c=add(&a,&b);
cout << a<<“ “<< c << endl;
Pass address as parameter.
int add(int m, int n)
void main()
int a=3, b=5,c;
c=add(a,b);
cout << a<<“ “<< c << endl;
Example: swapping values
#include
using namespace std;
void swap(int *p, int *q) {
int tmp;
tmp = *p; /* tmp = 3 */
*p = *q; /* *p = 7 */
*q = tmp; /* *q = 3 */
int main(void)
int a = 3, b = 7;
swap(&a, &b);
cout << a <<“ “<< b <
using namespace std;
void swap(int *p, int *q) {
int tmp;
tmp = *p; /* tmp = 3 */
*p = *q; /* *p = 7 */
*q = tmp; /* *q = 3 */
int main(void)
int a = 3, b = 7;
swap(&a, &b);
cout << a <<“ “<< b <
using namespace std;
void swap(int *p, int *q) {
int tmp;
tmp = *p; /* tmp = 3 */
*p = *q; /* *p = 7 */
*q = tmp; /* *q = 3 */
int main(void)
int a = 3, b = 7;
swap(&a, &b);
cout << a <<“ “<< b <
using namespace std;
void swap(int *p, int *q) {
int tmp;
tmp = *p; /* tmp = 3 */
*p = *q; /* *p = 7 */
*q = tmp; /* *q = 3 */
int main(void)
int a = 3, b = 7;
swap(&a, &b);
cout << a <<“ “<< b <
using namespace std;
void swap(int &p, int &q) {
int tmp;
tmp = p; /* tmp = 3 */
p = q; /* *p = 7 */
q = tmp; /* *q = 3 */
int main(void)
int a = 3, b = 7;
swap(a, b);
cout << a <<“ “<< b <