KEMBAR78
Lecture 09 - Pointers | PDF | Pointer (Computer Programming) | Variable (Computer Science)
0% found this document useful (0 votes)
5 views40 pages

Lecture 09 - Pointers

The document provides an overview of pointers in C++, explaining their definition, usage, and operations such as dereferencing and memory management. It also discusses the differences between pointers and references, as well as the handling of strings in C++, including C-style strings and the C++ standard string class. Key examples illustrate how to declare and manipulate pointers, arrays, and strings effectively.

Uploaded by

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

Lecture 09 - Pointers

The document provides an overview of pointers in C++, explaining their definition, usage, and operations such as dereferencing and memory management. It also discusses the differences between pointers and references, as well as the handling of strings in C++, including C-style strings and the C++ standard string class. Key examples illustrate how to declare and manipulate pointers, arrays, and strings effectively.

Uploaded by

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

Computer Programming & Applications

Pointers
Pointer

• A variable that stores the memory address of another variable.


• Pointers enable efficient memory management, dynamic
memory allocation, and support for pass-by-reference in
functions.
• Direct memory access and manipulation
• Essential for dynamic memory allocation
• Critical for optimizing large data structures and algorithms.
Pointers
• A pointer is a variable whose value is the address of another variable. Like
any variable or constant, you must declare a pointer before you can work
with it. The general form of a pointer variable declaration is −

• type *var-name;

• Here, type is the pointer's base type; it must be a valid C++ type and
var-name is the name of the pointer variable. The asterisk you used to
declare a pointer is the same asterisk that you use for multiplication.
However, in this statement the asterisk is being used to designate a
variable as a pointer.
Pointers

• type *var-name;
‘*’ Indicates that it will store and address
• int *x;
• The * symbol is used to declare a pointer, indicating that it stores the
address of a variable of.
• int num = 10;
• int *ptr = # // &num gives the memory address of num
Valid Pointers
• int *ip; // pointer to an integer
• double *dp; // pointer to a double
• float *fp; // pointer to a float
• char *ch // pointer to character
• The actual data type of the value of all pointers, whether integer,
float, character, or otherwise, is the same, a long hexadecimal
number that represents a memory address. The only difference
between pointers of different data types is the data type of the
variable or constant that the pointer points to.
Using Pointers
• There are few important operations, which we will do with the
pointers very frequently.
• (a) We define a pointer variable.
• (b) Assign the address of a variable to a pointer.
• (c) Finally access the value at the address available in the pointer
variable. This is done by using unary operator * that returns the value
of the variable located at the address specified by its operand.
Dereferencing
• Dereferencing: Accessing the value stored at the memory address
pointed to by the pointer

• int num = 10;


• int *ptr = # Output 10
• cout << *ptr;
Example
• int* pointVar;
• Int var = 5;

• // assign address of var to pointVar


• pointVar = &var;

Output: address
• cout << pointVar << endl; Output: 5

• cout << *pointVar << endl;



Pointers and array
Array as a Pointer: An array name is a constant pointer to the first
element
int arr[5] = {6, 2, 3, 4, 5};
int *ptr = arr; {6, 2, 3, 4, 5};
cout << *ptr; Output: 6

( arr or ptr)
ptr++;
What will be size of ptr?
Size 4 Bytes or 8 Bytes
Pointers with array
• In C++, Pointers are variables that hold addresses of other variables.
Not only can a pointer store the address of a single variable, it can
also store the address of cells of an array.
• Suppose we need to point to the fourth element of the array using
the same pointer ptr.
• Here, if ptr points to the first element in the above example then ptr
+ 3 will point to the fourth element.
Pointers with array

Output: 10
11

cout << *(x + 2); // Output: 9


Pointers with array

Output: 62
Pointers

Output: H
O
Pointers
• A special pointer constant introduced in C++ to represent a null point
• int *ptr = nullptr; // Better than using NULL
• Constant Pointers:
• Pointer that Cannot Change:
• Once a pointer is assigned a value, it cannot point to another address.
• int num = 10;
• int *const ptr = &num;
Pointers
• Pointer to Constant • Constant Pointers:
• Pointer that Cannot
• Constant Value through Change:
Pointer: The value
• Once a pointer is assigned a
pointed to by the pointer value, it cannot point to
cannot be modified another address.
• int num=10; • int num = 10; int a=5;
• const int *ptr = &num; • int *const ptr = &num;
• *ptr = 5; // Error: • ptr= &a; //Error
Pointers
• Pointer to Constant
• Constant Value through Pointer: The value pointed to by the
pointer cannot be modified

• const int *ptr = &num;


• *ptr = 5; // Error: Cannot modify value through a constant pointer
Pointer with functions
• C++ allows you to pass a pointer to a function. To do
so, simply declare the function parameter as a pointer
type.
• Following a simple example where we pass an unsigned
long pointer to a function and change the value inside
the function which reflects back in the calling function
#include <iostream>
#include <ctime>
using namespace std;
void getSeconds(unsigned long *par);
int main () {
unsigned long sec;
getSeconds( &sec );
// print the actual value
cout << "Number of seconds :" << sec << endl;
return 0;
}
void getSeconds(unsigned long *par) {
// get the current number of seconds
*par = time( NULL );
return;
}
Example

Pointer with functions
• Pass-by-Value with Pointers

• Pass-by-Reference with Pointers


Pass-by-Reference with Pointers
Pass-by-Value with Pointers
Differences Between Pass By Value and Pass
By Reference in Pointers
Pass-by-Value Pass-by-Reference

Pointer Address The pointer itself is passed by value. The address The pointer is passed by reference,
it points to can be modified, but the pointer's so the address it holds (the pointer
address (the actual pointer) in the calling function itself) can be modified in the called
is not changed. function, which will affect the calling
function.
Can modify the pointer itself (e.g.,
Cannot modify the pointer itself in the calling
pointing it to a different memory
Change of Pointer function. Any change to the pointer (e.g., making it
location) in the calling function.
point to something else) is local to the function.

Change of Value The value pointed to can be modified, but the The value pointed to can be
pointer itself cannot be modified to point to another modified, and the pointer can also
variable be modified to point to another
variable.
Reference

• A reference variable is an alias, that is, another name for an already


existing variable. Once a reference is initialized with a variable, either the
variable name or the reference name may be used to refer to the variable.
• Think of a variable name as a label attached to the variable's location in
memory. You can then think of a reference as a second label attached to
that memory location. Therefore, you can access the contents of the
variable through either the original variable name or the reference. For
example, suppose we have the following example −
• int i = 17;
We can declare reference variables for i as follows.
• int& r = i;
Read the & in these declarations as reference. Thus, read the first
declaration as "r is an integer reference initialized to i"
Reference
#include <iostream>
using namespace std;
int main() {
int num = 10;
int &ref = num; // ref is a reference to num
cout << "Value of num: " << num << endl; // 10
cout << "Value of ref: " << ref << endl; // 10
cout<<" Address of ref is : "<<&ref<<endl;
cout<<" Address of num is : "<<&num<<endl;
ref = 20; // Changing the value through the reference
cout << "After modifying ref: " << num << endl; // 20
return 0;
}
Pointer with array
Reference vs Pointer
• References are often confused with pointers but three major
differences between references and pointers are −
• You cannot have NULL references. You must always be able to
assume that a reference is connected to a legitimate piece of storage.
• Once a reference is initialized to an object, it cannot be changed to
refer to another object. Pointers can be pointed to another object at
any time.
• A reference must be initialized when it is created. Pointers can be
initialized at any time.
Strings
Strings
• A string is a sequence of characters.
• Types of Strings in C++:
• C-style strings: Character arrays (null-terminated).
• C++ standard string class: Part of the C++ Standard Library, provides
more functionalities.
Strings
• C-style strings: also called Character Arrays.
• A C-style string is an array of characters terminated by a null
character ('\0')

• char s[] = "Hello Class";

char str[] = “AI-2024-Class";


cout << str[1];

char str[] = "Hello";


cout << strlen(str); // Output: 5
Strings
Concatenating Strings
char str1[20] = "Hello";
char str2[] = " World!";
strcat(str1, str2);
cout << str1; // Output: Hello World!
Finding Substrings

Strings Comparison
char str1[] = "Hello";
char str2[] = "World";
if (strcmp(str1, str2) == 0) {
cout << "Strings are equal";
}
Strings
Finding Substrings

char str[] = "Hello, World!";


char *sub = strstr(str, "World");
cout << sub; // Output: World!
Strings
• C++ String Class: Declared with the string keyword
• Dynamic resizing.
• Built-in member functions for handling strings.
• Safe and easy-to-use compared to C-style strings.

string str = "Hello, C++!";


cout << str; // Output: Hello, C++!
cout << str[3];

string str1 = “Mr.";


string str2 (“Jamal");
Strings
string str = "Hello";
cout << str.size(); // Output: 5

Concatenating Strings

string str1 = "Hello";


string str2 = " World!";
str1 += str2;
cout << str1; // Output: Hello World!
!
Strings
Strings Comparison

string str1 = "Hello";


string str2 = "World";
if (str1 == str2) {
cout << "Strings are equal";
}

string str = "Hello, World!";


string sub = str.substr(7, 5); // Starts at position 7, length 5
cout << sub; // Output: World
Strings
string str = "Hello";
str.insert(5, ", World");
cout << str; // Output: Hello, World
str.erase(5, 7); // Remove ", World"
cout << str; // Output: Hello

string str = "Hello";


transform(str.begin(), str.end(), str.begin(), ::tolower);
cout << str; // Output: hello
Strings
string str = "Hello, World!";
str.replace(7, 5, "C++");
cout << str; // Output: Hello, C++!
Thank You

You might also like