0% found this document useful (0 votes)
30 views

Array Functions Pointers Strutures

Array functions and structure of data

Uploaded by

joleho5996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Array Functions Pointers Strutures

Array functions and structure of data

Uploaded by

joleho5996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

What is an Array?

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.

An array can also be defined as follows...

"Collection of similar data items stored in continuous memory locations with single name".

To understand the concept of arrays, consider the following example declaration.

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.

Now consider the following declaration...

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...

1. Single Dimensional Array / One Dimensional Array

2. Multi Dimensional Array

Single Dimensional Array


In c programming language, single dimensional arrays are used to store list of values of same datatype. In

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

Arrays or simply 1-D Arrays.

Declaration of Single Dimensional Array

We use the following general syntax for declaring a single dimensional array...

datatype arrayName [ size ] ;


Example Code
int rollNumbers [60] ;

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.

Initialization of Single Dimensional Array

We use the following general syntax for declaring and initializing a single dimensional array with size and

initial values.

datatype arrayName [ size ] = {value1, value2, ...} ;


Example Code
int marks [6] = { 89, 90, 76, 78, 98, 86 } ;

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

and with initial values...

datatype arrayName [ ] = {value1, value2, ...} ;

The array must be initialized if it is created without specifying any size. In this case, the size of the array is

decided based on the number of values initialized.

Example Code
int marks [] = { 89, 90, 76, 78, 98, 86 } ;

char studentName [] = "CSE" ;

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

called \0 (NULL) at the end.

Accessing Elements of Single Dimensional Array

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

dimensional array or three dimensional array or four dimensional array or more...

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.

Declaration of Two Dimensional Array

We use the following general syntax for declaring a two dimensional array...

datatype arrayName [ rowSize ] [ columnSize ] ;


Example Code
int matrix_A [2][3] ;

The above declaration of two dimensional array reserves 6 continuous memory locations of 2 bytes each in

the form of 2 rows and 3 columns.

Initialization of Two Dimensional Array

We use the following general syntax for declaring and initializing a two dimensional array with specific

number of rows and coloumns with initial values.

datatype arrayName [rows][colmns] = {{r1c1value, r1c2value, ...},{r2c1, r2c2,...}...} ;

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

initialized with values 4, 5 & 6.


We can also initialize as follows...

Example Code
int matrix_A [2][3] = {
{1, 2, 3},
{4, 5, 6}
} ;

Accessing Individual Elements of Two Dimensional Array

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

compiler assigns separate index values for rows and columns.

We use the following general syntax to access the individual elements of a two-dimensional array...

arrayName [ rowIndex ] [ columnIndex ]


Example Code
matrix_A [0][1] = 10 ;

In the above statement, the element with row index 0 and column index 1 of matrix_A array is assinged

with value 10.


Functions in C
When we write a program to solve a larger problem, we divide that larger problem into smaller subproblems

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

easy to understand and easy to implement. A function is defined as follows...

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.

Every function in C has the following...

• Function Declaration (Function Prototype)

• 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.

Function declaration syntax -

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

main function or after the main function.

Function definition syntax -

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

main function or any other function or inside the function itself.

Function call syntax -

functionName(parameters);

ADVANTAGES OF FUNCTIONS

• Using funcions we can implement modular programming.

• Functions make the program more readable and understandable.

• Using functions the program implementation becomes easy.

• Once a function is created it can be used many times (code re-usability).

• Using functions larger programs can be divided into smaller modules.


Types of Functions in C
In C Programming Language, based on providing the function definition, functions are divided into two

types. Those are as follows...

• System Defined Functions

• User Defined Functions

System Defined 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

defined in the header file called stdio.h.

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 declared in header files

• 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

function main is implemented by user so it is called as user defined function.

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

fucntion called addition with two paramenters and a return value.

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);

result = addition(num1, num2) ; // function call

printf("SUM = %d", result);


getch() ;
}
int addition(int a, int b) // function definition
{
return a+b ;
}

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

to the function call along with return value.

In the concept of functions, the function call is known as "Calling Function" and the function definition is

known as "Called Function".

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

transferred from called funcion to calling function is called Return value.

Based on the data flow between the calling function and called function, the functions are classified as

follows...

• Function without Parameters and without Return value

• Function with Parameters and without Return value

• Function without Parameters and with Return value

• Function with Parameters and with Return value

Function without Parameters and without Return value

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() ;

addition() ; // function call

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);

addition(num1, num2) ; // function call

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() ;

result = addition() ; // function call


printf("Sum = %d", result) ;
getch() ;
}
int addition() // function definition
{
int num1, num2 ;
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);
return (num1+num2) ;
}
Function with Parameters and with Return value

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);

result = addition(num1, num2) ; // function call


printf("Sum = %d", result) ;
getch() ;
}
int addition(int a, int b) // function definition
{
return (a+b) ;
}

Points to be Remembered

• The parameters specified in calling function are said to be Actual Parameters.

• The parameters declared in called function are said to be Formal Parameters.

• The value of actual parameters is always copied into formal parameters.


Parameter Passing in C
When a function gets executed in the program, the execution control is transferred from calling-function to

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

