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

ITC Lect 17 (Pointers and Strings - I)

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

ITC Lect 17 (Pointers and Strings - I)

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

Lecture 17: Pointers and Strings CS 101: Introduction to Computing

Pointers and Strings - I

Dr. Zahid Halim

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 17: Pointers and Strings CS 101: Introduction to Computing

Introduction
• Pointers
– Powerful, but difficult to master
– Simulate call-by-reference
– Close relationship with arrays and strings

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 17: Pointers and Strings CS 101: Introduction to Computing

Pointer Variable Declarations and Initialization


• Pointer variables
count
– Contain memory addresses as their values
– Normal variables contain a specific value (direct reference) 7
– Pointers contain the address of a variable that has a specific value (indirect
reference)
• Indirection countPtr count

– Referencing a pointer value 7


• Pointer declarations
– * indicates variable is a pointer
int *myPtr;
declares a pointer to an int, a pointer of type int *
– Multiple pointers require multiple asterisks
int *myPtr1, *myPtr2;

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 17: Pointers and Strings CS 101: Introduction to Computing

Pointer Variable Declarations and Initialization


• Can declare pointers to any data type
• Pointers initialization
– Initialized to 0, NULL, or an address
• 0 or NULL points to nothing

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 17: Pointers and Strings CS 101: Introduction to Computing

Pointer Operators
• & (address operator)
– Returns the address of its operand
– Example
int y = 5;
int *yPtr;
yPtr = &y; // yPtr gets address of y
– yPtr “points to” y

y yptr y
5 500000 600000 600000 5
yPtr

address of y
is value of
yptr
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 17: Pointers and Strings CS 101: Introduction to Computing

Pointer Operators
• * (indirection/dereferencing operator)
– Returns the value of what its operand points to
– *yPtr returns y (because yPtr points to y).
– * can be used to assign a value to a location in memory
*yptr = 7; // changes y to 7
– Dereferenced pointer (operand of *) must be an lvalue (no constants)
• * and & are inverses
– Cancel each other out
*&myVar == myVar
and
&*yPtr == yPtr

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


1 // Fig. 5.4: fig05_04.cpp
2 // Using the & and * operators
3 #include <iostream>
4
5 using std::cout;
6 using std::endl; The address of a is the
7 value of aPtr.
8 int main()
9 {
10 int a; // a is an integer
11 int *aPtr; // aPtr is a pointer to an integer
12
13 a = 7; The * operator returns an
14 aPtr = &a; // aPtr set to address of a alias to what its operand
15 points to. aPtr points to
16 cout << "The address of a is " << &a
17 << "\nThe value of aPtr is " << aPtr; a, so *aPtr returns a.
18
19 cout << "\n\nThe value of a is " << a
20 << "\nThe value of *aPtr is " << *aPtr;
21
22 cout << "\n\nShowing that * and & are inverses of "
Notice how *
23 << "each other.\n&*aPtr = " << &*aPtr and & are
24 << "\n*&aPtr = " << *&aPtr << endl; inverses
25 return 0;
26 }

The address of a is 006AFDF4


The value of aPtr is 006AFDF4
The value of a is 7
The value of *aPtr is 7
Showing that * and & are inverses of each other.
&*aPtr = 006AFDF4
*&aPtr = 006AFDF4
Lecture 17: Pointers and Strings CS 101: Introduction to Computing

Calling Functions by Reference


• Call by reference with pointer arguments
– Pass address of argument using & operator
– Allows you to change actual location in memory
– Arrays are not passed with & because the array name is already a
pointer
– * operator used as alias/nickname for variable inside of function
void doubleNum( int *number )
{
*number = 2 * ( *number );
}
– *number used as nickname for the variable passed in
– When the function is called, must be passed an address
doubleNum( &myNum );

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


1 // Fig. 5.7: fig05_07.cpp
2 // Cube a variable using call-by-reference
3 // with a pointer argument
4 #include <iostream>
5
6 using std::cout; Notice how the address of
7 using std::endl; number is given -
8
9 void cubeByReference( int * ); cubeByReference
// prototype expects a
10 pointer (an address of a
11 int main() variable).
12 {
13 int number = 5;
14
15 cout << "The original value of number is " << number;
16 cubeByReference( &number );
17 cout << "\nThe new value of number is " << number <<
18
endl; return 0; Inside cubeByReference,
19 } *nPtr is used (*nPtr is
20
21 void cubeByReference( int *nPtr ) number).
22 {
23 *nPtr = *nPtr * *nPtr * *nPtr; // cube number in main
24 }

The original value of number is 5


The new value of number is 125
Lecture 17: Pointers and Strings CS 101: Introduction to Computing

Using the Const Qualifier with Pointers


• const qualifier
– Variable cannot be changed
– const used when function does not need to change a variable
– Attempting to change a const variable is a compiler error
• const pointers
– Point to same memory location
– Must be initialized when declared
int *const myPtr = &x;
• Constant pointer to a non-constant int
const int *myPtr = &x;
• Non-constant pointer to a constant int
const int *const Ptr = &x;
• Constant pointer to a constant int

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


1 // Fig. 5.13: fig05_13.cpp
2 // Attempting to modify a constant pointer to
3 // non-constant data
4 #include <iostream>
5
6 int main()
7 { Changing *ptr is allowed - x
8 int x, y; is not a constant.
9
10 int * const ptr = &x; // ptr is a constant
pointer to an
11 // integer. An integer can
be modified
12 // through ptr, but ptr
always points
13 // to the same memory
location.
14 *ptr = 7; Changing ptr is an error -
15 ptr = &y; ptr is a constant pointer.
16
17 return 0;
18 }

Error E2024 Fig05_13.cpp 15: Cannot modify a const object in


function main()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy