PREPARED BY:- PRADEEP DWIVEDI (pursuing B.TECH-IT) FROM H.C.S.T.(MATHURA) Mob-+919027843806 [email protected] C-PROGRAMMING SLIDE-6 5/30/22 1 PRADEEP DWIVEDI(pur.B.TECH-IT)
Wednesday, April 12, 2023
1
PREPARED BY:-PRADEEP DWIVEDI
(pursuing B.TECH-IT)
FROM H.C.S.T.(MATHURA)
Mob-+919027843806
C-PROGRAMMING SLIDE-6
PRADEEP DWIVEDI(pur.B.TECH-IT)
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
2
C-6
TOPIC:- USER DEFINED FUNCTION POINTERS
Wednesday, April 12, 2023
3
PRADEEP DWIVEDI(pur.B.TECH-IT)
PART-6.1
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
4
USER DEFINED FUNCTION
Every 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.
Wednesday, April 12, 2023
5
ELEMENTS OF A USER DEFINED FUNCTION
PRADEEP DWIVEDI(pur.B.TECH-IT)
1. Function prototyping/declaration.2. function definition.3. function invocation/calling.
Wednesday, April 12, 2023
6
FUNCTION DECLARATION/PROTOTYPING
PRADEEP DWIVEDI(pur.B.TECH-IT)
Like 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-
1. function type(return type).
2. function name.
3. parameter list.
4. terminating semicolon.
eg:-
int sum(int a,int b);
or int sum(int,int);
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
7
FUNCTION DECLARATION/PROTOTYPING
return 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(;).
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
8
DEFINITION OF FUNCTION
A function definition, also known as function implementation shall include the following elements.
1. Function name.2. Function type.3. List of parameter.4. Local variable declaration.5. Function statement.6. A return statement.
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
9
DEFINITION OF FUNCTION
Format of function definition given by a example-
int sum(int a,int b){c=a+b;return c;}
Function_type(return_type)
Function name
Parameter list
Function statement
return statement
Wednesday, April 12, 2023
10
FUNCTION CALLING
PRADEEP DWIVEDI(pur.B.TECH-IT)
A 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.
Wednesday, April 12, 2023
11
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
pradeep dwivedi\n");}
PRADEEP DWIVEDI(pur.B.TECH-IT)
function declaration
function calling
control comes here
print this line
Wednesday, April 12, 2023
12
NOTE
PRADEEP DWIVEDI(pur.B.TECH-IT)
We 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.
Wednesday, April 12, 2023
13
PRADEEP DWIVEDI(pur.B.TECH-IT)
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");}
Function declaration
Function calling
Control comes here
Print this line
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
14
NOTE
When 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.
Wednesday, April 12, 2023
15
PRADEEP DWIVEDI(pur.B.TECH-IT)
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");}
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
16
return KEYWORD
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.
in the case of main() function value is return to the compiler. because main() function called by the compiler.
Wednesday, April 12, 2023
17
PRADEEP DWIVEDI(pur.B.TECH-IT)
PART-6.2
Wednesday, April 12, 2023
18
CATEGORIES OF A FUNCTION
PRADEEP DWIVEDI(pur.B.TECH-IT)
On 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.
Categories of function
parameterizedNon
parameterized
Wednesday, April 12, 2023
19
Prog4(based on parameterized-function)
//w.a.p. to add two number by using parameterized function
#include<stdio.h>#include<conio.h>void main(){int a,b,sum;int calsum(int,int);clrscr();printf("Enter two values");scanf("%d%d",&a,&b);
sum=calsum(a,b);printf("\n sum=
%d",sum);getch();}int calsum(int x,int y){int d;d=x+y;return(d);}
PRADEEP DWIVEDI(pur.B.TECH-IT)
Function declaration
Function calling
Wednesday, April 12, 2023
20
TERMS RELATED TO PROGRAM
PRADEEP DWIVEDI(pur.B.TECH-IT)
the 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.
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
21
PARAMETERIZED FUNCTION
to invoke(call) a parameterize function there are two ways-
1. call by vale.2. call by reference.
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
22
CALL BY VALUE
if 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.
Wednesday, April 12, 2023
23
PRADEEP DWIVEDI(pur.B.TECH-IT)
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(int x,int y)
{int t;t=x;x=y;y=t;printf("x=%d\n",x);printf("y=%d\n",y);}
control comes herewith x=10
y=20
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
24
CALL BY REFERENCE
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
Wednesday, April 12, 2023
25
PRADEEP DWIVEDI(pur.B.TECH-IT)
PART-6.3
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
26
POINTER
A 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;
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
27
TAKE AN EXAMPLE FOR UNDERSTANDING POINTER
int a=20; it stores in memory as- a 5000 these address is stored as-
&a=5000(address of 20
integer variable declaration
20
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
28
TAKE AN EXAMPLE FOR UNDERSTANDING POINTER
to 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=5000
value at address
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
29
POINTER DECLARATION STYLE pointer 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 1
style 2(generally used)
style 3
Wednesday, April 12, 2023
30
prog6
//Demo for pointer#include<stdio.h>#include<conio.h>void main(){int i=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();}
PRADEEP DWIVEDI(pur.B.TECH-IT)
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
31
EXPLAINATIONstep1 I j k x y z
10 20 30 40 50 60step2
10 20 30 40 50 60step3
10 20 30 40 50 60
3 1.5 c variable
address(suppose)3 1.5 c 10 20 30
3 1.5 c 12 24 31
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
32
POINTER INCREMENT AND SCALE FACTOR
when 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 byte integer 2 bytes float 4 bytes long integer 4 bytes double 8 bytes
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
33
prog7
//This demo is for pointer increment and scale factorvoid main(){int i=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();}
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
34
POINTER EXPRESSION
int *p1,*p2; y=*p1 * *p2;=(*p1)*(*p2) z=*p1/*p2 we may not use pointers in division or
multiplication. p1/p2 p1*p2 p1/3
valid(because these are
the value at address)
invalid(because these are
the address)
c 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;
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
35
RULES FOR POINTER OPERATION a 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 added a value can not be assigned to an arbitary address.
(i.e. & =10 is illegal)
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
36
CHAIN OF POINTER
int a=10;
p2 p1 a
p1 holds the address of integer variable
p2 holds the address of integer pointer
variable
int *p1;p1=&a;
int **p2;p2=&p1;
valueaddress1address2
Wednesday, April 12, 2023
37
PRADEEP DWIVEDI(pur.B.TECH-IT)
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=100
Wednesday, April 12, 2023
38
PRADEEP DWIVEDI(pur.B.TECH-IT)
PART-6.4
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
39
POINTER vs ARRAY
Array 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};
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
40
POINTER vs ARRAY
x[0] x[1] x[2] x[3] x[4]
element
value 1000 1002 1004 1006 1008 address
base addressi.e. x=&x[0]=1000;
1 2 3 4 5
x+1=&x[1]=1002x+2=&x[2]=1004x+3=&x[3]=1006x+4=&x[4]=1008
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
address of x[3]=base address+(3*scale factor of int)
=1000+(3*2)=1006
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
41
IMPORTANT POINT
An 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.
Wednesday, April 12, 2023
42
PRADEEP DWIVEDI(pur.B.TECH-IT)
prog9
//prog- pointer vs array#include<stdio.h>#include<conio.h>void main(){int
arr[]={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();}
Wednesday, April 12, 2023
43
prog10
//wap to sibtract a pointer from anoter pointer
#include<stdio.h>#include<conio.h>void main(){int arr[]={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();}
PRADEEP DWIVEDI(pur.B.TECH-IT)
I THINK THERE IS NO NEED TO EXPLIAN
THIS PROGRAM
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
44
REMOVE CONFUSION
I 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 address
Pointer variable declaration
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
45
POINTER AND CHARACTER STRINGS
The 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.
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
46
NOTE
When 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)
Wednesday, April 12, 2023
47
PROG11
PRADEEP DWIVEDI(pur.B.TECH-IT)
//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();}
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
48
SIZE OF OPERATOR
With 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.
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
49
prog12.c
//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 INDUSTAN BECAUSE ADDRESS OF S IS
INCREASED BY ONE CHARACTER
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
50
CALL BY REFERENCE
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.
Wednesday, April 12, 2023
51
PRADEEP DWIVEDI(pur.B.TECH-IT)
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();}
WHEN WE RUN THIS PROGRAM THIS PROGRAM PRODUCE THE CHANGE IN SWAPV FUNCTION AND MAIN FUNCTION ALSO BECAUSE VALUE IS CHANGE AT ADDRESS
print the value at addres 20 10
Wednesday, April 12, 2023PRADEEP DWIVEDI(pur.B.TECH-IT)
52
FUNCTION RETURNING POINTER When we declare a function as pointer
type then function return pointer type value (address ).
Wednesday, April 12, 2023
53
PRADEEP DWIVEDI(pur.B.TECH-IT)
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}try to under stand it self