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

Lec 7 Pointers

The document provides an overview of pointers, explaining their purpose in accessing memory addresses, modifying function arguments, and creating data structures. It covers basic concepts, syntax, and the use of pointers with functions, strings, and objects, as well as the new and delete operators for memory management. Additionally, it includes a section for questions and discussions.

Uploaded by

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

Lec 7 Pointers

The document provides an overview of pointers, explaining their purpose in accessing memory addresses, modifying function arguments, and creating data structures. It covers basic concepts, syntax, and the use of pointers with functions, strings, and objects, as well as the new and delete operators for memory management. Additionally, it includes a section for questions and discussions.

Uploaded by

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

POINTER

LECTURER: AZMATULL AH HALIMZAI


1. Pointer overview

2. Basic Concept

3. Pointers and function

OUTLINE 4. Pointer and Strings

5. New and delete Operator

6. Pointer to object

7. Question and Discussion


OVERVIEW

Pointers: to access memory by its address rather than by its name, some common uses are

some common uses are

• accessing array elements

• modifying the original argument passed to function

• obtaining memory from the system

• creating data structures such as linked list


BASIC CONCEPT
• The address-of Operator &; to get the address
int main (){
int somevar;
cout « &somevar;
}

• The Pointer Variables


int main (){
int somevar;
int* ptr;
ptr = &somevar;
cout « ptr;
}
BASIC CONCEPT –CONTINUE…

• Accessing the variable pointed to


int somevar;
int* ptr;
ptr = &somevar;
cout « *ptr;
• Pointer to void
int someint = 5;
float somefloat = 9.3;
void* ptr;
ptr = &someint;
cout « ptr « ” : ”«*reinterpret cast(ptr);
ptr = &somefloat;
cout « ptr « ” : ”«*reinterpret cast(ptr);
EXCEPTON SYNTAX
Syntax class A{

public:

class Error {};

void Some (){

if (/∗ condition ∗/){

throw Error(); } };

int main() {

try {

A obj;

obj.Some(); }

catch(A::Error)

{ /∗ inform user and handle the error ∗/ } }


POINTERS AND FUNCTIONS
• We can pass arguments to a function as:
• By value
void somefun(int param){ }
int x = 8;
somefun(x);
• By reference
void somefun(int& param){ }
int x = 8;
somefun(x);
• By passing pointers
void somefun(int∗ param){ }
int x = 8;
somefun(&x);
POINTERS AND

STRINGS
As strings are array of characters therefore we ca interpret them as pointers as
below :
char str1[] = ”Some constant string”;
char* str2 = ”Some constant pointer string”;
cout « str1 « endl; //Some constant string
cout « str2 « endl; //Some constant pointer string
cout « ++str2 « endl; //ome constant pointer string
THE NEW AND DELETE OPERATORS
• new Operator: a different approach to obtain blocks of memory
• It obtains memory from operating system and returns a pointer to its starting
point
• We must release the memory assigned by new operator
• Thus use the delete operator to release the memory
POINTERS TO OBJECTS
• Pointers can point simple data types and arrays but can also point to objects
• Sometimes, however it is not known how many objects of a given class to
create as hardcoded
• In such cases, we can use new to create objects during program execution
Questions?
Subtitle goes Here

You might also like