Array Functions Pointers Strutures
Array Functions Pointers Strutures
Whenever we want to work with large number of data values, we need to use that much number of different
variables. As the number of variables are increasing, complexity of the program also increases and
programmers get confused with the variable names. There may be situations in which we need to work with
large number of similar data values. To make this work more easy, C programming language provides a
concept called "Array".
An array is a variable which can store multiple values of same data type at a time.
"Collection of similar data items stored in continuous memory locations with single name".
int a, b, c;
Here, the compiler allocates 2 bytes of memory with name 'a', another 2 bytes of memory with name 'b' and
more 2 bytes with name 'c'. These three memory locations are may be in sequence or may not be in
sequence. Here these individual variables store only one value at a time.
int a[3];
Here, the compiler allocates total 6 bytes of continuous memory locations with single name 'a'. But allows
to store three different integer values (each in 2 bytes of memory) at a time. And memory is organized as
follows...
That means all these three memory locations are named as 'a'. But "how can we refer individual
elements?" is the big question. Answer for this question is, compiler not only allocates memory, but also
assigns a numerical value to each individual element of an array. This numerical value is called as "Index".
Index values for the above example are as follows...
The individual elements of an array are identified using the combination of 'name' and 'index' as follows...
arrayName[indexValue]
For the above example, the individual elements can be referred as follows...
If I want to assign a value to any of these memory locations (array elements), we can assign as follows...
a[1] = 100;
The result will be as follows...
Types of Arrays in C
In c programming language, arrays are classified into two types. They are as follows...
other words, single dimensional arrays are used to store a row of values. In single dimensional array, data
is stored in linear form. Single dimensional arrays are also called as one-dimensional arrays, Linear
We use the following general syntax for declaring a single dimensional array...
The above declaration of single dimensional array reserves 60 continuous memory locations of 2 bytes
each with the name rollNumbers and tells the compiler to allow only integer values into those memory
locations.
We use the following general syntax for declaring and initializing a single dimensional array with size and
initial values.
The above declaration of single dimensional array reserves 6 contiguous memory locations of 2 bytes each
with the name marks and initializes with value 89 in first memory location, 90 in second memory location,
76 in third memory location, 78 in fourth memory location, 98 in fifth memory location and 86 in sixth
memory location.
We can also use the following general syntax to intialize a single dimensional array without specifying size
The array must be initialized if it is created without specifying any size. In this case, the size of the array is
Example Code
int marks [] = { 89, 90, 76, 78, 98, 86 } ;
In the above example declaration, size of the array 'marks' is 6 and the size of the
array 'studentName' is 3. This is because in case of character array, compiler stores one exttra character
In c programming language, to access the elements of single dimensional array we use array name
followed by index value of the element that to be accessed. Here the index value must be enclosed in
square braces. Index value of an element in an array is the reference number given to each element at the
time of memory allocation. The index value of single dimensional array starts with zero (0) for first element
and incremented by one for each element. The index value in an array is also called
as subscript or indices.
We use the following general syntax to access individual elements of single dimensional array...
arrayName [ indexValue ]
Example Code
marks [2] = 99 ;
In the above statement, the third element of 'marks' array is assinged with value '99'.
Multi Dimensional Array
An array of arrays is called as multi dimensional array. In simple words, an array created with more than
one dimension (size) is called as multi dimensional array. Multi dimensional array can be of two
Most popular and commonly used multi dimensional array is two dimensional array. The 2-D arrays are
used to store data in the form of table. We also use 2-D arrays to create mathematical matrices.
We use the following general syntax for declaring a two dimensional array...
The above declaration of two dimensional array reserves 6 continuous memory locations of 2 bytes each in
We use the following general syntax for declaring and initializing a two dimensional array with specific
Example Code
int matrix_A [2][3] = { {1, 2, 3},{4, 5, 6} } ;
The above declaration of two-dimensional array reserves 6 contiguous memory locations of 2 bytes each in
the form of 2 rows and 3 columns. And the first row is initialized with values 1, 2 & 3 and second row is
Example Code
int matrix_A [2][3] = {
{1, 2, 3},
{4, 5, 6}
} ;
In a c programming language, to access elements of a two-dimensional array we use array name followed
by row index value and column index value of the element that to be accessed. Here the row and column
index values must be enclosed in separate square braces. In case of the two-dimensional array the
We use the following general syntax to access the individual elements of a two-dimensional array...
In the above statement, the element with row index 0 and column index 1 of matrix_A array is assinged
and are solved individually to make the program easier. In C, this concept is implemented using functions.
Functions are used to divide a larger program into smaller subprograms such that the program becomes
Function is a subpart of a program used to perform a specific task and is executed individually.
Every C program must contain at least one function called main(). However, a program may also contain
other functions.
• Function Definition
• Function Call
Function Declaration
The function declaration tells the compiler about function name, the data type of the return value and
parameters. The function declaration is also called a function prototype. The function declaration is
performed before the main function or inside the main function or any other function.
returnType functionName(parametersList);
In the above syntax, returnType specifies the data type of the value which is sent as a return value from
the function definition. The functionName is a user-defined name used to identify the function uniquely in
the program. The parametersList is the data values that are sent to the function definition.
Function Definition
The function definition provides the actual code of that function. The function definition is also known as
the body of the function. The actual task of the function is implemented in the function definition. That
means the actual instructions to be performed by a function are written in function definition. The actual
instructions of a function are written inside the braces "{ }". The function definition is performed before the
returnType functionName(parametersList)
{
Actual code...
Function Call
The function call tells the compiler when to execute the function definition. When a function call is
executed, the execution control jumps to the function definition where the actual code gets executed and
returns to the same functions call once the execution completes. The function call is performed inside the
functionName(parameters);
ADVANTAGES OF FUNCTIONS
The C Programming Language provides pre-defined functions to make programming easy. These pre-
defined functions are known as syatem defined functions. The system defined function is defined as
follows...
The function whose definition is defined by the system is called as system defined function.
The system defined functions are also called as Library Functions or Standard Functions or Pre-
Defined Functions. The implementation of system defined functions is already defined by the system.
In C, all the system defined functions are defined inside the header
files like stdio.h, conio.h, math.h, string.h etc., For example, the funtions printf() and scanf() are
Whenever we use system defined functions in the program, we must include the respective header file
using #include statement. For example, if we use a system defined function sqrt() in the program, we must
include the header file called math.h because the function sqrt() is defined in math.h.
Points to be Remembered
• System defined functions are implemented in .dll files. (DLL stands for Dynamic Link Library).
• To use system defined functions the respective header file must be included.
User Defined Functions
In C programming language, users can also create their own functions. The functions that are created by
users are called as user defined functions. The user defined function is defined as follows...
The function whose definition is defined by the user is called as user defined function.
That means the function that is implemented by user is called as user defined function. For example, the
In C every user defined function must be declared and implemented. Whenever we make function call the
function definition gets executed. For example, consider the following program in which we create a
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int num1, num2, result ;
int addition(int,int) ; // function declaration
clrscr() ;
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);
In the above example program, the function declaration statement "int addition(int,int)" tells the compiler
that there is a function with name addition which takes two integer values as parameters and returns an
integer value. The function call statement takes the execution control to the additon() definition along with
values of num1 and num2. Then function definition executes the code written inside it and comes back
In the concept of functions, the function call is known as "Calling Function" and the function definition is
When we make a function call, the execution control jumps from calling function to called function. After
executing the called function, the execution control comes back to calling function from called function.
When the control jumps from calling function to called function it may carry one or more data values called
"Paramenters" and while coming back it may carry a single value called "return value". That means the
data values transferred from calling function to called function are called as Parameters and the data value
Based on the data flow between the calling function and called function, the functions are classified as
follows...
In this type of functions there is no data transfer between calling function and called function. Simply the
execution control jumps from calling-function to called function and executes called function, and finally
comes back to the calling function. For example, consider the following program...
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
void addition() ; // function declaration
clrscr() ;
getch() ;
}
void addition() // function definition
{
int num1, num2 ;
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);
printf("Sum = %d", num1+num2 ) ;
}
Function with Parameters and without Return value
In this type of functions there is data transfer from calling-function to called function (parameters) but there
is no data transfer from called function to calling-function (return value). The execution control jumps from
calling-function to called function along with the parameters and executes called function, and finally comes
back to the calling function. For example, consider the following program...
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int num1, num2 ;
void addition(int, int) ; // function declaration
clrscr() ;
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);
getch() ;
}
void addition(int a, int b) // function definition
{
printf("Sum = %d", a+b ) ;
}
Function without Parameters and with Return value
In this type of functions there is no data transfer from calling-function to called-function (parameters) but
there is data transfer from called function to calling-function (return value). The execution control jumps
from calling-function to called function and executes called function, and finally comes back to the calling
function along with a return value. For example, consider the following program...
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int result ;
int addition() ; // function declaration
clrscr() ;
In this type of functions there is data transfer from calling-function to called-function (parameters) and also
from called function to calling-function (return value). The execution control jumps from calling-function to
called function along with parameters and executes called function, and finally comes back to the calling
function along with a return value. For example, consider the following program...
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int num1, num2, result ;
int addition(int, int) ; // function declaration
clrscr() ;
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);
Points to be Remembered
called function and executes function definition, and finally comes back to the calling function. When the
execution control is transferred from calling-function to called-function it may carry one or number of data
Parameters are the data values that are passed from calling function to called function.
• Actual Parameters
• Formal Parameters
The actual parameters are the parameters that are speficified in calling function. The formal
parameters are the parameters that are declared at called function. When a function gets executed, the
In C Programming Language, there are two methods to pass parameters from calling function to called
• Call by Value
• Call by Reference
Call by Value
In call by value parameter passing method, the copy of actual parameter values are copied to formal
parameters and these formal parameters are used in called function. The changes made on the formal
parameters does not effect the values of actual parameters. That means, after the execution control
comes back to the calling function, the actual parameter values remains same. For example consider the
following program...
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int num1, num2 ;
void swap(int,int) ; // function declaration
clrscr() ;
num1 = 10 ;
num2 = 20 ;
Output:
In the above example program, the variables num1 and num2 are called actual parameters and the
variables a and b are called formal parameters. The value of num1 is copied into a and the value of num2
is copied into b. The changes made on variables a and b does not effect the values of num1 and num2.
Call by Reference
In Call by Reference parameter passing method, the memory location address of the actual parameters is
copied to formal parameters. This address is used to access the memory locations of the actual
parameters in called function. In this method of parameter passing, the formal parameters must
be pointer variables.
That means in call by reference parameter passing method, the address of the actual parameters is passed
to the called function and is recieved by the formal parameters (pointers). Whenever we use these formal
parameters in called function, they directly access the memory locations of actual parameters. So the
changes made on the formal parameters effects the values of actual parameters. For example
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int num1, num2 ;
void swap(int *,int *) ; // function declaration
clrscr() ;
num1 = 10 ;
num2 = 20 ;
In the above example program, the addresses of variables num1 and num2 are copied to pointer
variables a and b. The changes made on the pointer variables a and b in called function effects the values
is used to create user-defined data type in the C programming language. As the structure used to create a
user-defined data type, the structure is also said to be “user-defined data type in C”.
In other words, a structure is a collection of non-homogeneous elements. Using structure we can define
new data types called user-defined data types that holds multiple values of the different data type. The
Structure is a colloction of different type of elements under a single name that acts as user defined
data type in C.
Generally, structures are used to define a record in the c programming language. Structures allow us to
combine elements of a different data type into a group. The elements that are defined in a structure are
To create structure in c, we use the keyword called "struct". We use the following syntax to create
struct <structure_name>
{
data_type member1;
data_type member2, member3;
.
.
};
Following is the example of creating a structure called Student which is used to hold student record.
Creating structure in C
struct Student
{
char stud_name[30];
int roll_number;
float percentage;
} ;
In a c programming language, there are two ways to create structure variables. We can create structure
variable while defining the structure and we can also create after terminating structure using struct
keyword.
To access members of a structure using structure variable, we use dot (.) operator. Consider the following
example code...
void main(){
struct Student stud_2; // using struct keyword
In the above example program, the stucture variable "stud_1 is created while defining the structure and the
variable "stud_2 is careted using struct keyword. Whenever we access the members of a structure we use
When the structures are used in the c programming language, the memory does not allocate on defining a
structure. The memory is allocated when we create the variable of a particular structure. As long as the
variable of a structure is created no memory is allocated. The size of memory allocated is equal to the sum
of memory required by individual members of that structure. In the above example program, the variables
The memory required by a structure variable is sum of the memory required by individual members of that
structure.
Pointers in C
In the c programming language, we use normal variables to store user data values. When we declare a
variable, the compiler allocates required memory with the specified name. In the c programming language,
every variable has a name, datatype, value, storage class, and address. We use a special type of variable
called a pointer to store the address of another variable with the same datatype. A pointer is defined as
follows...
Pointer is a special type of variable used to store the memory location address of a variable.
In the c programming language, we can create pointer variables of any datatype. Every pointer stores the
address the variable with same datatype only. That means integer pointer is used store the address of
In c programming language, we use the reference operator "&" to access the address of variable. For
example, to access the address of a variable "marks" we use "&marks". We use the following printf
Example Code
printf("Address : %u", &marks) ;
In the above example statement %u is used to display address of marks variable. Address of any memory
In c programming language, declaration of pointer variable is similar to the creation of normal variable but
the name is prefixed with * symbol. We use the following syntax to declare a pointer variable...
datatype *pointerName ;
A variable declaration prefixed with * symbol becomes a pointer variable.
Example Code
int *ptr ;
In the above example declaration, the variable "ptr" is a pointer variable that can be used to store any
To assign address to a pointer variable we use assignment operator with the following syntax...
In the above declaration, variable "a" is a normal integer variable and variable "ptr" is an integer pointer
variable. If we want to assign the address of variable "a" to pointer variable "ptr" we use the following
statement...
Example Code
ptr = &a ;
In the above statement, the address of variable "a" is assigned to pointer variable "prt". Here we say that
Pointer variables are used to store the address of other variables. We can use this address to access the
value of the variable through its pointer. We use the symbol "*" infront of pointer variable name to access
the value of variable to which the pointer is pointing. We use the following general syntax...
*pointerVariableName
Example Code
#include<stdio.h>
#include<conio.h>
void main()
{
int a = 10, *ptr ;
clrscr();
ptr = &a ;
Output:
In the above example program, variable a is a normal variable and variable ptr is a pointer variable.
Address of variable a is stored in pointer variable ptr. Here ptr is used to access the address of
Every pointer variable is used to store the address of another variable. In computer memory address of any
memory location is an unsigned integer value. In c programming language, unsigned integer requires 2
bytes of memory. So, irrespective of pointer datatype every pointer variable is allocated with 2 bytes of
memory.
Dynamic Memory Allocation in C
In C programming language, when we declare variables memory is allocated in space called stack. The
memory allocated in the stack is fixed at the time of compilation and remains until the end of the program
execution. When we create an array, we must specify the size at the time of the declaration itself and it can
not be changed during the program execution. This is a major problem when we do not know the number of
values to be stored in an array. To solve this we use the concept of Dynamic Memory Allocation. The
dynamic memory allocation allocates memory from heap storage. Dynamic memory allocation is defined
as follow...
Allocation of memory during the program execution is called dynamic memory allocation.
or
Dynamic memory allocation is the process of allocating the memory manually at the time of
program execution.
are FOUR standard library functions that are defined in the header file known as "stdlib.h". They are as
follows...
1. malloc()
2. calloc()
3. realloc()
4. free()
malloc()
malloc() is the standard library function used to allocate a memory block of specified number of bytes and
returns void pointer. The void pointer can be casted to any datatype. If malloc() function unable to allocate
Syntax
void* malloc(size_in_bytes)
Example
Example Program for malloc().
#include<stdio.h>
#include<conio.h>
int main () {
char *title;
return(0);
}
calloc()
calloc() is the standard library function used to allocate multiple memory blocks of the specified number of
bytes and initializes them to ZERO. calloc() function returns void pointer. If calloc() function unable to
allocate memory due to any reason it returns a NULL pointer. Generally, calloc() is used to allocate
memory for array and structure. calloc() function takes two arguments and they are 1. The number of
Syntax
Example
Example Program for calloc().
#include<stdio.h>
#include<conio.h>
int main () {
int i, n;
int *ptr;
return(0);
}
realloc()
realloc() is the standard library function used to modify the size of memory blocks that were previously
allocated using malloc() or calloc(). realloc() function returns void pointer. If calloc() function unable to
Syntax
Example
Example Program for realloc().
#include<stdio.h>
#include<conio.h>
int main () {
char *title;
return(0);
}
free()
free() is the standard library function used to deallocate memory block that was previously allocated using
malloc() or calloc(). free() function returns void pointer. When free() function is used with memory allocated
that was created using calloc(), all the blocks are get deallocated.
Syntax
void free(*pointer)
Example
Example Program for free().
#include<stdio.h>
#include<conio.h>
int main () {
char *title;
free(title);
return(0);