0% found this document useful (0 votes)
6 views19 pages

Unit 3

Uploaded by

vichu0408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views19 pages

Unit 3

Uploaded by

vichu0408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

21CB101 PROBLEM

SOLVING AND C
PROGRAMMING
UNIT-3
FUNCTIONS AND POINTERS
FUNCTIONS
• C function contains set of instructions enclosed by “{ }”
which performs specific operation in a C program.
• C functions are used to avoid rewriting same logic/code
again and again in a program.
• There is no limit in calling C functions to make use of
same functionality wherever required.
• We can call functions any number of times in a program
and from any place in a program.
• A large C program can easily be tracked when it is divided
into functions.
• The core concept of C functions are, re-usability, dividing
a big task into small pieces to achieve the functionality and
to improve understandability of very large C programs.
FUNCTION DECLARATION, FUNCTION
CALL AND FUNCTION DEFINITION

• Function declaration or prototype – This


informs compiler about the function name,
function parameters and return value’s data
type.
• Function call – This calls the actual
function
• Function definition – This contains all the
statements to be executed.
HOW TO CALL C
FUNCTIONS IN A PROGRAM
• There are two ways that a C function
can be called from a program.
• Call by value
• Call by reference
CALL BY VALUE
• In call by value method, the value of the variable is passed
to the function as parameter.
• The value of the actual parameter can not be modified by
formal parameter.
• Different Memory is allocated for both actual and formal
parameters. Because, value of actual parameter is copied to
formal parameter.
Note:
• Actual parameter
– This is the argument which is used in function call.
• Formal parameter
– This is the argument which is used in function definition
CALL BY REFERENCE
• In call by reference method, the address of
the variable is passed to the function as
parameter.
• The value of the actual parameter can be
modified by formal parameter.
• Same memory is used for both actual and
formal parameters since only address is
used by both parameters.
RECURSION
• A process by which a function calls itself
repeatedly
• Either directly. X calls X
• Or cyclically in a chain. X calls Y, and Y
calls X
• Used for repetitive computations in which
each action is stated in terms of a previous
result fact(n) = n * fact (n-1)
BINARY SEARCH
•Searching for an element k in a sorted array A with n
elements
•Idea:
– Choose the middle element A[n/2]
– If k == A[n/2], we are done
– If k < A[n/2], search for k between A[0] and A[n/2 -
1]
– If k > A[n/2], search for k between A[n/2 + 1] and
A[n-1]
– Repeat until either k is found, or no more elements to
search
•Requires less number of comparisons than linear search in
the worst case (log2n instead of n)
RECURSIVE BINARY
SEARCH
• Find the element at arr[size/2], which will be the array's
midpoint. ...
• Compare the key to arr[midpoint] by calling the user
function.
• If the key is a match, return arr[midpoint];
• Otherwise return NULL, indicating that there is no match
if the array has only one element.
• Search the lower half of the array by repeatedly executing
search if the key is less than the value taken from
arr[midpoint].
• Call search recursively to search the upper half of the
array.
POINTERS
A pointer is a reference to another variable (memory
location) in a program
– Used to change variables inside a function (reference
parameters)
– Used to remember a particular member of a group
(such as an array)
– Used in dynamic (on-the-fly) memory allocation
(especially of arrays)
– Used in building complex data structures (linked lists,
stacks, queues, trees, etc.)
• A pointer variable contains a representation
of an address of another variable (P is a
pointer variable in the following):

Name V P
Address v (some value) p (some value)
int V = 101;
Abstract
101
Representation
int *P = &V;
Concrete 4 bytes for 4 bytes for
Representation int value 101 mem address v
POINTER VARIABLE
DEFINITION
Basic syntax: Type *Name
Examples:
int *P; /* P is var that can point to an int var */
float *Q; /* Q is a float pointer */
char *R; /* R is a char pointer */
Complex example:
int *AP[5]; /* AP is an array of 5 pointers to ints */
– more on how to read complex declarations later
POINTER VARIABLE
INITIALIZATION/ASSIGNMENT
NULL - pointer list constant to non-existent address
– used to indicate pointer points to nothing
Can initialize/assign pointer vars to NULL or use the
address (&) op to get address of a variable
– variable in the address operator must be of the right
type for the pointer (an integer pointer points only at
integer variables)
Examples:
int V;
int *P = &V;
int A[5];
P = &(A[2]);
POINTERS TO POINTERS
A pointer can also be made to point to a pointer
variable (but the pointer must be of a type that
allows it to point to a pointer)
Example:
int V = 101;
int *P = &V; /* P points to int V */
int **Q = &P; /* Q points to int pointer P */

printf(“%d %d %d\n”,V,*P,**Q); /* prints 101 3 times */


ARRAYS OF POINTERS
• Different arrays can have different number
of elements
– more flexibility
• Elements of a single array are contiguous
but different arrays could be located far off
in memory
• Have to be initialized element by element
• More power, responsibility
ARRAY EXAMPLE USING A POINTER
int x[4] = {12, 20, 39, 43}, *y;
y = &x[0]; // y points to the beginning of the array
printf("%d\n", x[0]); // outputs 12
printf("%d\n", *y); // also outputs 12
printf("%d\n", *y+1); // outputs 13 (12 + 1)
printf("%d\n", (*y)+1); // also outputs 13
printf("%d\n", *(y+1)); // outputs x[1] or 20
y+=2; // y now points to x[2]
printf("%d\n", *y); // prints out 39
*y = 38; // changes x[2] to 38
printf("%d\n", *y-1);// prints out x[2] - 1 or 37
*y++; // sets y to point at the next array element
printf("%d\n", *y); // outputs x[3] (43)
(*y)++; // sets what y points to to be 1 greater
printf("%d\n", *y); // outputs the new value of x[3] (44)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy