C programming slide-6

2,054 views

Published on

Published in: Education

C programming slide-6

  1. 1. PREPARED BY:-<br /> PRADEEP DWIVEDI<br />(pursuing B.TECH-IT)<br />FROM H.C.S.T.(MATHURA)<br />Mob-+919027843806<br />E-mail-pradeep.it74@gmail.com<br />C-PROGRAMMING SLIDE-6<br />Friday, February 11, 2011<br />1<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />
  2. 2. C-6<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />2<br />TOPIC:-<br />USER DEFINED FUNCTION<br />POINTERS<br />
  3. 3. PART-6.1<br />Friday, February 11, 2011<br />3<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />
  4. 4. USER DEFINED FUNCTION<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />4<br />Every c program collection of one or more functions.<br />It consist some predefine function and some user define function.<br />main() is a user define function which is first executed.<br />
  5. 5. ELEMENTS OF A USER DEFINED FUNCTION<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />5<br />Function prototyping/declaration.<br />function definition.<br />function invocation/calling.<br />
  6. 6. FUNCTION DECLARATION/PROTOTYPING<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />6<br />Like variables , all functions in a c program must be declared, before they are invoked.<br />A function declaration (also known as function prototype) consists of four parts-<br /> function type(return type).<br />function name.<br />parameter list.<br />terminating semicolon.<br />eg:- <br />int sum(inta,int b);<br /> or int sum(int,int);<br />
  7. 7. FUNCTION DECLARATION/PROTOTYPING<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />7<br />return type(function type) specifies that what type of value do you want to return.<br />function name specifies the name of function this can be anything which do you want.<br />in parameterized function at the time of declaration we specifies the parameter in parentheses as data type of parameter. (variable is optional)<br />in declaration of a function after parentheses we must terminate by semicolon(;). <br />
  8. 8. DEFINITION OF FUNCTION<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />8<br />A function definition, also known as function implementation shall include the following elements.<br />Function name.<br />Function type.<br />List of parameter.<br />Local variable declaration.<br />Function statement.<br />A return statement. <br />
  9. 9. DEFINITION OF FUNCTION<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />9<br />Format of function definition given by a example-<br />int sum(inta,int b)<br /> {<br /> c=a+b;<br /> return c;<br /> }<br />Function_type(return_type)<br />Function name<br />Parameter list<br />Function statement<br />return statement<br />
  10. 10. FUNCTION CALLING<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />10<br />A function can be called by simply using the function name followed by the list actual parameter(if any), enclosed by parenthes.<br />eg:- y=sum(5,10);<br /><ul><li>when we call our function that time we sends the control to the body of a function.</li></li></ul><li>prog1<br />//this demo is for use defined function.<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />void message();<br />clrscr();<br />message();<br />printf("I, am in mainn");<br />getch();<br />}<br />void message()<br />{<br />printf("Hello, I am pradeepdwivedin");<br />}<br />Friday, February 11, 2011<br />11<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />function declaration<br />function calling<br />print this line<br />control comes here<br />
  11. 11. NOTE<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />12<br />We can declare and invoke a function inside another function . But we can not define the body of a function inside any other function.<br />Function execution always depends on function invocation . It never depends on the declaration or the definition.<br />main() is a predefined function which declared in c library and called by the c compiler. so there is no need to declare or call the main() function and only define the body of main() function.<br />
  12. 12. PROG2<br />//prog2<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />void italy();<br />void brazil(); <br />void argentina();<br />clrscr();<br />printf("I am in mainn");<br />italy();<br />brazil(); <br />argentina(); <br />getch();<br />}<br />void italy()<br />{<br />printf("I am in italyn");<br />}<br />void brazil()<br />{<br />printf("I am in braziln");<br />}<br />void argentina()<br />{<br />printf("I am in argentina");<br />} <br />Friday, February 11, 2011<br />13<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />Control comes here<br />Function declaration<br />Print this line<br />Function calling<br />
  13. 13. NOTE<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />14<br />When we invoke a function inside another function these always execute in stack form.<br />In other words , a function that is invoked at last would we the first one two finish.<br />
  14. 14. prog3<br />//prog- function call inside another function.<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />void italy(); <br />printf("I am in main() n");<br />italy(); <br />printf("I am finally back in main()n");<br />getch();<br />}<br />void italy()<br />{<br />void brazil(); <br />printf("I am in italyn");<br />brazil();<br />printf("I am back in italyn");<br />}<br />void brazil()<br />{<br />void argentina();<br />printf("I am in braziln");<br />argentina();<br />}<br />void argentina()<br />{<br />printf("I am in argentinan");<br />}<br />Friday, February 11, 2011<br />15<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />function declaration<br />print this line<br />function calling<br />control comes here<br />
  15. 15. return KEYWORD<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />16<br />return is a keyword with the help of that we return a value and a value is always returned where from the function being called. <br />in the case of main() function value is return to the compiler. because main() function called by the compiler.<br />
  16. 16. PART-6.2<br />Friday, February 11, 2011<br />17<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />
  17. 17. CATEGORIES OF A FUNCTION<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />18<br />On the bases of parameter function can be categories into two categories.<br />Non parameterized function are those function in which we do not pass any parameter.<br />Parameterized function are those function in which we pass a parameter.<br />
  18. 18. Prog4(based on parameterized-function)<br />//w.a.p. to add two number by using parameterized function<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />inta,b,sum;<br />intcalsum(int,int);<br />clrscr();<br />printf("Enter two values");<br />scanf("%d%d",&a,&b);<br />sum=calsum(a,b);<br />printf("n sum=%d",sum);<br />getch();<br />}<br />intcalsum(intx,int y)<br />{<br />int d;<br />d=x+y;<br />return(d);<br />}<br />Friday, February 11, 2011<br />19<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />Function calling<br />Function declaration<br />
  19. 19. TERMS RELATED TO PROGRAM<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />20<br />the declaration of variable specifies that when we call the calsum() function it take two integer value.<br /> when we call calsum(a,b); function it takes two parameter and these values are stored in variable x and y.<br />after calculating the expression d=a+b; the value of d is returned where from which the function is called and the value of d is stored in variable sum.<br />in nest statement the value of sum is printed out. <br />
  20. 20. PARAMETERIZED FUNCTION<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />21<br />to invoke(call) a parameterize function there are two ways-<br />call by vale.<br />call by reference.<br />
  21. 21. CALL BY VALUE<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />22<br />if we pass the value as a function parameter that is known as call by value.<br />in call by value if we make some changes in the called function these changes will not be appeared at the original place. (in the calling function).<br />calling function are those function those invoke any other function.(where the function is being calling).<br />called function are those function those are invoked by any other function.<br />in the call by value the variable always maintain the separate-separate copy for the calling function and called function that is why no changes are reflected takes place. <br />
  22. 22. prog5<br />//prog to swap the variable (using call by value).<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />int a=10;<br />int b=20;<br />clrscr();<br />void swapv(int,int);<br />swapv(a,b);<br />printf("a=%dn",a);<br />printf("b=%dn",b);<br />getch();<br />}<br />void swapv(intx,int y)<br />{<br />int t;<br />t=x;<br />x=y;<br />y=t;<br />printf("x=%dn",x);<br />printf("y=%dn",y);<br />} <br />Friday, February 11, 2011<br />23<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />function declaration<br />calling function <br />and pass (a,b)<br />means(10,20)<br />control comes here<br />with x=10<br />y=20<br />control comes here<br />print <br />x=20<br />y=10<br />print <br />a=10<br />b=20<br />
  23. 23. CALL BY REFERENCE<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />24<br /> when we pass address as a function parameter this is called call by reference.<br />in call by reference, if we make some changes into the called function, these changes will also be appeared in the calling function. because they don’t maintain the separate copy of the variables.<br />CALL BY REFERENCE DISCUSSED IN DETAIL AFTER STUDYING THE POINTERS<br />
  24. 24. PART-6.3<br />Friday, February 11, 2011<br />25<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />
  25. 25. POINTER<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />26<br />A pointer is a derived data type in c. it is built from one of the fundamental data types available in c.<br />Pointer variable is a special variable that holds the address of any other variable.<br />If we want to hold the address of any integer variable that time we need an integer pointer variable and so on.<br />A pointer variable prefix asterisk sign(*) at declaration time.<br />Eg:- int *p;<br />
  26. 26. TAKE AN EXAMPLE FOR UNDERSTANDING POINTER<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />27<br />int a=20;<br />it stores in memory as-<br /> a<br /> 5000<br /><ul><li>these address is stored as-&a=5000(address of 20</li></ul>integer variable declaration<br />variable name<br />20<br />value<br />address<br />
  27. 27. TAKE AN EXAMPLE FOR UNDERSTANDING POINTER<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />28<br />to store the address we declare a pointer variable-<br />int *p;<br /> p=&a;<br />(p is a pointer variable which holds the address)<br />*p=25;<br />(means this stores the 25 at address 5000)<br />integer pointer variable declaration <br />p=5000<br />value at address<br />
  28. 28. POINTER DECLARATION STYLE<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />29<br />pointer variables are declared similarly as normal variables except for the addition of unary * operator.<br />this symbol can appear anywhere between the type name and the pointer variable name.<br />programmers use the following styles-<br />int* p;<br />int *p;<br />int * p;<br />style 1<br />style 2(generally used)<br />style 3<br />
  29. 29. prog6<br />//Demo for pointer<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />inti=3,*x;<br />float j=1.5,*y;<br />char k='c',*z;<br />printf("the value of i=%dn",i);<br />printf("the value of j=%fn",j);<br />printf("the value of k=%cn",k);<br />x=&i;<br />y=&j;<br />z=&k;<br />printf("original value of x=%dn",x);<br />printf("original value of y=%dn",y);<br />printf("original value of z=%dn",z);<br />x++;<br />y++;<br />z++;<br />printf("new value of x=%dn",x);<br />printf("new value of y=%dn",y);<br />printf("new value of z=%dn",z);<br />getch();<br />}<br />Friday, February 11, 2011<br />30<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />
  30. 30. EXPLAINATION<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />31<br />step1 I j k x y z<br /> 10 20 30 40 50 60<br />step2<br /> 10 20 30 40 50 60<br />step3<br /> 10 20 30 40 50 60 <br />3<br />1.5<br />c<br />variable<br />value<br />address(suppose)<br />3<br />1.5<br />c<br />10<br />20<br />30<br />3<br />1.5<br />c<br />12<br />24<br />31<br />
  31. 31. POINTER INCREMENT AND SCALE FACTOR<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />32<br />when we increment a pointer, its value is increased by the length of the data type. this length is called scale factors.<br />eg:- let p1 is an integer pointer with an initial value says- 2800,<br />then after operation p1=p1+1; the value of it will be 2802 , and not 2801.<br />the length of various data type are as follows-<br />character 1 byte<br />integer 2 bytes<br />float 4 bytes<br />long integer 4 bytes<br />double 8 bytes<br />
  32. 32. prog7<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />33<br />//This demo is for pointer increment and scale factor<br />void main()<br />{<br />inti=4,*j,*k;<br />clrscr();<br />j=&i;<br />printf("The value of j:%dn",j);<br />j=j+1;<br />printf("The value of j:%dn",j);<br />k=j+3;<br />printf("The value of k:%dn",k);<br />getch();<br />}<br />Suppose &i=40<br />Address of I is assign to J<br />Address of i(value of j) is printed<br />Vlue of j is increased by 1(40+2=42) <br />Vlue of j is increased by 3(42+6=48) <br />
  33. 33. POINTER EXPRESSION<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />34<br />int *p1,*p2;<br />y=*p1 * *p2;=(*p1)*(*p2) <br />z=*p1/*p2 <br />we may not use pointers in division or multiplication.<br />p1/p2<br />p1*p2<br />p1/3<br />valid<br />(because these are the value at address)<br />allowed<br />c allows us to add integers to or subtract integers from pointers, as well as to subtract one pointer from another.<br />p1+4;<br />p2-2; <br />p1-p2; <br />invalid<br />(because these are the address)<br />
  34. 34. RULES FOR POINTER OPERATION<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />35<br />a pointer variable can be assigned the address of another variable.<br />a pointer variable can be assigned the value of another pointer variable.<br />a pointer variable can be assigned with zero or NULL value.<br />a pointer variable can prefixed or post fixed with increment or decrement operator.<br />an integer value may be added or subtract a pointer variable.<br />when two pointer point to the same array, one pointer variable can be subtracted from another.<br />when two pointer points to the object of same data types, they can be compared using relational operator.<br />a pointer variable can’t be multiplied by a constant.<br />two pointer variable can’t be added<br />a value can not be assigned to an arbitary address.<br /> (i.e. & =10 is illegal)<br />
  35. 35. CHAIN OF POINTER<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />36<br />int a=10;<br /> p2 p1 a<br />p1 holds the address of integer variable<br />int *p1;<br />p1=&a;<br />p2 holds the address of integer pointer variable<br />int **p2;<br />p2=&p1;<br />value<br />address1<br />address2<br />
  36. 36. prog8<br />//prog-for chain of pointer<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />int x,*p1,**p2;<br />clrscr();<br />x=100;<br />p1=&x;<br />p2=&p1;<br />printf("Address of a=%dn",p1);<br />printf("Address of p1=%dn",p2);<br />printf("Value at address p1=%dn",**p2);<br />printf("Value at address p2=%dn",*p2);<br />getch();<br />}<br />explanation:-<br />in last statement second last statement<br />**p2=*(*p2)<br /> =*(value at address p2)<br /> =*(p1)<br /> =value at address p1<br /> =100<br />Friday, February 11, 2011<br />37<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />
  37. 37. PART-6.4<br />Friday, February 11, 2011<br />38<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />
  38. 38. POINTER vs ARRAY<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />39<br />Array is also used to hold the address but, array variable used to holds the base address of an array.<br />The term base address means address of very first element in the array.<br />Suppose we declare an array-<br />int x[5]={1,2,3,4,5};<br />
  39. 39. POINTER vs ARRAY<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />40<br /> x[0] x[1] x[2] x[3] x[4] element<br /> value<br /> 1000 1002 1004 1006 1008 address<br /> base address <br /> i.e. x=&x[0]=1000;<br />x+1=&x[1]=1002<br />x+2=&x[2]=1004<br />x+3=&x[3]=1006<br />x+4=&x[4]=1008<br />with the help of base address we can calculate the address of any element by using its index and the scale factor of the data type<br />address of x[3]=base address+(3*scale factor of int)<br />=1000+(3*2)=1006<br />
  40. 40. IMPORTANT POINT<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />41<br />An array variable is always having address of it zero index by default.<br />Array variable can hold the address of only zero index element these can not hold the address of any other element.<br />But pointer can hold the address of any element in the list.<br />An array variable is a reference variable it is little bit similar with pointer.<br />
  41. 41. prog9<br />//prog- pointer vs array<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />intarr[]={10,20,36,72,45,36};<br />int *j,*k;<br />clrscr();<br />j=&arr[4];<br />k=(arr+4);<br />if(j==k)<br />printf("Two pointers point two the same location");<br />else<br />printf("Two pointers do not point to the same location");<br />getch();<br />} <br />Friday, February 11, 2011<br />42<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />address of arr[4]=&arr[4]<br />(arr+4) arr[0]+4=&arr[4]<br />true<br />
  42. 42. prog10<br />//wap to sibtract a pointer from anoter pointer<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />intarr[]={10,20,30,40,60,57,56};<br />int *i,*j;<br />clrscr();<br />i=&arr[1];<br />j=&arr[5];<br />printf("value of i:%dn",i);<br />printf("value of j:%dn",j);<br />printf("The value at address i:%dn",*i);<br />printf("The value at address j:%dn",*j);<br />printf("The difference between address: %dn",j-i);<br />printf("The difference between values is %dn",*j-*i);<br />getch();<br />}<br />Friday, February 11, 2011<br />43<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />I THINK THERE IS NO NEED TO EXPLIAN THIS PROGRAM<br />
  43. 43. REMOVE CONFUSION<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />44<br />I think pointer is very confusing topic in “C”<br />Here we try to remove confusion –<br />Suppose - int *p;<br />Pointer variable declared with (*) sign such as (*p) but address is stored in p not *p<br />*p is always store the value at address<br />Pointer variable declaration<br />
  44. 44. POINTER AND CHARACTER STRINGS<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />45<br />The character arrays are declared and initialized as follows:-<br /> char str[5]=“good”;<br />The compiler automatically inserts the null character ‘0’ at the end of the string.<br />C supports an alternative method to create string using pointer variable of type char.<br />eg: char *str=“good”;<br /><ul><li>Remember, although str is a pointer to the string, it is also the name of string .therefore, we don’t need to use indirection operator(*) here. </li></li></ul><li>NOTE<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />46<br />When we want to print the string, that time we use the format string (%s).<br />and if we want to print the address of string that time we use format string (%d) or (%u)<br />
  45. 45. PROG11<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />47<br />//PROG POINTER VS CHARACTER ARRAY<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />char arr[]="PRADEEP DWIVEDI";<br />char *s="HINDUSTAN COLLEGE OF SCIENCE AND TECHNOLOGY";<br />clrscr();<br />printf("%sn",arr);<br />printf("%sn",s);<br />printf("%d %d",arr,s);<br />getch();<br />}<br />PRINT STRING<br />PRINT ADDRESSES<br />
  46. 46. SIZE OF OPERATOR<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />48<br />With the help of sizeof operator we can find out the size of value or variable that is occupied in the memory.<br />Note:-<br />each and every pointer variable always occupied two bytes in memory either it is float or char or int or etc.<br />
  47. 47. prog12.c<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />49<br />//PROG- DEMO FOR SIZE OF OPERATOR.<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />char arr[10]="HINDUSTAN";<br />char *s="HINDUSTAN";<br />float *p;<br />clrscr();<br />s++;<br />//arr++;<br />printf("%sn",arr);<br />printf("%sn",s);<br />printf("%d %d %d",sizeof(s),sizeof(arr),sizeof(p));<br />getch();<br />}<br />print HINDUSTAN<br />print INDUSTAN<br />BECAUSE ADDRESS OF S IS INCREASED BY ONE CHARACTER<br />THIS PRINT THE SIZE AS-<br />2 10 2<br />
  48. 48. CALL BY REFERENCE<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />50<br /> when we pass address as a function parameter this is called call by reference.<br />in call by reference, if we make some changes into the called function, these changes will also be appeared in the calling function. because they don’t maintain the separate copy of the variables.<br />
  49. 49. prog13<br />//PROG-WAP FOR SWAPING<br />void main()<br />{<br />int a=10;<br />int b=20;<br />void swapv(int*,int*);<br />clrscr();<br />swapv(&a,&b);<br />printf("a=%dn",a);<br />printf("b=%dn",b);<br />getch();<br />}<br />void swapv(int *x,int *y)<br />{<br />int t;<br />t=*x;<br />*x=*y;<br />*y=t;<br />printf("*x=%dn",*x);<br />printf("*y=%dn",*y);<br />getch();<br />}<br />Friday, February 11, 2011<br />51<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />WHEN WE RUN THIS PROGRAM THIS PROGRAM PRODUCE THE CHANGE IN SWAPV FUNCTION AND MAIN FUNCTION ALSO BECAUSE VALUE IS CHANGE AT ADDRESS<br />function prototyping<br />function calling<br />pass addresses<br />stores x=&a;<br />y=&b;<br />control comes here<br />print the value at addres 20 <br /> 10<br />values are also change here<br />a=20 b=10<br />
  50. 50. FUNCTION RETURNING POINTER<br />Friday, February 11, 2011<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />52<br />When we declare a function as pointer type then function return pointer type value (address ).<br />
  51. 51. prog14<br />//PROG- TO FIND THE MAXIMUM NUMBER<br />#include<stdio.h><br />#include<conio.h><br />int *larger(int*,int*);//prototype<br />void main()<br />{<br />int a=10;<br />int b=20;<br />int *p;<br />clrscr();<br />p=larger(&a,&b);//function call<br />printf("Greater number is: %d",*p);<br />getch();<br />}<br />int *larger(int *x,int *y)<br />{<br />if(*x>*y)<br />return(x);//address of a<br />else<br />return(y);//address of b<br />}<br />Friday, February 11, 2011<br />53<br />PRADEEP DWIVEDI(pur.B.TECH-IT)<br />try to under stand it self<br />

×