values. These data values are called as parameters.

Parameters are the data values that are passed from calling function to called function.

In C, there are two types of parameters and they are as follows...

• 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

copy of actual parameter values are copied into formal parameters.

In C Programming Language, there are two methods to pass parameters from calling function to called

function and they are as follows...

• 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 ;

printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;

swap(num1, num2) ; // calling function

printf("\nAfter swap: num1 = %d\nnum2 = %d", num1, num2);


getch() ;
}
void swap(int a, int b) // called function
{
int temp ;
temp = a ;
a=b;
b = temp ;
}

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

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 ;

printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;


swap(&num1, &num2) ; // calling function

printf("\nAfter swap: num1 = %d, num2 = %d", num1, num2);


getch() ;
}
void swap(int *a, int *b) // called function
{
int temp ;
temp = *a ;
*a = *b ;
*b = temp ;
}
Output:

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

of actual parameters num1 and num2 in calling function.


Structures in C
In C programming language, a structure is a collection of elements of the different data type. The structure

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

formal definition of structure is as follows...

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

called members of structure.

How to create structure?

To create structure in c, we use the keyword called "struct". We use the following syntax to create

structures in c programming language.

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;
} ;

Importent Points to be Remembered

Every structure must terminated with semicolon symbol (;).

"struct" is a keyword, it must be used in lowercase letters only.

Creating and Using structure variables

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...

Creating and Using structure variables in C


struct Student
{
char stud_name[30];
int roll_number;
float percentage;
} stud_1 ; // while defining structure

void main(){
struct Student stud_2; // using struct keyword

printf("Enter details of stud_1 : \n");


printf("Name : ");
scanf("%s", stud_1.stud_name);
printf("Roll Number : ");
scanf("%d", &stud_1.roll_number);
printf("Percentage : ");
scanf("%f", &stud_1.percentage);

printf("***** Student 1 Details *****\n);


printf("Name of the Student : %s\n", stud_1.stud_name);
printf("Roll Number of the Student : %i\n", stud_1.roll_number);
printf("Percentage of the Student : %f\n", stud_1.percentage);
}

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

the dot (.) operator.

Memory allocation of Structure

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

stud_1 and stud_2 are allocated with 36 bytes of memory each.

Importent Points to be Remembered

All the members of a structure can be used simultaneously.

Until variable of a structure is created no memory is allocated.

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

integer variable only.

Accessing the Address of Variables

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

statement to display memory location address of variable "marks"...

Example Code
printf("Address : %u", &marks) ;

In the above example statement %u is used to display address of marks variable. Address of any memory

location is unsigned integer value.

Declaring Pointers (Creating Pointers)

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

integer variable address.

Assigning Address to Pointer

To assign address to a pointer variable we use assignment operator with the following syntax...

pointerVariableName = & variableName ;

For example, consider the following variables declaration...

Example Program | Test whether given number is divisible by 5.


int a, *ptr ;

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 variable ptr is pointing to variable a.

Accessing Variable Value Using Pointer

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 ;

printf("Address of variable a = %u\n", ptr) ;


printf("Value of variable a = %d\n", *ptr) ;
printf("Address of variable ptr = %u\n", &ptr) ;
}

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

variable a and *ptr is used to access the value of variable a.

Memory Allocation of Pointer Variables

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.

We use pre-defined or standard library functions to allocate memory dynamically. There

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

memory due to any reason it returns NULL pointer.

Syntax

void* malloc(size_in_bytes)

Example
Example Program for malloc().
#include<stdio.h>
#include<conio.h>

int main () {

char *title;

title = (char *) malloc(15);

strcpy(title, "c programming");


printf("String = %s, Address = %u\n", title, 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

blocks to be allocated, 2. Size of each block in bytes

Syntax

void* calloc(number_of_blocks, size_of_each_block_in_bytes)

Example
Example Program for calloc().
#include<stdio.h>
#include<conio.h>
int main () {
int i, n;
int *ptr;

printf("Number of blocks to be created:");


scanf("%d",&n);

ptr = (int*)calloc(n, sizeof(int));


printf("Enter %d numbers:\n",n);
for( i=0 ; i < n ; i++ ) {
scanf("%d",&ptr[i]);
}

printf("The numbers entered are: ");


for( i=0 ; i < n ; i++ ) {
printf("%d ",ptr[i]);
}

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

allocate memory due to any reason it returns NULL pointer.

Syntax

void* realloc(*pointer, new_size_of_each_block_in_bytes)

Example
Example Program for realloc().
#include<stdio.h>
#include<conio.h>

int main () {

char *title;

title = (char *) malloc(15);

strcpy(title, "c programming");


printf("Before modification : String = %s, Address = %u\n", title, title);

title = (char*) realloc(title, 30);

strcpy(title,"C Programming Language");


printf("After modification : String = %s, Address = %u\n", title, 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;

title = (char *) malloc(15);

strcpy(title, "c programming");


printf("Before modification : String = %s, Address = %u\n", title, title);

title = (char*) realloc(title, 30);

strcpy(title,"C Programming Language");


printf("After modification : String = %s, Address = %u\n", title, title);

free(title);

return(0);

You might also like