advertisement

POinters in C

67 %
33 %
advertisement
Information about POinters in C
Education

Published on November 5, 2008

Author: jvgorabal

Source: authorstream.com

advertisement

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

Add a comment

Related presentations

Related pages

Pointers in C - Tutorialspoint

Pointers in C are easy and fun to learn. Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory ...
Read more

C-Programmierung: Zeiger – Wikibooks, Sammlung freier Lehr ...

C-Programmierung: Zeiger. Aus Wikibooks. Wechseln zu: Navigation, Suche. Eine Variable wurde bisher immer direkt über ihren Namen angesprochen.
Read more

A TUTORIAL ON POINTERS AND ARRAYS IN C

A tutorial on pointers and arrays in the C programming language by Ted Jensen.
Read more

Everything you need to know about pointers in C - boredzo.org

Assignment and pointers. Now, how do you assign an int to this pointer? This solution might be obvious: foo_ptr = 42; It is also wrong. Any direct ...
Read more

Zeiger in C - C-HowTo - Programmieren in C

Zeiger in C. Stelle dir vor, dass du mit Begeisterung Klavier spielst. Mit der Zeit bemerkst du, wie sich die Töne immer schiefer anhören.
Read more

Pointer (computer programming) - Wikipedia

C pointers. The basic syntax to define a pointer is: int * ptr; ... Dereferencing a null pointer in C produces undefined behavior, ...
Read more

Pointers in C - Tutorial - Cprogramming.com

Learn what pointers are and how to use pointers in C to work with memory, with beginner-friendly examples.
Read more

Pointer Pointer by @StudioMoniker

Please move your cursor. Do you like it? Follow @studiomoniker. Sorry, I don't support Internet Explorer - please use Firefox or Chrome.
Read more

dict.cc | pointers | Wörterbuch Englisch-Deutsch

Übersetzung für pointers im Englisch-Deutsch-Wörterbuch dict.cc.
Read more

Pointers in C/C++ - cplusplus.com

Pointers In earlier chapters, variables have been explained as locations in the computer's memory which can be accessed by their identifier (their name).
Read more