Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

24
Welcome to Concepts of Pointers

Transcript of Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

Page 1: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

Welcome to

Concepts of Pointers

Page 2: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

Prepared by:-Sumit Kumar PGT(Computer Science)Kv ,Samba

Page 3: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

POINTER

DEFINITION:

DEFINITION:

The Pointer is a Variable which holds the Address of the other Variable in same memory. Such as Arrays, structures, and Functions that are used in program.

It contains only the Memory Location of the variable rather than its content.

Page 4: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

EXAMPLE FOR POINTER:

Int X = 547;

HERE:HERE:

Veriable name Contents Location

X 547 4000

ptr 4000 4036

According to above figure, By the help of ptr variable we stored the address of variable X in address 4036

Page 5: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

Pointers are used in following variables

Variables of any basic data type

An Array

Functions

Structures, and

Unions

Page 6: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

Advantages of Pointers To point to different data structures

To achieve clarity and simplicity

More compact and efficient coding

To return multiple values via functions

Dynamic memory Allocation

Page 7: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

Operators used with Pointers

1.The address operator

& [ampersand]

An address operator gives the address of the variable.

2.The indirection operator

‘*’ [asterisk]

The indirection operator gives the value of the variable that the pointer is pointing to.

Page 8: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

Pointer in Details

POINTERCONSTANTS

POINTER VALUES

POINTER VARIABLES

POINTERS

Page 9: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

POINTER CONSTANTS:

-Address with in computer are refers to the to as pointer constants use to store data values

POINTER VALUES:

-Address store in a pointer variable is refined as pointer value ( Address of variable )

POINTER VARIABLE:

Variable that contains a pointer value is called a Pointer variable

Page 10: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

ACCESSING A VARIABLE THROUGH ITS POINTER

We access the value of the variable by help of ‘pointer’ this is done by using another unary operator * (asterisk) usually known as “ Indirection operator “ consider following statements int quantity, *p ,n; ( first line ) quantity = 179; (second line) p = & quantity; (third line) n = *p; (fourth line)

Page 11: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

EXPLANATION:

The First line declares ‘quantity’ & ‘n’ as integer variable and ‘p’ as pointer variable

The second line Assigns value 179 to variable quantity

The third line assigns address of variable quantity to the pointer variable ‘p’

the forth line contains ‘ * ’ operator in this case *p returns value of variable quantity Now value of ‘n’ would be 179

the Two statements p = & quantity; n = * p; Are Equivalent to n = * & quantity;

which in turns is equivalent to

n = quantity;

Page 12: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

POINTER DECLARATION

In ‘c’ Every Variable must be Declared its type ,since pointer variables contain address of separate Data type , They must be Declared before use them the declaration of pointer variable takes the following form

syntax:

data _type * pt_ name

EXAMPLE:

1. Int * p ; / * integer pointer */ & 2. float *p; /* float pointer */

HERE: 1. Declares the variable ‘p’ as a pointer variable that points to an integer data

type

2. Declares the variable ‘p’ as a pointer variable that points to an ‘float’ data type

Page 13: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

POINTER DECLARATION STYLES int* p ; // * style 1*// int *p ; //* style 2*// int * p ; //*style 3*//

However ,the style 2 is more popular due to following reasons;

1. This style is convenient to have multiple declaration in the same statement

Ex: int *p, x,*q ; 2. This style matches with the format used for accessing the target

values. Ex: int x, *p , y; x=10; y=*p; *p = 20;

Page 14: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

PROGRAM EXAMPLE

# include < stdio.h>

void main ()

{

int ivar = 486;

int * iptr ;

iptr = & ivar;

Printf(“ Address of ivar = %d\n”, iptr );

Printf (“ The content of ivar = %d\n”, * iptr);

}

OUT PUT:

Address of ivar = 4056

The content of ivar = 486

Page 15: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

POINTER INITIALIZATION

As ordinary variables are Initialized with in Declaration part of the program ,

The Pointer variables can initialized by Accessing address of the variable that are

Used in the program .

Syntax :Data_type *ptr = expression

Where:

data_type = Any Basic Data type

*ptr = pointer variable

expression = another variable or may be constant

Page 16: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

Example for pointer initialization

Int quantity ;

Int *p ; // * Declaration *//

p = & quantity ; // * initialization*//

Here;

the third line assign the address of ‘quantity’ to pointer variable ‘p’

Page 17: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

Invalid initializations

1. float a , b ; int x , * p ; p = & a ; // * wrong *//

b = * p ; Here; we will get erroneous output because we are trying to assign the

address of float variable to an integer variable it is not allowed in pointer assignments.

2. Int * p = & x , x ; // * wrong * //

Here;

it declares ‘ x ’ as integer variable, ‘p’ as pointer variable then assign ‘ p’ to the address of ‘x’ . first we must be declare the ‘x’ before initialization hence above statement gives the erroneous output

Page 18: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

PROGRAM EXAMPLE FOR POINTER INITIALIZATION

# include < stdio.h>

Void main ()

int m,*ptr ;

m = 270;

ptr = & m;

printf (“ The content of variable ‘m’ = %d\n”,*ptr);

*ptr= 434 ; /* pointer initialization*/

Printf(“ the content of the variable ‘m’ after initialization = %d\n”, *ptr);

0UT PUT:

The content of the variable ‘m’ = 270

The content of the variable ’m’ after initialization = 434

Page 19: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

#include <iostream>

Now that we know that the name of an array holds the address of the first member of the array, we realize that we can declare a pointer of the same data type as the array and initialize it with the array. Here is an example:int number[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };int *pNumbers = Number;

After this declaration and initialization, Number and pNumbers have the same value:

int main() { int number[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int *pNumbers = Number;

cout << "Addresses"; cout << "\n Number : " << Number;

cout << "\npNumbers : " << pNumbers;

return 0; }

This would produce:AddressesNumber : 1245020pNumbers : 1245020

RELATING A POINTER TO AN ARRAY

Page 20: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

As you may know, we can declare the form of a block of data containing different data types by means of a structure declaration. For example, a personnel file might contain structures which look something like:

struct tag { char lname[20]; /* last name */ char fname[20]; /* first name */ int age; /* age */ float rate; /* e.g. 12.75 per hour */ };

POINTERS AND STRUCTURE

Page 21: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

#include <stdio.h>#include <string.h>struct tag{ /* the structure type */ char lname[20]; /* last name */ char fname[20]; /* first name */ int age; /* age */ float rate; /* e.g. 12.75 per hour */};

struct tag my_struct; /* define the structure */void show_name(struct tag *p); /* function prototype */int main(void){ struct tag *st_ptr; /* a pointer to a structure */ st_ptr = &my_struct; /* point the pointer to my_struct */ strcpy(my_struct.lname,"Jensen"); strcpy(my_struct.fname,"Ted"); printf("%s ",my_struct.fname);

PROGRAM

Page 22: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.

void show_name(struct tag *p){ printf("%s ", p->fname); /* p points to a structure */ printf("%s ", p->lname); printf("%d", p->age);}

printf("%s",my_struct.lname); my_struct.age = 63; show_name(st_ptr); /* pass the pointer */ return 0;}

Page 23: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
Page 24: Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.