SlideShare a Scribd company logo
1 of 57
Download to read offline
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.
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).
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);
}
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ǯ.
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
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
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
Additional features͙
a) Return type of function
b) Calling functions by value or by reference
c) Recursion
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);
}
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();
}
void g()
{
a=a+2;
printf(Dz%ddz,a);
}
main()
{
f(a);
printf(Dz%ddz,a);
}
Ans:5 5 5
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.
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
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.
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)
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;
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
CONSTANT pointer:
int *const ptr=variable;
FUNCTIONAL pointer:
int f; variable
int f(); function
int *f; pointer
int *f(); function
int (*f)(); functional pointer
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 +,-
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);
}
y Output:
i=1000
i=1000
i=1000
j=2000
j=2000
k=3000
i=5
i=5
i=5
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
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
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
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
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
Test ur skills!!!
 void f(int i)
{
if(i)
{
printf(Dz%ddz,i);
f(i+1);
}
}
main()
{
f(10);
}
Output:: 10,11,12,13,ǥǥǥǥ,32767,-32767,ǥǥǥ.-1
Explanation:: when the value of i becomes Ǯ0ǯ that is
condition is false and execution stops.
#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
#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
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
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ǯ)
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);
}
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..
Output isǥǥ???
main()
{
printf(nab);
printf(bsi);
printf(rha);
}
Note::r-linefeedǥb-backspace
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..
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..
Output for the following program is??????
main()
{
int i=-1;
+i;
printf(i = %d, +i = %d n,i,+i);
}
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)..
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..
Output of the following program isǥǥǥ?
void main()
{
char *ptr=*******;
int i;
clrscr();
for(i=0;i8;i++)
{
printf(%*.*sn,8,i,ptr);
}
getch();
}
#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();
}
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.
#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
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ǥǥǥ.???
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
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++;
}
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
Output isǥǥ..?
void main()
{
static main;
int x;
x=call(main);
clrscr();
printf(%d ,x);
getch();
}
int call(int address)
{
address++;
return address;
}
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
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.
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++);
}
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
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.
46630497 fun-pointer-1

More Related Content

What's hot (20)

C++11
C++11C++11
C++11
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C standard library functions
C standard library functionsC standard library functions
C standard library functions
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
This pointer
This pointerThis pointer
This pointer
 
C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIA
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
Ch7 C++
Ch7 C++Ch7 C++
Ch7 C++
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Function C++
Function C++ Function C++
Function C++
 
C++ theory
C++ theoryC++ theory
C++ theory
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
Advanced C programming
Advanced C programmingAdvanced C programming
Advanced C programming
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
C++ programming
C++ programmingC++ programming
C++ programming
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 

Similar to 46630497 fun-pointer-1

Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptxhara69
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptSandipPradhan23
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 FocJAYA
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it outrajatryadav22
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxvanshhans21102005
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptxNagasaiT
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 

Similar to 46630497 fun-pointer-1 (20)

Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
functions
functionsfunctions
functions
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
C function
C functionC function
C function
 
Function in c program
Function in c programFunction in c program
Function in c program
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
 
Array Cont
Array ContArray Cont
Array Cont
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 

Recently uploaded

How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 

Recently uploaded (20)

How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 

46630497 fun-pointer-1

  • 1.
  • 2. 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.
  • 3. 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).
  • 4. 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); }
  • 5. 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ǯ.
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. Additional features͙ a) Return type of function b) Calling functions by value or by reference c) Recursion
  • 10. 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); }
  • 11. 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(); }
  • 13. 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.
  • 14. 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
  • 15. 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.
  • 16.
  • 17. 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)
  • 18. 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;
  • 19. 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
  • 20. CONSTANT pointer: int *const ptr=variable; FUNCTIONAL pointer: int f; variable int f(); function int *f; pointer int *f(); function int (*f)(); functional pointer
  • 21. 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 +,-
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. 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
  • 29. Test ur skills!!! void f(int i) { if(i) { printf(Dz%ddz,i); f(i+1); } } main() { f(10); }
  • 30. Output:: 10,11,12,13,ǥǥǥǥ,32767,-32767,ǥǥǥ.-1 Explanation:: when the value of i becomes Ǯ0ǯ that is condition is false and execution stops.
  • 31. #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
  • 32. #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
  • 33. 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
  • 34. 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ǯ)
  • 35. 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); }
  • 36. 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..
  • 38. 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..
  • 39. 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..
  • 40. Output for the following program is?????? main() { int i=-1; +i; printf(i = %d, +i = %d n,i,+i); }
  • 41. 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)..
  • 42. 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..
  • 43. Output of the following program isǥǥǥ? void main() { char *ptr=*******; int i; clrscr(); for(i=0;i8;i++) { printf(%*.*sn,8,i,ptr); } getch(); }
  • 44. #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(); }
  • 45. 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.
  • 46. #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
  • 47. 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ǥǥǥ.???
  • 48. 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
  • 49. 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++; }
  • 50. 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
  • 51. Output isǥǥ..? void main() { static main; int x; x=call(main); clrscr(); printf(%d ,x); getch(); } int call(int address) { address++; return address; }
  • 52. 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
  • 53. 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.
  • 54. 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++); }
  • 55. 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
  • 56. 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.