25

c++ pointers by Amir Hamza Khan (SZABISTIAN)

Embed Size (px)

Citation preview

Page 1: c++ pointers by Amir Hamza Khan (SZABISTIAN)
Page 2: c++ pointers by Amir Hamza Khan (SZABISTIAN)

Name – Amir hamza khan

Instructor –Sir Irfan Latif Memon

Section – BSCS-1

RegNo. – 1312118

Page 3: c++ pointers by Amir Hamza Khan (SZABISTIAN)

Introduction to pointers.

The addresss of operator “&”.

Pointers and the “*” operator.

Pointers and function parameters.

Size of operators.

Relation between array and pointers.

Page 4: c++ pointers by Amir Hamza Khan (SZABISTIAN)

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.

Page 5: c++ pointers by Amir Hamza Khan (SZABISTIAN)

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).

Page 6: c++ pointers by Amir Hamza Khan (SZABISTIAN)

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.

Page 7: c++ pointers by Amir Hamza Khan (SZABISTIAN)

#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;

}

Page 8: c++ pointers by Amir Hamza Khan (SZABISTIAN)

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.

Page 9: c++ pointers by Amir Hamza Khan (SZABISTIAN)

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

Page 10: c++ pointers by Amir Hamza Khan (SZABISTIAN)

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

Page 11: c++ pointers by Amir Hamza Khan (SZABISTIAN)

#include <iostream.h>

main ()

{

int *ptr = NULL;

Cout<<“the value of ptr

is” <<ptr<<endl;

}

Page 12: c++ pointers by Amir Hamza Khan (SZABISTIAN)

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; }

Page 13: c++ pointers by Amir Hamza Khan (SZABISTIAN)

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".

Page 14: c++ pointers by Amir Hamza Khan (SZABISTIAN)

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.

Page 15: c++ pointers by Amir Hamza Khan (SZABISTIAN)

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.

Page 16: c++ pointers by Amir Hamza Khan (SZABISTIAN)

#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;

}

Page 17: c++ pointers by Amir Hamza Khan (SZABISTIAN)
Page 18: c++ pointers by Amir Hamza Khan (SZABISTIAN)

Arrays and pointers are closely related◦ Array name is like constant pointer

◦ Pointers can do array subscripting operations

Page 19: c++ pointers by Amir Hamza Khan (SZABISTIAN)

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 ]

Page 20: c++ pointers by Amir Hamza Khan (SZABISTIAN)
Page 21: c++ pointers by Amir Hamza Khan (SZABISTIAN)
Page 22: c++ pointers by Amir Hamza Khan (SZABISTIAN)
Page 23: c++ pointers by Amir Hamza Khan (SZABISTIAN)
Page 24: c++ pointers by Amir Hamza Khan (SZABISTIAN)
Page 25: c++ pointers by Amir Hamza Khan (SZABISTIAN)