Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers and pointer applications

Similar presentations


Presentation on theme: "Pointers and pointer applications"— Presentation transcript:

1 Pointers and pointer applications

2 Introduction Every computer has addressible memory locations.
We use variable names to locate data. i.e We assign identifiers to data and then use them to manipulate the data. Uses of pointers Efficient method of accessing data Efficient techniques for manipulating data in arrays Used in functions for passing parameters Basis for dynamic allocation of memory

3 Pointer definition Pointer is a derived data type.i.e it is built from standard type. Its value is any of the address available in computer for storing and accessing data. A Pointer is a constant or variable that contains an address that can be used to access data.

4 Pointer Constants Pointer constants , drawn from the set of addresses for a computer,exist by themselves.We cannot change them;we can only use them.

5 Character Constants and variables
Char constants \n . ‘a’ ‘b’ ‘A’ ‘G’ Address 145600 value G aChar Variable name variable

6 Pointer Constants memory Char constants 00000 . \n 145595 . Address
145600 145603 Char constants \n . ‘a’ ‘b’ ‘A’ ‘G’ Address 145600 value G aChar Variable name variable Pointer constants

7 In the above picture address of aChar is drawn from the set of pointerconstants.
Like character constants pointerconstants cannot be changed Although the addresses within a computer cannot change the address of a variable can change from one run of our program to another Even though the addresses are constant we cannot know what they will be. It is still necessary to refer to them symbolically

8 Pointer Values Pointer constant is an address in memory
Saving this address is achieved using & operator. & variable_name. Example program in C to print address of variables A variables address is first byte occupied by the variable

9 Pointer Variables We can store the address of a variable into another variable called a pointer variable. Physical representation:Shows how the data and pointer variables exist in memory Logical representation :Shows relationship between data and pointer variable without physical details. We can have multiple pointers to a single variable. If we have a pointer variable and we do not want it to point to any location then we assign NULL value to it. It is defined in stdio.h file.

10 Accessing variables through pointers
If ‘p’ is a pointer variable to a data variable ‘a’ then a can be accessed through the p as *p * is indirection operator Suppose u want a=a+1 u can achieve this using pointer as *p=*p+1; or (*p)++; *&x=x (& and *inverse of each other)

11 Pointer declaration and definition
To declare a pointer also we use * but here this is not an indirection operator instead a syntactic notation for the compiler. In declaration* is associated with a type. int *p; int *q;(Declaration) Sum=*p+*q; (redirection)

12 Initialization of pointer variables
C does not initialize variables So when we start program all uninitialized variables have garbage values in them and this is for pointers too. This causes run time error. So always assign a valid memory address to the pointer Ex: int a; int *p=&a;

13 Some pointer example programs
Adding 2 numbers using pointers. Pointer flexibility demo(same pointer to print 3 different variables) Multiple pointers to one variable.

14 Pointers for inter-function communication
Passing address- swapping example Every time we want a called function to have access to a variable in the calling function,we pass the address of that variable to the called function and use the indirection operator to access it. Functions returning pointers—smallest example

15 When we return a pointer, it must point to data in calling function or a higher level function.
It is a serious error to return a pointer to a local variable in the called function Pointer to pointers

16 Compatibility Pointer size compatibility
The size of all pointers is the same Every pointer variable holds the address of one memory location in computer. Size of referenced variable may be different and is dependent on data type. Dereference type compatibility Though all pointers have same size you cannot assign pointer of one type to other type.

17 Pointer to void A pointer to void is a generic type and that is not associated with the reference type. A pointer of any reference type can be assigned to a void type A void pointer can be assigned to a pointer of any reference type. There is however one restriction i.e since a void pointer has no object type ,it cannot be dereferenced unless it is cast.

18 void * ptr; A null pointer is a pointer of any type that is assigned a constant NULL.Here reference type do not change with null assignment. Void pointer is a pointer with no reference type

19 Casting pointers int a; char *pc; pc=(char*)&a; is valid but dangerous
void *pv; char *pc; int *pi; pv=pc; pi=pv; pi=(int *)pc; all are valid but again dangerous. To dereference a void pointer we can use casting.Because this provides a type.

20 Dereference Level compatibility
int a; int *p; int **q; p=&a; is O.K. but q=&a not valid. More examples on pointers Writing a functionto convert seconds to hours,minutes and seconds. Solving quadratic equation using 3 functions namely getdata,quadratic,printresults.

21 Lvalue and Rvalue In C an expression is either an lvalue or an rvalue
Every expression has a value It can be used in 2 different ways Lvalue: An lvalue expression must be used whenever the object is receiving a value; i.e it is being modified An rvalue expression to be used to supply a value for further use;that is, to examine or copy its value.

22 Expression Type Comments identifier Variable identifier Expression[..] Array indexing (expression) Expression must already be l value *expression Dereferenced expression Expression.name Structure selection Expression->name Structure indirect selection functioncall If function uses return by address

23 A= .. a[5]= .. (a)=.. *p=.. Not these 5 a+2 a*6 a[3]+2 a++ Note that even an expression is lvalue, if it is the part of some bigger expression in which the operators create only rvalue expression, then the whole expression is rvalue. Ex: a++ a[3]+2

24 Why study these Some operators require lvalue expression as its operand and if we use rvalue instead it gives compiler error. Operators requiring lvalue as operands are &score x ++ x-- ++x --y x=1 y+=3 Invalid expressions A+2=9 &(a+3) &4 (a+2)++ ++(a+2) A variable name is both rvalue and lvalue in assignment EX: a=b;


Download ppt "Pointers and pointer applications"

Similar presentations


Ads by Google