Presentation is loading. Please wait.

Presentation is loading. Please wait.

Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.

Similar presentations


Presentation on theme: "Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND."— Presentation transcript:

1 Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND

2 Concepts of Pointers 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.

3 EXAMPLE FOR POINTER: int X = 547; int *ptr; Ptr=&X; HERE: Veriable nameContentsLocation 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

4 Pointers are used in following variables Variables of any basic data type An Array Functions Structures, and Unions

5 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

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

7 Pointer in Details POINTER CONSTANTS POINTER VALUES POINTER VARIABLES POINTERS

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

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

10 EXPLANATION: 1. The First line declares ‘quantity’ & ‘n’ as integer variable and ‘p’ as pointer variable 2.The second line Assigns value 179 to variable quantity 3.The third line assigns address of variable quantity to the pointer variable ‘p’ 4.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;

11 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

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

13 PROGRAM EXAMPLE # include 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

14 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

15 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’

16 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

17 PROGRAM EXAMPLE FOR POINTER INITIALIZATION # include 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

18 #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: Addresses Number : 1245020 pNumbers : 1245020 RELATING A POINTER TO AN ARRAY

19 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

20 #include 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

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


Download ppt "Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND."

Similar presentations


Ads by Google