Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

TCS Preparation Camp

(C Language)
Vijay Kumar Dwivedi
Assistant Professor
UCER, Prayagraj
Mobile: 9235668045
Email: vijay.kr.dwivedi@gmail.com
Pointer to Functions
Pointer to Function

A pointer to a function points to the address of the
executable code of the function.

You can use pointers to call functions and to pass
functions as arguments to other functions.

You cannot perform pointer arithmetic on pointers to
functions.

The type of a pointer to a function is based on both the
return type and parameter types of the function.

A declaration of a pointer to a function must have the
pointer name in parentheses.
Pointer to Functions Contd...
 int *f(int a);
 int (*g)(int a);
Which one is pointer to Function?
Pointer to Functions Contd...
 int *f(int a);
 int (*g)(int a);
Which one is pointer to Function?
 int *f(int a); /* function f returning an int* */
 int (*g)(int a); /* pointer g to a function
returning an int */

Pointer to Functions Contd...
#include <stdio.h>
 // A normal function with an int parameter // and void return type
 void fun(int a)
 {
 printf("Value of a is %d\n", a);
 }
 int main()
 {
 // fun_ptr is a pointer to function fun()
 void (*fun_ptr)(int) = &fun;
 /* The above line is equivalent of following two
 void (*fun_ptr)(int);
 fun_ptr = &fun;
 */
 // Invoking fun() using fun_ptr
 (*fun_ptr)(10);
 return 0;
 }
Lvalue and Rvalue
Lvalue and Rvalue?

L-value: “l-value” refers to memory location which identifies
an object

The l-value is one of the following:
 The name of the variable of any type i.e, an identifier of integral,
floating, pointer, structure, or union type.
 A subscript ([ ]) expression that does not evaluate to an array.
 A unary-indirection (*) expression that does not refer to an
array
 An l-value expression in parentheses.
 A const object (a nonmodifiable l-value).
 The result of indirection through a pointer, provided that it isn’t
a function pointer.
 The result of member access through pointer(-> or .)
Lvalue and Rvalue? Contd...

r-value” refers to data value that is stored at
some address in memory.

A r-value is an expression that can’t have a
value assigned to it which means r-value can
appear on right but not on left hand side of an
assignment operator(=).

int a, *p;

p = &a; // ok, assignment of address at l-value

&a = p; // error: &a is an r-value
realloc()
Realloc ()
 Let’s say we have allocated some memory
using malloc() and calloc(), but later we find
that memory is too large or too small. The
realloc() function is used to resize allocated
memory without losing old data. It’s syntax
is:
 Syntax: void *realloc(void *ptr, size_t
newsize);
Making customized header file
How to make Header Files?

Files with .h extension are called header
files in C.

These header files generally contain
function declarations which we can be used
in our main C program

Header files generally contain definitions of
data types, function prototypes and C
preprocessor commands.
Can header file be nested?

Yes. Include files can be nested any number of times. As
long as you use precautionary measures, you can avoid
including the same file twice.

In the past, nesting header files was seen as bad
programming practice, because it complicates the
dependency tracking function of the MAKE program and
thus slows down compilation.

Many of todays popular compilers make up for this difficulty
by implementing a concept called precompiled headers, in
which all headers and associated dependencies are stored
Volatile keyword in C
Volatile keyword in C
 C's volatile keyword is a qualifier that is
applied to a variable when it is declared.
 It tells the compiler that the value of the
variable may change at any time--without
any action being taken by the code the
compiler finds nearby.
Let’s Practice
Question 1
What is C language?
Question 1
What is C language?
Ans:

C is a general-purpose programming
language that is extremely popular, simple
and flexible.

It is machine-independent, structured
programming language which is used
extensively in various applications.
Question 2
What does static variable mean?
Question 2
What does static variable mean?
Ans:

Static variables have a property of preserving their value
even after they are out of their scope!

Hence, static variables preserve their previous value in their
previous scope and are not initialized again in the new scope.

A static int variable remains in memory while the program is
running.

A normal or auto variable is destroyed when a function call
where the variable was declared is over.
Question 3
What are the different storage classes in C?
Question 3
What are the different storage classes in C?
Question 4
 What is null Pointer?
Question 4
 What is null Pointer?
Ans: A null pointer is a pointer that does not
point to any object or function.
Question 5
 Difference between calloc() and malloc()

You might also like