46630497 fun-pointer-1


Published on

pointer concept

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

46630497 fun-pointer-1

  1. 1. FUNCTION?? y All languages have a construct to separate and package blocks of code. C uses the "function" to package blocks of code. y A function is self-contained block of statements that perform a coherent task of some kind.
  2. 2. Why Use Functions? y Writing functions avoids rewriting the same code over and over. y By using functions it becomes easier to write programs and keep track of what they are doing(separate the code into modular units).
  3. 3. LOOK LIKE?? y A function has a name, a list of arguments which it takes when called, and the block of code it executes when called. y #include<stdio.h> void message(); /*function prototype declaration*/ void main() { message(); /*function call*/ printf(Dzn Friendsǥǥdz); }
  4. 4. void message() /* function definition */ { printf(Dzn Hiǥǥǥ.dz); } It is necessary to mention the prototype of every function that we intend to define in the program. A function can be called from another function, Ǯbut a function cannot be defined in another functionǯ.
  5. 5. Communication?? y Between calling and called functions y Mechanism used to convey information to the function is the Ǯargumentǯ(parameter). y Parameter: y External value used for processing is called a parameter. y Parameters are of two types: i. Actual parameters ii. Formal parameters y Parameters can passed in two ways: i. Pass by value ii. Pass by reference
  6. 6. Types of parameters͙ Actual parameters: The expression passed to a function by its caller is called the actual parametersdz Formal parameters: The parameter storage local to the function is called the formal parameter
  7. 7. Calling Convention͙ y Indicates two things: i. The order in which the arguments are passed to the function ii. Which function(calling or called)performs the cleanup of variables when the control returns from the function y When a function call is encountered and the arguments are to be passed to a function two possibilities exist: i. Arguments can be passed from left to right ii. Arguments can be passed from right to left ‡ Most common calling convention is Dzstandard calling conventiondz
  8. 8. Additional features͙ a) Return type of function b) Calling functions by value or by reference c) Recursion
  9. 9. Note: y Always ǮCǯ uses positional correspondence in functions while passing parameters y Example: y void f(int a, int b, int c) { printf(%d %d %ddz,a,b,c); } z=x+y 7 main() y=z+5 12 { x+y 15 int x=3,y=4,z=5; f(x+y,y=z+5,z=x+y); }
  10. 10. Pass by Value͙ y We pass Ǯvaluesǯ of variables to the Ǯcalledǯ function y C passes parameters by value which means that the actual parameter values are copied into local storage. y The caller and callee functions do not share any memory(-- they each have their own copy) y Examples: y sum=area(a,b); y f=fact(a); Practice: Consider the following C function int a=3; void f(int a) { void g(); a=a+2; printf(Dz%ddz,a); g(); }
  11. 11. void g() { a=a+2; printf(Dz%ddz,a); } main() { f(a); printf(Dz%ddz,a); } Ans:5 5 5
  12. 12. NOTE͙ y DzPass by Valuedz is more secure than DzPass by Referencedz because actual data is not affected and a copy of it is maintained. y Ex: void f(int i) { i=i+1; } main() { int i=5; f(i); printf(Dz%ddz,i); } Output: 5 Explanation: Here value of i passed as pass by value. So value of i local to main is not changed.
  13. 13. Disadvantage!!! This scheme is fine for many purposes, but it has two disadvantages: Because the callee has its own copy, modifications to that memory are not communicated back to the caller. Therefore, value parameters do not allow the callee to communicate back to the caller. Sometimes it is undesirable to copy the value from the caller to the callee because the value is large and so copying it is expensive
  14. 14. Solution͙͙. The alternative is to pass the arguments by reference. Instead of passing a copy of a value from the caller to the callee, pass a pointer to the value. In this way there is only one copy of the value at any time, and the caller and callee both access that one value through pointers.
  15. 15. POINTERS??? Def.: A variable which stores the address of other variable. Represented by * Also called Indirection operator There are two pointers based on memory: Near pointer(occupies 2bytes) Far pointer(occupies 4bytes) Pointer declaration is as , i location name 6 value at location 100 location number(address)
  16. 16. Types of pointers͙ y Single pointer(*) Ex: int x=5; int *ptr; ptr=x; y Double pointer(**) Ex: int **pptr; pptr=ptr; y Triple pointer(***)ǥǥǥ y N pointer(*ǥǥǥ*) y NULL pointer: A pointer which holds null value Ex: int *ptr=NULL; ‡ WILD pointer: An uninitialized pointer Ex: int *ptr; char *p; float *f;
  17. 17. DANGLING pointer: A pointer that points to an address that doesnǯt exist. Ex: n1 n2 n3 nodes 200 300 NULL 100 200 300 If we delete 200 ptr, then n1 n3 200 NULL 100 300
  18. 18. CONSTANT pointer: int *const ptr=variable; FUNCTIONAL pointer: int f; variable int f(); function int *f; pointer int *f(); function int (*f)(); functional pointer
  19. 19. SPECIALIZED pointers: int *p; float *fp; char *cp; long *lp; GENERIC pointer: (void *) void *vp; NOTE::The arithmetic operations that are allowed on pointers are +,-
  20. 20. Example: void main() { int i=5,*j,**k; j=I; k=j; printf(Dzi=%udz,i); printf(Dzi=%udz,j); printf(Dzi=%udz,*k); printf(Dzj=%udz,j); printf(Dzj=Dz%udz,k); printf(Dzk=%udz,k); printf(Dzi=%ddz,i); printf(Dzi=%ddz,*(i)); printf(Dzi=%ddz,**k); }
  21. 21. y Output: i=1000 i=1000 i=1000 j=2000 j=2000 k=3000 i=5 i=5 i=5
  22. 22. Pass by Reference͙ y Here we pass the location number(also called address) of the variable to a function. y Example: y void swap(int*,int*); //prototype y Swap(a,b); //calling function y Void swap(int *x,int *y){} //definition
  23. 23. Practice: int x; void f(int *x); { void f2(int); *x=*x*2; f2(*x); } void f2(int x) { x=x*2; printf(Dz%ddz,x); } main() { x=3; f(x); printf(Dz%ddz,x); } Output::6 12 6
  24. 24. Recursion?... A function is said to be Ǯrecursiveǯ if a statement within the body of a function calls the same function (OR) Recursion is the process of defining something in terms of itself Recursion internally uses stack
  25. 25. Example: Q::What is the value of f(5)? int f(int n) { static int r=0; if(n=0) return 1; if(n3) { r=n; return(f(n-1)+2); } return f(n-1)+r; } Output:: 18
  26. 26. f(int n) { 0 return 1; } 1 f(n) { r=5; return f(n-1)+r; //1+5=6 } 2 f(n) { r=5; return f(n-1)+r; //6+5=11 } 3 f(n) { r=5; return f(n-1)+r; //11+5=16 } 5 STACK f(n) { r=5; return(f(n-2)+2); //16+2=18 } f(n) 5 f(n) 3 f(n) 2 f(n) 1 f(n) 0
  27. 27. Test ur skills!!! void f(int i) { if(i) { printf(Dz%ddz,i); f(i+1); } } main() { f(10); }
  28. 28. Output:: 10,11,12,13,ǥǥǥǥ,32767,-32767,ǥǥǥ.-1 Explanation:: when the value of i becomes Ǯ0ǯ that is condition is false and execution stops.
  29. 29. #definestdio.h #define print(x) printf(Dz%ddz,x); int x; void Q(int z) { z+=x; print(z); } void P(int *y) { int x=*y+2; Q(x); *y=x-1; print(x); } main() { int x=5; P(x); print(x); } Output:: 12 7 6
  30. 30. #includestdio.h int fun(int n, int *f_p) { int t, f; if(n=1) { *f_p=1; return 1; } t=fun(n-1,f_p); f=t+*f_p; *f_p=t; return f; } int main() { int x=15; printf(Dz%dndz,fun(5,x)); return 0; } The value printed is??? Output:: 8
  31. 31. main() { int *ptr, i; int A[]={10,20,30,40,50}; ptr=A+4; for(i=1;i5;i++) { *ptr=*(ptr-1); //*ptr=*ptr--; *ptr=*ptr-1; //*ptr=*ptr-1; } for(i=0;i5;i++) { printf(Dz%ddz,A[i]); } } Output is????? 10 20 30 40 39
  32. 32. Output of the following C program isǥǥǥǥǥǥ? main() { char s[]={'a','b','c','n','c','0'}; char *p,*str,*str1; p=s[3]; str=p; str1=s; printf(%d,++*p + ++*str1-32); } NOTE:ASCII value of n(10),0(0) Output:: 77(ǮMǯ)
  33. 33. main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=a[2][2][2]; *q=***a; printf(%d----%d,*p,*q); }
  34. 34. y Output: some garbage valueǥǥ y Explanation:p=a[2][2][2] you declared only two 2D arrays, but you are trying to access the third 2D(which you are not declared) it will print garbage values. *q=***a ǥstarting address of Ǯaǯ is assigned to integer pointer. Now q is pointing to starting address of a. If you print *q, it will print first element of 3D array..
  35. 35. Output isǥǥ??? main() { printf(nab); printf(bsi); printf(rha); } Note::r-linefeedǥb-backspace
  36. 36. Output isǥǥǥ.? main() { printf(%p,main); } Ans :Some address will be printed. Explanation:Function names are just addresses (just like array names are addresses). main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They are printed as hexadecimal numbers..
  37. 37. main() { static char names[5][20]={pascal,ada,cobol,fortran,perl}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i=4;i++) printf(%s,names[i]); } Ans ::Compiler error: Lvalue required in function main Explanation::Array names are pointer constants. So it cannot be modified..
  38. 38. Output for the following program is?????? main() { int i=-1; +i; printf(i = %d, +i = %d n,i,+i); }
  39. 39. Ans : i = -1, +i = -1 Explanation:Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it just because it has no effect in the expressions (hence the name dummy operator)..
  40. 40. main() { char name[10],s[12]; scanf(Dz %[^],s); } How scanf will execute? Ans :First it checks for the leading white space and discards it. Then it matches with a quotation mark and then it reads all character upto another quotation mark..
  41. 41. Output of the following program isǥǥǥ? void main() { char *ptr=*******; int i; clrscr(); for(i=0;i8;i++) { printf(%*.*sn,8,i,ptr); } getch(); }
  42. 42. #includestdio.h #includeconio.h void main(){ char *ptr=*********; int i,j; clrscr(); for(i=0;i11;i++){ if(i5) printf(%*.*sn,5+i,2*i+1,ptr); else { if(i==7) { *(ptr+3)=' '; *(ptr+4)=' '; *(ptr+5)=' '; } printf(%*.*sn,9,9,ptr); } } getch(); }
  43. 43. y Explanation: Meaning of %*.*s in the printf function: -First * indicates the width i.e. how many spaces will take to print the string. -Second * indicates how many characters will print of any string.
  44. 44. #includestdio.h #includeconio.h int fun(int); int i; void main( ) { int j; for(;;) { if(j=fun(i)) printf(%d,j); else break; } } int fun(x) { static int v=2; v--; return (v-x); } Output:: 1
  45. 45. void main() { static char *s[3]={math,phy,che}; typedef char *( *ppp)[3]; static ppp p1=s,p2=s,p3=s; char * (*(*array[3]))[3]={p1,p2,p3}; char * (*(*(*ptr)[3]))[3]=array; p2+=1; p3+=2; printf(%s,(***ptr[0])[2]); } Output isǥǥǥ.???
  46. 46. y Ans: che y Explanation: Here ptr: is pointer to array of pointer to string. P1, p2, p3: are pointers to array of string. array[3]: is array which contain pointer to array of string. As we know p[i]=*(p+i) (***ptr[0])[2]=(*(***ptr+0))[2]=(***ptr)[2] =(***(array))[2] //ptr=array =(**array)[2] //From rule *p=p =(**(p1))[2] //array=p1 =(*p1)[2] =(*s)[2] //p1=s =s[2]=dzchedz
  47. 47. What will be output if you will compile and execute the following c code? #includeconio.hDz int display(); int(*array[3])(); int(*(*ptr)[3])(); void main() { array[0]=display; array[1]=getch; ptr=array; printf(%d,(**ptr)()); (*(*ptr+1))(); } int display() { int x=5; return x++; }
  48. 48. y Ans: 5 y Explanation: In this example: array []: It is array of pointer to such function which parameter is void and return type is int data type. ptr: It is pointer to array which contents are pointer to such function which parameter is void and return type is int type data. (**ptr)() = (** (array)) () //ptr=array = (*array) () // from rule *p=p =array [0] () //from rule *(p+i)=p[i] =display () //array[0]=display (*(*ptr+1))() =(*(*array+1))() //ptr=array =*(array+1) () // from rule *p=p =array [1] () //from rule *(p+i)=p[i] =getch () //array[1]=getch
  49. 49. Output isǥǥ..? void main() { static main; int x; x=call(main); clrscr(); printf(%d ,x); getch(); } int call(int address) { address++; return address; }
  50. 50. int dynamic(int,...); void main() { int x,y; x=dynamic(2,4,6,8,10,12,14); y=dynamic(3,6,9,12); clrscr(); printf(%d %d ,x,y); getch(); } int dynamic(int s,...) { void *ptr; ptr=...; (int *)ptr+=2; s=*(int *)ptr; return s; } Output isǥǥ? 8 12
  51. 51. y In C three continuous dots is known as ellipsis which is variable number of arguments of function. In this example ptr is generic pointer which is pointing to first element of variable number of argument. After incrementing it will point third element.
  52. 52. y What will be output if you will compile and execute the following c code? void main() { int i; float a=5.2; char *ptr; ptr=(char *)a; for(i=0;i=3;i++) printf(%d ,*ptr++); }
  53. 53. y Ans: 102 102 -90 64 y Explanation: In c float data type is four byte data type while char pointer ptr can point one byte of memory at a time. ptr pointer will point first fourth byte then third byte then second byte then first byte. Content of fourth byte: Binary value=01100110 Decimal value= 64+32+4+2=102 Content of third byte: Binary value=01100110 Decimal value=64+32+4+2=102
  54. 54. Content of second byte: Binary value=10100110 Decimal value=-128+32+4+2=-90 Content of first byte: Binary value=01000000 Decimal value=64 y Note: Character pointer treats MSB bit of each byte i.e. left most bit of above figure as sign bit.