KEMBAR78
C programming slide-6 | PPTX
PREPARED BY:-	PRADEEP DWIVEDI(pursuing B.TECH-IT)FROM H.C.S.T.(MATHURA)Mob-+919027843806E-mail-pradeep.it74@gmail.comC-PROGRAMMING SLIDE-6Friday, February 11, 20111PRADEEP DWIVEDI(pur.B.TECH-IT)
C-6Friday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)2TOPIC:-USER DEFINED FUNCTIONPOINTERS
PART-6.1Friday, February 11, 20113PRADEEP DWIVEDI(pur.B.TECH-IT)
USER DEFINED FUNCTIONFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)4Every c program collection of one or more functions.It consist some predefine function and some user define function.main() is a user define function which is first executed.
ELEMENTS OF A USER DEFINED FUNCTIONFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)5Function prototyping/declaration.function definition.function invocation/calling.
FUNCTION DECLARATION/PROTOTYPINGFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)6Like variables , all functions in a c program must be declared, before they are invoked.A function declaration (also known as function prototype) consists of four parts-  function type(return type).function name.parameter list.terminating semicolon.eg:-   int sum(inta,int b);     or     int sum(int,int);
FUNCTION DECLARATION/PROTOTYPINGFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)7return type(function type) specifies that what type of value do you want to return.function name specifies the name of function this can be anything which do you want.in parameterized function at the time of declaration we specifies the parameter in parentheses as data type of parameter. (variable is optional)in declaration of a function after parentheses we must terminate by semicolon(;).
DEFINITION OF FUNCTIONFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)8A function definition, also known as function implementation shall include the following elements.Function name.Function type.List of parameter.Local variable declaration.Function statement.A return statement.
DEFINITION OF FUNCTIONFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)9Format of function definition given by a example-int sum(inta,int b)			{			c=a+b;			return c;			}Function_type(return_type)Function nameParameter listFunction statementreturn statement
FUNCTION CALLINGFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)10A function can be called by simply using the function name followed by the list actual parameter(if any), enclosed by parenthes.eg:- y=sum(5,10);when we call our function that time we sends the control to the body of a function.prog1//this demo is for use defined function.#include<stdio.h>#include<conio.h>void main(){void message();clrscr();message();printf("I, am in main\n");getch();}void message(){printf("Hello, I am pradeepdwivedi\n");}Friday, February 11, 201111PRADEEP DWIVEDI(pur.B.TECH-IT)function declarationfunction callingprint this linecontrol comes here
NOTEFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)12We can declare and invoke a function inside another function . But we can not define the body of a function inside any other function.Function execution always depends on function invocation . It never depends on the declaration or the definition.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.
PROG2//prog2#include<stdio.h>#include<conio.h>void main(){void italy();void brazil();         void argentina();clrscr();printf("I am in main\n");italy();brazil();		argentina();                  getch();}void italy(){printf("I am in italy\n");}void brazil(){printf("I am in brazil\n");}void argentina(){printf("I am in argentina");} Friday, February 11, 201113PRADEEP DWIVEDI(pur.B.TECH-IT)Control comes hereFunction declarationPrint this lineFunction calling
NOTEFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)14When we invoke a function inside another function these always execute in stack form.In other words , a function that is invoked at last would we the first one two finish.
prog3//prog- function call inside another function.#include<stdio.h>#include<conio.h>void main(){void italy(); printf("I am in main() \n");italy();   printf("I am finally back in main()\n");getch();}void italy(){void brazil();  printf("I am in italy\n");brazil();printf("I am back in italy\n");}void brazil(){void argentina();printf("I am in brazil\n");argentina();}void argentina(){printf("I am in argentina\n");}Friday, February 11, 201115PRADEEP DWIVEDI(pur.B.TECH-IT)function declarationprint this linefunction callingcontrol comes here
return KEYWORDFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)16return is a keyword with the help of that we return a value and a value is always returned where from the function being called. in the case of main() function value is return to the compiler. because main() function called by the compiler.
PART-6.2Friday, February 11, 201117PRADEEP DWIVEDI(pur.B.TECH-IT)
CATEGORIES OF A FUNCTIONFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)18On the bases of parameter function can be categories into two categories.Non parameterized function are those function in which we do not pass any parameter.Parameterized function are those function in which we pass a parameter.
Prog4(based on parameterized-function)//w.a.p. to add two number by using parameterized function#include<stdio.h>#include<conio.h>void main(){inta,b,sum;intcalsum(int,int);clrscr();printf("Enter two values");scanf("%d%d",&a,&b);sum=calsum(a,b);printf("\n sum=%d",sum);getch();}intcalsum(intx,int y){int d;d=x+y;return(d);}Friday, February 11, 201119PRADEEP DWIVEDI(pur.B.TECH-IT)Function callingFunction declaration
TERMS RELATED TO PROGRAMFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)20the declaration of variable specifies that when we call the calsum() function it take two integer value. when we call calsum(a,b); function it takes two parameter  and these values are stored in variable x and y.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.in nest statement the value of sum is printed out.
PARAMETERIZED FUNCTIONFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)21to invoke(call) a parameterize function there are two ways-call by vale.call by reference.
CALL BY VALUEFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)22if we pass the value as a function parameter that is known as call by value.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).calling function are those function those invoke any other function.(where the function is being calling).called function are those function those are invoked by any other function.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.
prog5//prog to swap the variable (using call by value).#include<stdio.h>#include<conio.h>void main(){int a=10;int b=20;clrscr();void swapv(int,int);swapv(a,b);printf("a=%d\n",a);printf("b=%d\n",b);getch();}void swapv(intx,int y){int t;t=x;x=y;y=t;printf("x=%d\n",x);printf("y=%d\n",y);} Friday, February 11, 201123PRADEEP DWIVEDI(pur.B.TECH-IT)function declarationcalling function and pass (a,b)means(10,20)control comes herewith x=10y=20control comes hereprint x=20y=10print a=10b=20
CALL BY REFERENCEFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)24 when we pass address as a function  parameter this is called call by reference.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.CALL BY REFERENCE DISCUSSED IN DETAIL AFTER STUDYING THE POINTERS
PART-6.3Friday, February 11, 201125PRADEEP DWIVEDI(pur.B.TECH-IT)
POINTERFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)26A pointer is a derived data type in c. it is built from one of the fundamental data types available in c.Pointer variable is a special variable that holds the address of any other variable.If we want to hold the address of any integer variable that time we need an integer pointer variable and so on.A pointer variable prefix asterisk sign(*) at declaration time.Eg:- int *p;
TAKE AN EXAMPLE FOR UNDERSTANDING POINTERFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)27int a=20;it stores in memory as-                                a                             5000these address is stored as-&a=5000(address of 20integer variable declarationvariable name20valueaddress
TAKE AN EXAMPLE FOR UNDERSTANDING POINTERFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)28to store the address we declare a pointer variable-int *p; p=&a;(p is a pointer variable which holds the address)*p=25;(means this stores the 25 at address 5000)integer pointer variable declaration p=5000value at address
POINTER DECLARATION STYLEFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)29pointer variables are declared similarly as normal variables except for the addition of unary * operator.this symbol can appear anywhere between the type name and the pointer variable name.programmers use the following styles-int*       p;int   *p;int     *     p;style 1style 2(generally used)style 3
prog6//Demo for pointer#include<stdio.h>#include<conio.h>void main(){inti=3,*x;float j=1.5,*y;char k='c',*z;printf("the value of i=%d\n",i);printf("the value of j=%f\n",j);printf("the value of k=%c\n",k);x=&i;y=&j;z=&k;printf("original value of x=%d\n",x);printf("original value of y=%d\n",y);printf("original value of z=%d\n",z);x++;y++;z++;printf("new value of x=%d\n",x);printf("new value of y=%d\n",y);printf("new value of z=%d\n",z);getch();}Friday, February 11, 201130PRADEEP DWIVEDI(pur.B.TECH-IT)
EXPLAINATIONFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)31step1	I	j	k	 x	 y	 z		10	20	30	 40	 50	 60step2		 10	20	30	40	50	60step3		10	20	30	40	50	60	31.5cvariablevalueaddress(suppose)31.5c10203031.5c122431
POINTER INCREMENT AND SCALE FACTORFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)32when we increment a pointer, its value is increased by the length of the data type. this length is called scale factors.eg:- let p1 is an integer pointer with an initial value says- 2800,then after operation p1=p1+1; the value of it will be 2802 , and not 2801.the length of various data type are as follows-character		1 byteinteger		2 bytesfloat		4 byteslong integer 	4 bytesdouble		8 bytes
prog7Friday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)33//This demo is for pointer increment and scale factorvoid main(){inti=4,*j,*k;clrscr();j=&i;printf("The value of j:%d\n",j);j=j+1;printf("The value of j:%d\n",j);k=j+3;printf("The value of k:%d\n",k);getch();}Suppose &i=40Address of I is assign to JAddress of i(value of j) is printedVlue of j is increased by 1(40+2=42) Vlue of j is increased by 3(42+6=48)
POINTER EXPRESSIONFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)34int *p1,*p2;y=*p1 * *p2;=(*p1)*(*p2) z=*p1/*p2 we may not use pointers in division or multiplication.p1/p2p1*p2p1/3valid(because these are the value at address)allowedc allows us to add integers to or subtract integers from pointers, as well as to subtract one pointer from another.p1+4;p2-2;         p1-p2; invalid(because these are the  address)
RULES FOR POINTER OPERATIONFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)35a pointer variable can be assigned the address of another variable.a pointer variable can be assigned the value of another pointer variable.a pointer variable can be assigned with zero or NULL value.a pointer variable can prefixed or post fixed with increment or decrement operator.an integer value may be added or subtract a pointer variable.when two pointer point to the same array, one pointer variable can be subtracted from another.when two pointer points to the object of same data types, they can be compared using relational operator.a pointer variable can’t be multiplied by a constant.two pointer variable can’t be addeda value can not be assigned to an arbitary address.		(i.e.   & =10   is illegal)
CHAIN OF POINTERFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)36int  a=10;              p2                   p1                 ap1 holds the address of integer variableint *p1;p1=&a;p2 holds the address of integer  pointer variableint **p2;p2=&p1;valueaddress1address2
prog8//prog-for chain of pointer#include<stdio.h>#include<conio.h>void main(){int x,*p1,**p2;clrscr();x=100;p1=&x;p2=&p1;printf("Address of a=%d\n",p1);printf("Address of p1=%d\n",p2);printf("Value at address p1=%d\n",**p2);printf("Value at address p2=%d\n",*p2);getch();}explanation:-in last statement second last statement**p2=*(*p2)        =*(value at address p2)	=*(p1)	=value at address p1	=100Friday, February 11, 201137PRADEEP DWIVEDI(pur.B.TECH-IT)
PART-6.4Friday, February 11, 201138PRADEEP DWIVEDI(pur.B.TECH-IT)
POINTER vs ARRAYFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)39Array is also used to hold the address but, array variable used to holds the base address of an array.The term base address means address of very first element in the array.Suppose we declare an array-int x[5]={1,2,3,4,5};
POINTER vs ARRAYFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)40      x[0]   x[1]     x[2]    x[3]       x[4]        element                                                              value	 1000  1002   1004  1006  1008        address		      base address				i.e.   x=&x[0]=1000;x+1=&x[1]=1002x+2=&x[2]=1004x+3=&x[3]=1006x+4=&x[4]=1008with the help of base address we can calculate the address of any element by using its index and the scale factor of the data typeaddress of x[3]=base address+(3*scale factor of int)=1000+(3*2)=1006
IMPORTANT POINTFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)41An array variable is always having address of it zero index by default.Array variable can hold the address of only zero index element these can not hold the address of any other element.But pointer can hold the address of any element in the list.An array variable is a reference variable it is little bit similar with pointer.
prog9//prog- pointer vs array#include<stdio.h>#include<conio.h>void main(){intarr[]={10,20,36,72,45,36};int *j,*k;clrscr();j=&arr[4];k=(arr+4);if(j==k)printf("Two pointers point two the same location");elseprintf("Two pointers do not point to the same location");getch();} Friday, February 11, 201142PRADEEP DWIVEDI(pur.B.TECH-IT)address of arr[4]=&arr[4](arr+4) arr[0]+4=&arr[4]true
prog10//wap to sibtract a pointer from anoter pointer#include<stdio.h>#include<conio.h>void main(){intarr[]={10,20,30,40,60,57,56};int *i,*j;clrscr();i=&arr[1];j=&arr[5];printf("value of i:%d\n",i);printf("value of j:%d\n",j);printf("The value at address i:%d\n",*i);printf("The value at address j:%d\n",*j);printf("The difference between address: %d\n",j-i);printf("The difference between values is %d\n",*j-*i);getch();}Friday, February 11, 201143PRADEEP DWIVEDI(pur.B.TECH-IT)I THINK THERE IS NO NEED TO EXPLIAN THIS PROGRAM
REMOVE CONFUSIONFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)44I think pointer is very confusing topic in “C”Here we try to remove confusion –Suppose -     int *p;Pointer variable declared with (*) sign such as (*p) but address is stored in p not *p*p is always store the value at addressPointer variable declaration
POINTER AND CHARACTER STRINGSFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)45The character arrays are declared and initialized as follows:-                   char str[5]=“good”;The compiler automatically inserts the null character ‘\0’ at the end of the string.C supports an alternative method to create string using pointer variable of type char.eg: char *str=“good”;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.	NOTEFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)46When we want to print the string, that time we use the format string (%s).and if we want to print the address of string that time we use format string (%d) or (%u)
PROG11Friday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)47//PROG POINTER VS CHARACTER ARRAY#include<stdio.h>#include<conio.h>void main(){char arr[]="PRADEEP DWIVEDI";char *s="HINDUSTAN COLLEGE OF SCIENCE AND TECHNOLOGY";clrscr();printf("%s\n",arr);printf("%s\n",s);printf("%d  %d",arr,s);getch();}PRINT STRINGPRINT ADDRESSES
SIZE OF OPERATORFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)48With the help of sizeof operator we can find out the size of value or variable that is occupied in the memory.Note:-each and every pointer variable always occupied two bytes in memory either it is float or char or int or etc.
prog12.cFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)49//PROG- DEMO FOR SIZE OF OPERATOR.#include<stdio.h>#include<conio.h>void main(){char arr[10]="HINDUSTAN";char *s="HINDUSTAN";float *p;clrscr();s++;//arr++;printf("%s\n",arr);printf("%s\n",s);printf("%d %d %d",sizeof(s),sizeof(arr),sizeof(p));getch();}print  HINDUSTANprint  INDUSTANBECAUSE ADDRESS OF S IS INCREASED BY ONE CHARACTERTHIS PRINT THE SIZE AS-2       10        2
CALL BY REFERENCEFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)50 when we pass address as a function  parameter this is called call by reference.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.
prog13//PROG-WAP FOR SWAPINGvoid main(){int a=10;int b=20;void swapv(int*,int*);clrscr();swapv(&a,&b);printf("a=%d\n",a);printf("b=%d\n",b);getch();}void swapv(int *x,int *y){int t;t=*x;*x=*y;*y=t;printf("*x=%d\n",*x);printf("*y=%d\n",*y);getch();}Friday, February 11, 201151PRADEEP DWIVEDI(pur.B.TECH-IT)WHEN WE RUN THIS PROGRAM THIS PROGRAM PRODUCE THE CHANGE IN SWAPV FUNCTION AND MAIN FUNCTION ALSO BECAUSE VALUE IS CHANGE AT ADDRESSfunction prototypingfunction  callingpass addressesstores    x=&a;y=&b;control comes hereprint the value at addres  20              10values are also change herea=20      b=10
FUNCTION RETURNING POINTERFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)52When we declare  a function as pointer type then function return pointer type value (address ).
prog14//PROG- TO FIND THE MAXIMUM NUMBER#include<stdio.h>#include<conio.h>int *larger(int*,int*);//prototypevoid main(){int a=10;int b=20;int *p;clrscr();p=larger(&a,&b);//function callprintf("Greater number is:  %d",*p);getch();}int *larger(int *x,int *y){if(*x>*y)return(x);//address of aelsereturn(y);//address of b}Friday, February 11, 201153PRADEEP DWIVEDI(pur.B.TECH-IT)try  to under stand it self
C programming slide-6
C programming slide-6

C programming slide-6

  • 1.
    PREPARED BY:- PRADEEP DWIVEDI(pursuingB.TECH-IT)FROM H.C.S.T.(MATHURA)Mob-+919027843806E-mail-pradeep.it74@gmail.comC-PROGRAMMING SLIDE-6Friday, February 11, 20111PRADEEP DWIVEDI(pur.B.TECH-IT)
  • 2.
    C-6Friday, February 11,2011PRADEEP DWIVEDI(pur.B.TECH-IT)2TOPIC:-USER DEFINED FUNCTIONPOINTERS
  • 3.
    PART-6.1Friday, February 11,20113PRADEEP DWIVEDI(pur.B.TECH-IT)
  • 4.
    USER DEFINED FUNCTIONFriday,February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)4Every c program collection of one or more functions.It consist some predefine function and some user define function.main() is a user define function which is first executed.
  • 5.
    ELEMENTS OF AUSER DEFINED FUNCTIONFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)5Function prototyping/declaration.function definition.function invocation/calling.
  • 6.
    FUNCTION DECLARATION/PROTOTYPINGFriday, February11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)6Like variables , all functions in a c program must be declared, before they are invoked.A function declaration (also known as function prototype) consists of four parts- function type(return type).function name.parameter list.terminating semicolon.eg:- int sum(inta,int b); or int sum(int,int);
  • 7.
    FUNCTION DECLARATION/PROTOTYPINGFriday, February11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)7return type(function type) specifies that what type of value do you want to return.function name specifies the name of function this can be anything which do you want.in parameterized function at the time of declaration we specifies the parameter in parentheses as data type of parameter. (variable is optional)in declaration of a function after parentheses we must terminate by semicolon(;).
  • 8.
    DEFINITION OF FUNCTIONFriday,February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)8A function definition, also known as function implementation shall include the following elements.Function name.Function type.List of parameter.Local variable declaration.Function statement.A return statement.
  • 9.
    DEFINITION OF FUNCTIONFriday,February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)9Format of function definition given by a example-int sum(inta,int b) { c=a+b; return c; }Function_type(return_type)Function nameParameter listFunction statementreturn statement
  • 10.
    FUNCTION CALLINGFriday, February11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)10A function can be called by simply using the function name followed by the list actual parameter(if any), enclosed by parenthes.eg:- y=sum(5,10);when we call our function that time we sends the control to the body of a function.prog1//this demo is for use defined function.#include<stdio.h>#include<conio.h>void main(){void message();clrscr();message();printf("I, am in main\n");getch();}void message(){printf("Hello, I am pradeepdwivedi\n");}Friday, February 11, 201111PRADEEP DWIVEDI(pur.B.TECH-IT)function declarationfunction callingprint this linecontrol comes here
  • 11.
    NOTEFriday, February 11,2011PRADEEP DWIVEDI(pur.B.TECH-IT)12We can declare and invoke a function inside another function . But we can not define the body of a function inside any other function.Function execution always depends on function invocation . It never depends on the declaration or the definition.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.
  • 12.
    PROG2//prog2#include<stdio.h>#include<conio.h>void main(){void italy();voidbrazil(); void argentina();clrscr();printf("I am in main\n");italy();brazil(); argentina(); getch();}void italy(){printf("I am in italy\n");}void brazil(){printf("I am in brazil\n");}void argentina(){printf("I am in argentina");} Friday, February 11, 201113PRADEEP DWIVEDI(pur.B.TECH-IT)Control comes hereFunction declarationPrint this lineFunction calling
  • 13.
    NOTEFriday, February 11,2011PRADEEP DWIVEDI(pur.B.TECH-IT)14When we invoke a function inside another function these always execute in stack form.In other words , a function that is invoked at last would we the first one two finish.
  • 14.
    prog3//prog- function callinside another function.#include<stdio.h>#include<conio.h>void main(){void italy(); printf("I am in main() \n");italy(); printf("I am finally back in main()\n");getch();}void italy(){void brazil(); printf("I am in italy\n");brazil();printf("I am back in italy\n");}void brazil(){void argentina();printf("I am in brazil\n");argentina();}void argentina(){printf("I am in argentina\n");}Friday, February 11, 201115PRADEEP DWIVEDI(pur.B.TECH-IT)function declarationprint this linefunction callingcontrol comes here
  • 15.
    return KEYWORDFriday, February11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)16return is a keyword with the help of that we return a value and a value is always returned where from the function being called. in the case of main() function value is return to the compiler. because main() function called by the compiler.
  • 16.
    PART-6.2Friday, February 11,201117PRADEEP DWIVEDI(pur.B.TECH-IT)
  • 17.
    CATEGORIES OF AFUNCTIONFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)18On the bases of parameter function can be categories into two categories.Non parameterized function are those function in which we do not pass any parameter.Parameterized function are those function in which we pass a parameter.
  • 18.
    Prog4(based on parameterized-function)//w.a.p.to add two number by using parameterized function#include<stdio.h>#include<conio.h>void main(){inta,b,sum;intcalsum(int,int);clrscr();printf("Enter two values");scanf("%d%d",&a,&b);sum=calsum(a,b);printf("\n sum=%d",sum);getch();}intcalsum(intx,int y){int d;d=x+y;return(d);}Friday, February 11, 201119PRADEEP DWIVEDI(pur.B.TECH-IT)Function callingFunction declaration
  • 19.
    TERMS RELATED TOPROGRAMFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)20the declaration of variable specifies that when we call the calsum() function it take two integer value. when we call calsum(a,b); function it takes two parameter and these values are stored in variable x and y.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.in nest statement the value of sum is printed out.
  • 20.
    PARAMETERIZED FUNCTIONFriday, February11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)21to invoke(call) a parameterize function there are two ways-call by vale.call by reference.
  • 21.
    CALL BY VALUEFriday,February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)22if we pass the value as a function parameter that is known as call by value.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).calling function are those function those invoke any other function.(where the function is being calling).called function are those function those are invoked by any other function.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.
  • 22.
    prog5//prog to swapthe variable (using call by value).#include<stdio.h>#include<conio.h>void main(){int a=10;int b=20;clrscr();void swapv(int,int);swapv(a,b);printf("a=%d\n",a);printf("b=%d\n",b);getch();}void swapv(intx,int y){int t;t=x;x=y;y=t;printf("x=%d\n",x);printf("y=%d\n",y);} Friday, February 11, 201123PRADEEP DWIVEDI(pur.B.TECH-IT)function declarationcalling function and pass (a,b)means(10,20)control comes herewith x=10y=20control comes hereprint x=20y=10print a=10b=20
  • 23.
    CALL BY REFERENCEFriday,February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)24 when we pass address as a function parameter this is called call by reference.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.CALL BY REFERENCE DISCUSSED IN DETAIL AFTER STUDYING THE POINTERS
  • 24.
    PART-6.3Friday, February 11,201125PRADEEP DWIVEDI(pur.B.TECH-IT)
  • 25.
    POINTERFriday, February 11,2011PRADEEP DWIVEDI(pur.B.TECH-IT)26A pointer is a derived data type in c. it is built from one of the fundamental data types available in c.Pointer variable is a special variable that holds the address of any other variable.If we want to hold the address of any integer variable that time we need an integer pointer variable and so on.A pointer variable prefix asterisk sign(*) at declaration time.Eg:- int *p;
  • 26.
    TAKE AN EXAMPLEFOR UNDERSTANDING POINTERFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)27int a=20;it stores in memory as- a 5000these address is stored as-&a=5000(address of 20integer variable declarationvariable name20valueaddress
  • 27.
    TAKE AN EXAMPLEFOR UNDERSTANDING POINTERFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)28to store the address we declare a pointer variable-int *p; p=&a;(p is a pointer variable which holds the address)*p=25;(means this stores the 25 at address 5000)integer pointer variable declaration p=5000value at address
  • 28.
    POINTER DECLARATION STYLEFriday,February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)29pointer variables are declared similarly as normal variables except for the addition of unary * operator.this symbol can appear anywhere between the type name and the pointer variable name.programmers use the following styles-int* p;int *p;int * p;style 1style 2(generally used)style 3
  • 29.
    prog6//Demo for pointer#include<stdio.h>#include<conio.h>voidmain(){inti=3,*x;float j=1.5,*y;char k='c',*z;printf("the value of i=%d\n",i);printf("the value of j=%f\n",j);printf("the value of k=%c\n",k);x=&i;y=&j;z=&k;printf("original value of x=%d\n",x);printf("original value of y=%d\n",y);printf("original value of z=%d\n",z);x++;y++;z++;printf("new value of x=%d\n",x);printf("new value of y=%d\n",y);printf("new value of z=%d\n",z);getch();}Friday, February 11, 201130PRADEEP DWIVEDI(pur.B.TECH-IT)
  • 30.
    EXPLAINATIONFriday, February 11,2011PRADEEP DWIVEDI(pur.B.TECH-IT)31step1 I j k x y z 10 20 30 40 50 60step2 10 20 30 40 50 60step3 10 20 30 40 50 60 31.5cvariablevalueaddress(suppose)31.5c10203031.5c122431
  • 31.
    POINTER INCREMENT ANDSCALE FACTORFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)32when we increment a pointer, its value is increased by the length of the data type. this length is called scale factors.eg:- let p1 is an integer pointer with an initial value says- 2800,then after operation p1=p1+1; the value of it will be 2802 , and not 2801.the length of various data type are as follows-character 1 byteinteger 2 bytesfloat 4 byteslong integer 4 bytesdouble 8 bytes
  • 32.
    prog7Friday, February 11,2011PRADEEP DWIVEDI(pur.B.TECH-IT)33//This demo is for pointer increment and scale factorvoid main(){inti=4,*j,*k;clrscr();j=&i;printf("The value of j:%d\n",j);j=j+1;printf("The value of j:%d\n",j);k=j+3;printf("The value of k:%d\n",k);getch();}Suppose &i=40Address of I is assign to JAddress of i(value of j) is printedVlue of j is increased by 1(40+2=42) Vlue of j is increased by 3(42+6=48)
  • 33.
    POINTER EXPRESSIONFriday, February11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)34int *p1,*p2;y=*p1 * *p2;=(*p1)*(*p2) z=*p1/*p2 we may not use pointers in division or multiplication.p1/p2p1*p2p1/3valid(because these are the value at address)allowedc allows us to add integers to or subtract integers from pointers, as well as to subtract one pointer from another.p1+4;p2-2; p1-p2; invalid(because these are the address)
  • 34.
    RULES FOR POINTEROPERATIONFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)35a pointer variable can be assigned the address of another variable.a pointer variable can be assigned the value of another pointer variable.a pointer variable can be assigned with zero or NULL value.a pointer variable can prefixed or post fixed with increment or decrement operator.an integer value may be added or subtract a pointer variable.when two pointer point to the same array, one pointer variable can be subtracted from another.when two pointer points to the object of same data types, they can be compared using relational operator.a pointer variable can’t be multiplied by a constant.two pointer variable can’t be addeda value can not be assigned to an arbitary address. (i.e. & =10 is illegal)
  • 35.
    CHAIN OF POINTERFriday,February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)36int a=10; p2 p1 ap1 holds the address of integer variableint *p1;p1=&a;p2 holds the address of integer pointer variableint **p2;p2=&p1;valueaddress1address2
  • 36.
    prog8//prog-for chain ofpointer#include<stdio.h>#include<conio.h>void main(){int x,*p1,**p2;clrscr();x=100;p1=&x;p2=&p1;printf("Address of a=%d\n",p1);printf("Address of p1=%d\n",p2);printf("Value at address p1=%d\n",**p2);printf("Value at address p2=%d\n",*p2);getch();}explanation:-in last statement second last statement**p2=*(*p2) =*(value at address p2) =*(p1) =value at address p1 =100Friday, February 11, 201137PRADEEP DWIVEDI(pur.B.TECH-IT)
  • 37.
    PART-6.4Friday, February 11,201138PRADEEP DWIVEDI(pur.B.TECH-IT)
  • 38.
    POINTER vs ARRAYFriday,February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)39Array is also used to hold the address but, array variable used to holds the base address of an array.The term base address means address of very first element in the array.Suppose we declare an array-int x[5]={1,2,3,4,5};
  • 39.
    POINTER vs ARRAYFriday,February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)40 x[0] x[1] x[2] x[3] x[4] element value 1000 1002 1004 1006 1008 address base address i.e. x=&x[0]=1000;x+1=&x[1]=1002x+2=&x[2]=1004x+3=&x[3]=1006x+4=&x[4]=1008with the help of base address we can calculate the address of any element by using its index and the scale factor of the data typeaddress of x[3]=base address+(3*scale factor of int)=1000+(3*2)=1006
  • 40.
    IMPORTANT POINTFriday, February11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)41An array variable is always having address of it zero index by default.Array variable can hold the address of only zero index element these can not hold the address of any other element.But pointer can hold the address of any element in the list.An array variable is a reference variable it is little bit similar with pointer.
  • 41.
    prog9//prog- pointer vsarray#include<stdio.h>#include<conio.h>void main(){intarr[]={10,20,36,72,45,36};int *j,*k;clrscr();j=&arr[4];k=(arr+4);if(j==k)printf("Two pointers point two the same location");elseprintf("Two pointers do not point to the same location");getch();} Friday, February 11, 201142PRADEEP DWIVEDI(pur.B.TECH-IT)address of arr[4]=&arr[4](arr+4) arr[0]+4=&arr[4]true
  • 42.
    prog10//wap to sibtracta pointer from anoter pointer#include<stdio.h>#include<conio.h>void main(){intarr[]={10,20,30,40,60,57,56};int *i,*j;clrscr();i=&arr[1];j=&arr[5];printf("value of i:%d\n",i);printf("value of j:%d\n",j);printf("The value at address i:%d\n",*i);printf("The value at address j:%d\n",*j);printf("The difference between address: %d\n",j-i);printf("The difference between values is %d\n",*j-*i);getch();}Friday, February 11, 201143PRADEEP DWIVEDI(pur.B.TECH-IT)I THINK THERE IS NO NEED TO EXPLIAN THIS PROGRAM
  • 43.
    REMOVE CONFUSIONFriday, February11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)44I think pointer is very confusing topic in “C”Here we try to remove confusion –Suppose - int *p;Pointer variable declared with (*) sign such as (*p) but address is stored in p not *p*p is always store the value at addressPointer variable declaration
  • 44.
    POINTER AND CHARACTERSTRINGSFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)45The character arrays are declared and initialized as follows:- char str[5]=“good”;The compiler automatically inserts the null character ‘\0’ at the end of the string.C supports an alternative method to create string using pointer variable of type char.eg: char *str=“good”;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. NOTEFriday, February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)46When we want to print the string, that time we use the format string (%s).and if we want to print the address of string that time we use format string (%d) or (%u)
  • 45.
    PROG11Friday, February 11,2011PRADEEP DWIVEDI(pur.B.TECH-IT)47//PROG POINTER VS CHARACTER ARRAY#include<stdio.h>#include<conio.h>void main(){char arr[]="PRADEEP DWIVEDI";char *s="HINDUSTAN COLLEGE OF SCIENCE AND TECHNOLOGY";clrscr();printf("%s\n",arr);printf("%s\n",s);printf("%d %d",arr,s);getch();}PRINT STRINGPRINT ADDRESSES
  • 46.
    SIZE OF OPERATORFriday,February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)48With the help of sizeof operator we can find out the size of value or variable that is occupied in the memory.Note:-each and every pointer variable always occupied two bytes in memory either it is float or char or int or etc.
  • 47.
    prog12.cFriday, February 11,2011PRADEEP DWIVEDI(pur.B.TECH-IT)49//PROG- DEMO FOR SIZE OF OPERATOR.#include<stdio.h>#include<conio.h>void main(){char arr[10]="HINDUSTAN";char *s="HINDUSTAN";float *p;clrscr();s++;//arr++;printf("%s\n",arr);printf("%s\n",s);printf("%d %d %d",sizeof(s),sizeof(arr),sizeof(p));getch();}print HINDUSTANprint INDUSTANBECAUSE ADDRESS OF S IS INCREASED BY ONE CHARACTERTHIS PRINT THE SIZE AS-2 10 2
  • 48.
    CALL BY REFERENCEFriday,February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)50 when we pass address as a function parameter this is called call by reference.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.
  • 49.
    prog13//PROG-WAP FOR SWAPINGvoidmain(){int a=10;int b=20;void swapv(int*,int*);clrscr();swapv(&a,&b);printf("a=%d\n",a);printf("b=%d\n",b);getch();}void swapv(int *x,int *y){int t;t=*x;*x=*y;*y=t;printf("*x=%d\n",*x);printf("*y=%d\n",*y);getch();}Friday, February 11, 201151PRADEEP DWIVEDI(pur.B.TECH-IT)WHEN WE RUN THIS PROGRAM THIS PROGRAM PRODUCE THE CHANGE IN SWAPV FUNCTION AND MAIN FUNCTION ALSO BECAUSE VALUE IS CHANGE AT ADDRESSfunction prototypingfunction callingpass addressesstores x=&a;y=&b;control comes hereprint the value at addres 20 10values are also change herea=20 b=10
  • 50.
    FUNCTION RETURNING POINTERFriday,February 11, 2011PRADEEP DWIVEDI(pur.B.TECH-IT)52When we declare a function as pointer type then function return pointer type value (address ).
  • 51.
    prog14//PROG- TO FINDTHE MAXIMUM NUMBER#include<stdio.h>#include<conio.h>int *larger(int*,int*);//prototypevoid main(){int a=10;int b=20;int *p;clrscr();p=larger(&a,&b);//function callprintf("Greater number is: %d",*p);getch();}int *larger(int *x,int *y){if(*x>*y)return(x);//address of aelsereturn(y);//address of b}Friday, February 11, 201153PRADEEP DWIVEDI(pur.B.TECH-IT)try to under stand it self