c++ pointers by Amir Hamza Khan (SZABISTIAN)

Ameerii132 894 views 25 slides Nov 09, 2014
Slide 1
Slide 1 of 25
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25

About This Presentation

This slides will help you to learn pointers in c++ and i have put in some programs in this slides to help beginners in c++ and also you can use it as your own presentation lol ;)
follow me on facebook https://www.facebook.com/Ameerii132
and you are welcome for questions and quirries


Slide Content

pointers Name – Amir hamza khan Instructor –Sir Irfan Latif Memon Section – BSCS-1 RegNo . – 1312118

Introduction to pointers. The addresss of operator “&”. Pointers and the “*” operator. Pointers and function parameters. Size of operators. Relation between array and pointers. CONTENTS

The pointer is a variable that holds a memory address. To understand the pointers we should first understand a bit about the computer memory. Whenever any application is executed, it is loaded in to the memory form the disk and all the elements and components of that application will be located at the particular location somewhere in the memory. Introduction to Pointers

Computer memory is divided into sequentially numbered memory locations and each variable is located at a unique location in memory, known as its address. The address of these memory locations is typically represented in Hexadecimal format with the prefix 0x before the number (e.g. 0x8f4ffff2). CONTINUE

As you know every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator which denotes an address in memory To return the address of the certain variables the address-of operator is placed in front of the variable name as, cout <<&var1; cout <<&var2; cout <<&var3; Now the “&” used with the variables in the cout statement will return the addresses at which these variable are located into the memory. The Address-of Operator “&”

#include < iostream.h > main () { int var1; char var2[10 ]; cout << "Address of var1 variable: "; cout << &var1 << endl ; cout << "Address of var2 variable: "; cout << &var2 << endl ; } Lets have a look on example

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 and the  Operator

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 Following are the valid pointer declaration

It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a  null  pointer Notes on Pointers

#include < iostream.h > main () { int * ptr = NULL ; Cout <<“the value of ptr is” << ptr << endl ; } Lets have a look on example

Example: Function to swap the values of two variables #include < iostream.h > void swap2( int * a, int * b) { int tmp ; tmp = *a; *a = *b; *b = tmp ; return; } int main() { int x = 1, y = 2 ; swap2(&x, &y); cout <<x<<y; return 0; } Pointers and Function Parameters

Pointers can be used to pass addresses of variables to called functions, thus allowing the called function to alter the values stored there. We looked earlier at a swap function that did not change the values stored in the main program because only the values were passed to the function swap. This is known as "call by value". CONTINUE

If instead of passing the values of the variables to the called function, we pass their addresses, so that the called function can change the values stored in the calling routine. This is known as "call by reference" since we are referencing the variables. The following shows the swap function modified from a "call by value" to a "call by reference". Note that the values are now actually swapped when the control is returned to main function . CONTINUE

The  sizeof  is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type . The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type . The syntax of using sizeof is : sizeof (data type) The  sizeof  is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type. The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type. Size of Operators

Lets have a look on Example #include < iostream.h > main () { cout << "Size of char : " << sizeof (char) << endl ; cout << "Size of int : " << sizeof ( int ) << endl ; cout << "Size of short int : " << sizeof (short int ) << endl ; cout << "Size of long int : " << sizeof (long int ) << endl ; cout << "Size of float : " << sizeof (float) << endl ; cout << "Size of double : " << sizeof (double) << endl ; cout << "Size of wchar_t : " << sizeof ( wchar_t ) << endl ; }

And output be like this

Arrays and pointers are closely related Array name is like constant pointer Pointers can do array subscripting operations Relationship Between Pointers and Arrays

Accessing array elements with pointers Assume declarations: int b[ 5 ]; int * bPtr ; bPtr = b; Element b[ n ] can be accessed by *( bPtr + n ) Called pointer/offset notation Addresses &b[ 3 ] is same as bPtr + 3 Array name can be treated as pointer b[ 3 ] is same as *( b + 3 ) Pointers can be subscripted (pointer/subscript notation) bPtr [ 3 ] is same as b[ 3 ] CONTINUE

Lets have a look on example:

CONTINUE

And output be like this

Lets have a look on example:

CONTINUE