Unit 3
Unit 3
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
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 */