logging in or signing up POinters in C jvgorabal Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 5844 Category: Education License: All Rights Reserved Like it (3) Dislike it (3) Added: November 05, 2008 This Presentation is Public Favorites: 2 Presentation Description Basics of Pointers Comments Posting comment... By: Rohit05 (23 month(s) ago) nice Saving..... Post Reply Close Saving..... Edit Comment Close Premium member Presentation Transcript Slide 1: 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT DATA STRUCTURES Slide 2: 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT UNIT 1 POINTERS A tour of Knowledge POINTER : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT POINTER DEFINATION: 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. Slide 4: 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT EXAMPLE FOR POINTER: Int X = 547; 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 Pointers are used in following variables : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT Pointers are used in following variables Variables of any basic data type An Array Functions Structures, and Unions Advantages of Pointers : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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 Operators used with Pointers : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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. Pointer in Details : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT Pointer in Details POINTER CONSTANTS POINTER VALUES POINTER VARIABLES POINTERS Slide 9: 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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 ACCESSING A VARIABLE THROUGH ITS POINTER : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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) EXPLANATION: : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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; POINTER DECLARATION : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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: 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 POINTER DECLARATION STYLES : 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; PROGRAM EXAMPLE : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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 POINTER INITIALIZATION : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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 Example for pointer initialization : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT Int quantity ; Int *p ; // * Declaration *// p = & quantity ; // * initialization*// Here; the third line assign the address of ‘quantity’ to pointer variable ‘p’ Example for pointer initialization Invalid initializations : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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. 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 PROGRAM EXAMPLE FOR POINTER INITIALIZATION : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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 Slide 19: 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT Compiled By ANAND. 3rd Sem ISE Slide 20: 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT Last Word You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
POinters in C jvgorabal Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 5844 Category: Education License: All Rights Reserved Like it (3) Dislike it (3) Added: November 05, 2008 This Presentation is Public Favorites: 2 Presentation Description Basics of Pointers Comments Posting comment... By: Rohit05 (23 month(s) ago) nice Saving..... Post Reply Close Saving..... Edit Comment Close Premium member Presentation Transcript Slide 1: 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT DATA STRUCTURES Slide 2: 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT UNIT 1 POINTERS A tour of Knowledge POINTER : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT POINTER DEFINATION: 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. Slide 4: 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT EXAMPLE FOR POINTER: Int X = 547; 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 Pointers are used in following variables : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT Pointers are used in following variables Variables of any basic data type An Array Functions Structures, and Unions Advantages of Pointers : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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 Operators used with Pointers : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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. Pointer in Details : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT Pointer in Details POINTER CONSTANTS POINTER VALUES POINTER VARIABLES POINTERS Slide 9: 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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 ACCESSING A VARIABLE THROUGH ITS POINTER : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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) EXPLANATION: : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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; POINTER DECLARATION : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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: 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 POINTER DECLARATION STYLES : 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; PROGRAM EXAMPLE : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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 POINTER INITIALIZATION : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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 Example for pointer initialization : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT Int quantity ; Int *p ; // * Declaration *// p = & quantity ; // * initialization*// Here; the third line assign the address of ‘quantity’ to pointer variable ‘p’ Example for pointer initialization Invalid initializations : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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. 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 PROGRAM EXAMPLE FOR POINTER INITIALIZATION : 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT 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 Slide 19: 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT Compiled By ANAND. 3rd Sem ISE Slide 20: 11/5/2008 Compiled by ANANDPRABHU III ISE RLJIT Last Word