Slide 01 DS Preliminaries
Slide 01 DS Preliminaries
Topic to be covered...
• Structure
• Union
• Pointer
• Enumerated (enum)
Structure
Objectives:
Contents:
• Introduction
• Array of Structure
• Pointer to Structure
• Nested Structure
• Passing Structure to Function
Introduction to Structure
A structure is
a convenient way of grouping several pieces of related information together
a collection of variables under a single name
Problem: – How to group together a collection of data items of different types that
are logically related to a particular entity??? (Array)
: Structure
Structure:
different data types under a single name.
The variables are called members of the structure.
The structure helps to define a user-defined data type.
Why use structure?
Enter the name, roll number, and marks of the student 1: Arun 90 91
Enter the name, roll number, and marks of the student 2: Varun 91 56
Enter the name, roll number, and marks of the student 3: Shyam 89 69
Enter the name, roll number, and marks of the student 4: Amrit 52 63
Enter the name, roll number, and marks of the student 5: Bipin 45 73
This program may fulfill the requirement of storing the information of a student
entity.
But, the program is very complex, and the complexity increase with the amount of
the input.
The elements of each of the array are stored contiguously, but all the arrays may
not be stored contiguously in the memory.
Approach-II
structure is an user defined data type available in C that allows to combine data
items of different kinds.
Approach-II
void main() {
struct student {
struct student s1={“name", 4};
char name[20];
printf("Name=%s", s1.name);
int roll;
printf("\n Roll=%d", s1.roll);
char remarks;
printf("\n Remarks=%c",s1.remarks);
float marks;
printf("\n Marks=%f", s1.marks);
};
}
Copy & Comparison of Structure
Two variables of the same structure type can be copied in the same way as
ordinary variables.
If student1 and student2 belong to the same structure, then the following
statements are valid: student1=student2; student2=student1;
However, the statements such as: student1==student2 student1!=student2
are not permitted.
If we need to compare the structure variables, we may do soby comparing
members individually.
void main() {
struct student student1={“ABC", 4,};
struct student { struct student student2; student2=student1;
printf("\nStudent2.name=%s", student2.name);
char name[20];
printf("\nStudent2.roll=%d", student2.roll);
int roll; if(strcmp(student1.name, student2.name)==0 &&
}; (student1.roll==student2.roll)) { printf("\n\n student1 and
student2 aresame."); }
}
How Structure members are stored?
The elements of a structure are always stored in contiguous memory
locations.
A structure variable reserves number of bytes equal to sum of bytes
needed to each of its members.
Array of Structure
Consider a structure as: struct student { char name[20]; int roll; char
remarks; float marks; };
If we want to keep record of 100 students, we have to make 100 structure
variables like st1, st2,…,st100.
In this situation, array of structure can be used to store the records of 100
students which is easier and efficient to handle (because loops can be
used).
Two ways to declare an array of structure:
struct personal_record {
char name[20];
struct Date birthday;
float salary;
}person;
Structure within a Structure
Here, the structure personal_record contains a member named birthday
which itself is a structure with 3 members. This is called structure within
structure.
The members contained within the inner structure can be accessed as:
person.birthday.day_of_birth,
person.birthday.month_of_birth,
person.birthday. year_of_birth
The other members within the structure personal_record are accessed
asusual:
person.name,
person.salary
Structure within a Structure
printf("Enter name:\t");
scanf("%s", person.name);
printf("\nEnter day of birthday:\t");
scanf("%d", &person.birthday.day_of_birth);
printf("\nEnter month of birthday:\t");
scanf("%d", &person.birthday.month_of_birth);
printf("\nEnter year of birthday:t");
scanf("%d", &person.birthday.year_of_birth);
printf("\nEnter salary:\t");
scanf("%f", &person.salary);
Structure within a Structure
struct Date {
int day;
int month;
int year;
};
struct Name {
char first_name[10];
char middle_name[10];
char last_name[10];
};
struct personal_record {
float salary;
struct Date birthday;
struct Name full_name;
};
Pointers to Structure
Astructure type pointer variable canbe declared as:
struct Book {
char name[20];
int pages;
float price;
};
struct Book *bptr;
However, this declaration for a pointer to structure does not allocate any
memory for a structure.
but allocates only for a pointer, so that to access structure’s members through
pointer bptr, we must allocate the memory using malloc()function.
Pointers to Structure
Now, individual structure members are accessed as:
bptr->name or (*bptr).name
bptr->pages or (*bptr).pages
bptr->price or (*bptr).price
Here, -> is called arrow operator and there must be apointer to the
structure on the left side of this operator.
struct book b, *bptr;
bptr=b;
printf("\n Enter name:\t");
scanf("%s", bptr->name);
printf("\n Enter no. of pages:\t");
scanf("%d", &bptr->pages);
printf("\n Enter price:\t");
scanf("%f", &bptr->price);
Pointers to Structure
Now the members of the structure book can be accessed in 3 ways as:
b.name bptr->name (*bptr).name
b.pages bptr->pages (*bptr).pages
b.price bptr-> price (*bptr).price
Pointers to Array of Structure
Consider a structure as follows:
struct book {
char name[20];
int pages;
float price;
};
struct book b[10], *bptr;
Then the assignment statement bptr=b; assigns the address of the 0th
element of b to bptr.
Pointers to Array of Structure
The members of b[0] can be accessed as:
bptr->name
bptr->pages
bptr->price
The following for statement can be used to print all the values of array of
structure b as:
for(bptr=b; bptr<b+10; bptr++)
printf(“%s %d %f”, bptr->name, bptr->pages, bptr- >price);
Passing Structure to a Function
Consider four cases here:
– Passing the individual members to functions
– Passing whole structure to functions
– Passing structure pointer to functions
– Passing array of structure to functions
Passing Structure to a Function
Consider four cases here:
– Passing the individual members to functions
– Passing whole structure to functions
– Passing structure pointer to functions
– Passing array of structure to functions
Passing Structure member to a
Function
Structure members can be passed to functions like ordinary variables.
Example: Let us consider a structure employee having members name, id
and salary and pass these members to a function:
struct employee {
char name[20];
int id
float salary;
};
display(emp.name, emp.id, emp.salary);
void display(char e[], int id , float sal) {
printf("\nName \t\t Id \t\t Salary\n);
printf("%s\t\t%d\t%.2f", e, id, sal);
}
Problem: Huge number of structure members
Passing the whole Structure to a
Function
Whole structure can be passed to a function by the syntax:
function_name(structure_variable_name);
The called function has the form:
return_type function_name(struct tag_name structure_variable_name)
{ … … … … …; }
display(emp);
/* function declaration */
void printBook( struct Books book );
Structures as Function Arguments
int main() {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Balguruswami");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
printBook( Book1 );
printBook( Book2 );
return 0;
}
Structures as Function Arguments
void printBook( struct Books book ) {
printf( "Book title : %s\n", book.title);
printf( "Book author : %s\n", book.author);
printf( "Book subject : %s\n", book.subject);
printf( "Book book_id : %d\n", book.book_id);
}
Output:
Book 1 title : C Programming
Book 1 author : Balguruswami
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : OOPs
Book 2 author : Balguruswami
Book 2 subject : Object Oriented Programming
Book 2 book_id : 6495700
Passing Structure Pointer to a Function
In this case, address of structure variable is passed as an actual argument
to a function.
The corresponding formal argument must be a structure type pointer
variable.
Note: Any changes made to the members in the called function are
directly reflected in the calling function.
display(&emp);
To find the address of a structure variable, place the '&'; operator before
the structure's name as follows:
struct_pointer = &Book1;
/* function declaration */
void printBook( struct Books *book );
Pointers to Structures
int main() {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Balguruswami");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
printBook( &Book1 );
printBook( &Book2 );
return 0;
}
Pointers to Structures
void printBook( struct Books *book ) {
printf( "Book title : %s\n", book->title);
printf( "Book author : %s\n", book->author);
printf( "Book subject : %s\n", book->subject);
printf( "Book book_id : %d\n", book->book_id);
}
Output:
Book 1 title : C Programming
Book 1 author : Balguruswami
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : OOPs
Book 2 author : Balguruswami
Book 2 subject : Object Oriented Programming
Book 2 book_id : 6495700
Passing an Array of Structure Type to a
Function
Passing an array of structure type to a function is similar to passing an
array of any type to a function.
That is, the name of the array of structure is passed by the calling function
which is the base address of the array of structure.
Note: The function prototype comes after the structure definition
.
Passing Array of structures to function display(emp);
struct {
unsigned int f1 : 1;
unsigned int f2 : 1;
} status2;
Bit Fields
int main( ) {
printf( "Memory size occupied by status1 : %d\n", sizeof(status1));
printf( "Memory size occupied by status2 : %d\n", sizeof(status2));
return 0;
}
Output
Memory size occupied by status1 : 8
Memory size occupied by status2 : 4
Bit Field Declaration
The declaration of a bit-field has the following form inside a structure:
struct {
type [member_name] : width ;
};
int main( ) {
Age.age = 4;
printf( "Sizeof( Age ) : %d\n", sizeof(Age) );
printf( "Age.age : %d\n", Age.age );
Age.age = 7;
printf( "Age.age : %d\n", Age.age );
Age.age = 8;
printf( "Age.age : %d\n", Age.age );
return 0;
}
Union
A union is a special data type available in C that allows to store different
data types in the same memory location.
Union can be defined with many members, but only one member can
contain a value at any given time.
Unions provide an efficient way of using the same memory location for
multiple-purpose.
Defining a Union
Use union statement in the same as it is used in defining a structure.
The union statement defines a new data type with more than one member
for the program.
The syntax of the union statement is as follows:
union [union tag] {
member definition;
member definition;
...
member definition;
} [one or more union variables];
The union tag is optional and each member definition is a normal variable
definition, such as int i; or float f; or any other valid variable definition.
At the end of the union's definition, before the final semicolon, one or
more union variables can be specified but it is optional.
Union
Example:
union Data {
int i;
float f;
char str[20];
} data;
A variable of this union data type can store an integer, a floating-point number, or a
string of characters.
It means a single variable, i.e., same memory location, can be used to store multiple
types of data.
The memory occupied by a union will be large enough to hold the largest member of
the union.
For example, in this union declaration, a variable of this type will occupy 20 bytes of
memory space because this is the maximum space which can be occupied by a
character string.
Union: Program
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( ) {
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
Output:
Memory size occupied by data : 20
Accessing Union Members
To access any member of a union, the member access operator (.) is used.
The member access operator is coded as a period between the union
variable name and the union member that need to be accessed.
Union: Program
#include <stdio.h> Output:
#include <string.h> data.i : 1917853763
union Data { data.f :
int i; 4122360580327794860452759994368.000000
float f; data.str : C Programming
char str[20];
};
int main( ) { The values of i and f members of union got
union Data data; corrupted because the final value assigned
data.i = 10; to the variable has occupied the memory
data.f = 220.5; location and this is the reason that the value
strcpy(data.str, "C Programming"); of str member is getting printed very well.
printf("data.i : %d\n", data.i);
printf("data.f : %f\n", data.f);
printf("data.str : %s\n", data.str);
return 0;
}
Union: Program
#include <stdio.h> Output:
#include <string.h> data.i : 10
union Data { data.f : 220.500000
int i; data.str : C Programming
float f;
char str[20];
};
int main( ) {
union Data data;
data.i = 10;
printf( "data.i : %d\n", data.i);
data.f = 220.5;
printf( "data.f : %f\n", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %s\n", data.str);
return 0;
}
Pointers
Pointers: Introduction
A pointer is a variable that represents the location (rather than the value) of
a data item.
They have a number of useful applications.
Enables us to access a variable/ data item that is defined outside the function.
Can be used to pass information back and forth between functions.
More efficient in handling data tables.
Reduces the length and complexity of a program.
Sometimes also increases the execution speed.
Pointers: Basic Concept
Within the computer memory, every stored data item occupies one or
more contiguous memory cells/ bytes.
The number of memory cells required to store a data item depends on its
type (char, int, double, etc.).
xyz variable
50 value
1001 address
Contd.
During execution of the program, the system always associates the
name xyz with the address 1001.
The value 50 can be accessed by using either the name xyz or the address
1001.
Since memory addresses are simply numbers, they can be assigned to
some variables which can be stored in memory.
Such variables that hold memory addresses are called pointers.
Since a pointer is a variable, its value is also stored in some memory
location.
Contd.
Suppose we assign the address of xyz to a variable ptr.
ptr is said to point to the variable xyz.
&(a+b)
Pointing at expression.
Example
#include <stdio.h>
int main() {
int a;
float b, c;
double d;
char ch;
a = 4 * (c + 5) ;
p = &c;
b = 4 * (*p + 5) ;
printf (“a=%d b=%d \n”, a, b) ;
return 0;
}
Example
#include <stdio.h> Output:
int main() { *&xx
int x, y;
10 is stored in location 3221224908
int *ptr;
10 is stored in location 3221224908
ptr=&x;
10 is stored in location 3221224908
x = 10 ; &x&*ptr
10 is stored in location 3221224908
ptr = &x ;
3221224908 is stored in location
y = *ptr ;
3221224900
printf (“%d is stored in location %u \n”, x, &x) ;
10 is stored in location 3221224904
printf (“%d is stored in location %u \n”, *&x, &x) ;
printf (“%d is stored in location %u \n”, *ptr, ptr) ;
Now x = 25
printf (“%d is stored in location %u \n”, y, &*ptr) ;
printf (“%u is stored in location %u \n”, ptr, &ptr) ;
Address of x: 3221224908
printf (“%d is stored in location %u \n”, y, &y) ;
Address of y: 3221224904
*ptr = 25;
Address of ptr: 3221224900
printf (“\nNow x = %d \n”, x);
return 0;
}
Pointer Expressions
Like other variables, pointer variables can be used in expressions.
If p1 and p2 are two pointers, the following statements are valid:
sum = *p1 + *p2 ;
prod = *p1 * *p2 ;
prod = (*p1) * (*p2) ;
*p1 = *p1 + 2;
x = *p1 / *p2 + 5 ;
Contd.
What are allowed in C?
Add an integer to a pointer.
If p1 and p2 are both pointers to the same array, them p2–p1 gives
the number of elements between p1 and p2.
What are not allowed?
Add two pointers.
p1 = p1 + p2 ;
Multiply / divide a pointer in an expression.
p1 = p2 / 5 ;
p1 = p1 – p2 * 10 ;
Scale Factor
It has been seen that an integer value can be added to or subtracted
from a pointer variable.
int *p1, *p2 ;
int i, j;
:
p1 = p1 + 1 ;
p2 = p1 + j ;
p2++ ;
p2 = p2 – (i + j) ;
In reality, it is not the integer value which is added/subtracted, but
rather the scale factor times the value.
Contd.
Data Type Scale Factor
char 1
int 4
float 4
double 8
#include <stdio.h>
int main() {
printf (“Number of bytes occupied by int is %d \n”, sizeof(int));
printf (“Number of bytes occupied by float is %d \n”, sizeof(float));
printf (“Number of bytes occupied by double is %d \n”, sizeof(double));
printf (“Number of bytes occupied by char is %d \n”, sizeof(char));
return 0;
}
Output:
Number of bytes occupied by int is 4
Number of bytes occupied by float is 4
Number of bytes occupied by double is 8
Number of bytes occupied by char is 1
Passing Pointers to a Function
Pointers are often passed to a function as arguments.
Allows data items within the calling program to be accessed by
array.
The compiler also defines the array name as a constant pointer to
requires 4 bytes.
Element Value Address
x[0] 1 2500
x[1] 2 2504
x[2] 3 2508
x[3] 4 2512
x[4] 5 2516
Contd.
x &x[0] 2500 ;
print(Complex *a) {
printf("(%f, %f)\n", a->real, a->imag);
} int main() {
Complex x={10.0, 3.0}, y={-20.0, 4.0};
print(&x); print(&y);
(10.000000, 3.000000) swap(&x, &y);
(-20.000000, 4.000000) print(&x); print(&y);
(-20.000000, 4.000000) return 0;
(10.000000, 3.000000) }
A Warning
When using structure pointers, we should take care of operator precedence.
Member operator “.” has higher precedence than “*”.
int main() {
struct complex a, b, c;
scanf (“%f %f”, &a.re, &a.im);
scanf (“%f %f”, &b.re, &b.im);
add (&a, &b, &c) ;
printf (“\n %f %f”, c.re, c.im);
return 0;
}
Pointer to Pointer
Example:
int **p;
p=(int **) malloc(3 * sizeof(int *));
p[0]
p int ** int *
p[1] int *
int *
p[2]
Pointers and 2-D arrays
#include<stdio.h> Output
int main() {
int a[3][3], i, j, *p; Enter elements of 2-D array
printf ("Enter elements of 2-D array"); 123456789
for (i=0; i<3; i++) Elements of 2-D array are...
for (j=0; j<3; j++) 123
scanf ("%d", &a[i] [j]); 456
p = &a[0][0]; 789
printf ("Elements of 2-D array are...\n");
for (i=0; i<3; i++){
for (j=0; j<3; j++)
printf ("%d \t", *(p+i*3+j));
printf ("\n");
}
return 0;
}
Pointers and 2-D arrays
int (*p)[10];
Here p is a pointer that can point to an array of 10 integers.
In this case, the type of p is a pointer to an array of 10 integers.
A pointer that points to the 0th element of an array and a pointer that
points to the whole array are totally different.
Pointers and 2-D arrays
#include<stdio.h> Output
int main() {
int *p; Address of p = 2293296
int (*parr)[5]; // pointer to an array of 5 integers Address of parr = 2293296
int my_arr[5];
p = my_arr; After incrementing p and parr
parr = my_arr; by 1
printf("Address of p = %u\n", p);
printf("Address of parr = %u\n", parr ); Address of p = 2293300
p++; Address of parr = 2293316
parr++;
printf("\nAfter incrementing p and parr by 1 \n\n");
printf("Address of p = %u\n", p );
printf("Address of parr = %u\n", parr );
printf("Address of parr = %u\n", *parr );
return 0;
}
Pointers and 2-D arrays
int arr[3][4] = {
{11, 22, 33, 44},
{55, 66, 77, 88},
{11, 66, 77, 44}
};
A 2-D array is actually a 1-D array in which each element is itself a 1-D
array.
So arr is an array of 3 elements where each element is a 1-D array of 4
integers.
Pointers and 2-D arrays
We know that the name of a 1-D array is a constant pointer to the 0th
element.
In the case, of a 2-D array, 0th element is a 1-D array.
Hence the type or base type of arr is a pointer to an array of 4 integers.
Since pointer arithmetic is performed relative to the base size of the
pointer.
In the case of arr, if arr points to address 2000 then arr + 1 points to address
2016 (i.e 2000 + 4*4).
Pointers and 2-D arrays
We know that the name of the array is a constant pointer that points to
the 0th element of the array.
In the case of a 2-D array, 0th element is a 1-D array.
So the name of the array in case of a 2-D array represents a pointer to the 0th
1-D array.
Therefore in this case arr is a pointer to an array of 4 elements.
If the address of the 0th 1-D is 2000, then according to pointer arithmetic
(arr + 1) will represent the address 2016,
similarly (arr + 2) will represent the address 2032.
Pointers and 2-D arrays
In general
(arr + i) points to ith 1-D array.
As we know that dereferencing a pointer to an array gives the base address of the array.
So dereferencing arr we will get *arr,
base type of *arr is (int*).
Similarly, on dereferencing arr+1 we will get *(arr+1).
Again it is important to note that type (arr + i) and *(arr+i) points to same
address but their base types are completely different.
The base type of (arr + i) is a pointer to an array of 4 integers, while the base type of
*(arr + i) is a pointer to int or (int*).
Pointers and 2-D arrays
Use arr to access individual elements of a 2-D array:
Since *(arr + i) points to the base address of ith 1-D array and it is of base type
pointer to int, by using pointer arithmetic we should able to access elements of ith
1-D array.
*(arr + i) points to the address of the 0th element of the 1-D array. So,
*(arr + i) + 1 points to the address of the 1st element of the 1-D array
*(arr + i) + 2 points to the address of the 2nd element of the 1-D array
Hence
*(arr + i) + j points to the base address of jth element of ith 1-D array.
On dereferencing *(arr + i) + j, we will get the value of jth element of ith 1-D
array: *( *(arr + i) + j)
By using this expression we can find the value of jth element of ith 1-D array.
Furthermore, the pointer notation *(*(arr + i) + j) is equivalent to the subscript
notation.
Pointers and 2-D arrays
Output
Address of 0 th array 2686736
#include<stdio.h> arr[0][0]=11
int main() { arr[0][1]=22
int arr[3][4] = { arr[0][2]=33
{11, 22, 33, 44}, arr[0][3]=44
{55, 66, 77, 88},
{11, 66, 77, 44} Address of 1 st array 2686752
}; arr[1][0]=55
int i, j; arr[1][1]=66
for(i = 0; i < 3; i++) { arr[1][2]=77
printf("Address of %d th array %u \n",i , *(arr + i)); arr[1][3]=88
for(j = 0; j < 4; j++)
printf("arr[%d][%d]=%d\n", i, j, *( *(arr + i) + j) ); Address of 2 nd array 2686768
printf("\n\n"); arr[2][0]=11
} arr[2][1]=66
return 0; arr[2][2]=77
} arr[2][3]=44
Dynamic Memory Allocation
Basic Idea
Many a time it has been observed data is dynamic in nature.
Amount of data cannot be predicted beforehand.
Number of data item keeps changing during program execution.
Such situations can be handled more easily and effectively using
dynamic memory management techniques.
Contd.
C language requires the number of elements in an array to be specified
at compile time.
Often leads to wastage of memory space or program failure.
Dynamic Memory Allocation
Memory space required can be specified at the time of execution.
C supports allocating and freeing memory dynamically using library
functions.
Memory Allocation Process in C
bytes is reserved.
The address of the first byte of the allocated memory is
p
assigned to the pointer p of type int.
available.
If it fails, malloc returns NULL.
Example
#include <stdio.h> printf("Input heights for %d students \n", n);
int main() { for(i=0; i<n; i++)
int i, n; scanf("%f", &height[i]);
float *height;
float sum=0,avg; for(i=0; i< n ;i++)
sum+=height[i];
printf("Input the number of students. \n");
scanf("%d", &n); avg=sum/(float) n;
int main() {
int row = 3, col = 4, i, j, count; Output
int* arr[row];
for (i = 0; i < row; i++) 1 2 3 4 5 6 7 8 9 10 11 12
arr[i] = (int*)malloc(col * sizeof(int));
// Note that arr[i][j] is same as *(*(arr+i)+j)
count = 0;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
arr[i][j] = ++count;
// Or *(*(arr+i)+j) = ++count
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
printf("%d ", arr[i][j]);
// Free the dynamically allocated memory
for (int i = 0; i < row; i++)
free(arr[i]);
return 0;
}
Program
Using pointer to a pointer
int main() {
int row = 3, col = 4, i, j, count; Output
bytes is reserved.
The address of the first byte of the allocated memory is
p
assigned to the pointer p of type int.
General format:
free (ptr) ;
where ptr is a pointer to a memory block which has been already
created using malloc.
2-D Array Allocation
#include <stdio.h> void read_data(int **p, int h, int w)
#include <stdlib.h> {
int i, j;
int **allocate(int h, int w) { for(i=0; i<h; i++)
int **p; Allocate array for(j=0; j<w; j++)
int i, j; of pointers scanf ("%d", &p[i][j]);
}
p=(int **) calloc(h, sizeof (int *) );
for(i=0; i<h; i++) Elements accessed
p[i]=(int *) calloc(w, sizeof (int)); like 2-D array
return(p); elements.
} Allocate array of
integers for each
row
2-D Array: Contd.
void print(int **p, int h, int w) { int main() { Enter m and n value:
int i, j; int **p; 33
for(i=0; i<h; i++) { int m, n; 123
for(j=0; j<w; j++) printf("Enter m and n value:\n"); 456
printf("%5d ", p[i][j]); scanf("%d%d", &m, &n); 789
printf("\n"); p=allocate(m, n);
} read_data(p, m, n); The array read as
} printf("\n The array read as \n"); 1 2 3
print(p, m, n); 4 5 6
} 7 8 9
Difference: malloc() & calloc()
Description:
malloc() is an abbreviation for memory calloc() is an abbreviation for contiguous
allocation. It is a predefined function allocation. It is an advancement over malloc()
defined in the stdlib.h header file. function.
Use:
malloc() is used to allocate memory during calloc() is used to allocate multiple blocks of
the runtime of a program. memory of the same size dynamically.
Memory Initialization:
Memory allocated is uninitialized that Memory is initialized with zero.
means it has garbage values.
Purpose:
malloc() is used for creating structures. calloc() is for creating dynamic arrays.
How?
By using the realloc() function.