Introduction To Problem Solving Using C - Unit 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

MODULE – IV

Arrays: Declaration and Initialization of one and two dimensional arrays


– Multidimensional array – dynamic arrays - Character arrays and strings.
Structure: Defining and processing. Structure initialization Operations on
individual members Arrays of structure, Arrays within Structure, Structure
and Functions- Passing to a function.

Array
Array is the group of identical elements organized in a single variable. An array
is a collection of data that holds fixed number of values of same type. For example: if
you want to store marks of 100 students, you can create an array for it.

int mark[100];

Array Declaration:
Array has to be declared before using it in C Program. Array is nothing but the collection of elements of
similar data types.
Syntax: <data type> array name [size1][size2] .......... [sizen];

Syntax Parameter Significance

Data type Data Type of Each Element of the array

Array name Valid variable name

Size Dimensions of the Array


Array declaration requirements
Requirement Explanation
Data Type specifies the type of the array. We can compute the sizerequired
Data Type
for storing the single cell of array.

Valid identifier is any valid variable or name given to the array. Using
Valid Identifier this identifier name array can be accessed.

Size of Array It is maximum size that array can have.


What does Array Declaration tell to Compiler?
1. Type of the Array
2. Name of the Array
3. Number of Dimension
4. Number of Elements in Each Dimension

Arrays are of three


types:

i. One-dimensional arrays
int mark[5] = {19, 10, 8, 17, 9};
Arrays have 0 as the first index not 1. In this example, mark[0]
If the size of an array is n, to access the last element, (n-1) index is used. In this
example, mark[4]

Single / One Dimensional Array:


1. Single or One Dimensional array is used to represent and store data in a linear form.
2. Array having only one subscript variable is called One-Dimensional array
3. It is also called as Single Dimensional Array or Linear Array
Single Dimensional Array Declaration and initialization:
Syntax for declaration: <data type> <array name> [size];
Examples for declaration: int iarr[3]; char carr[20]; float farr[3];
Syntax for initialization: <data type> <array name> [size] = {val1, val2, …, valn};
Examples for initialization:
int iarr[3] = {2, 3, 4};
char carr[20] = “program”;
float farr[3] = {12.5, 13.5, 14.5};
Different Methods of Initializing 1-D Array
Whenever we declare an array, we initialize that array directly at compile time.
Initializing 1-D Array is called as compiler time initialization if and only if we assign certain set ofvalues
to array element before executing program. i.e. at compilation time.

Here we are learning the different ways of compile time initialization of an array.
Ways of Array Initializing 1-D Array:
1. Size is Specified Directly
2. Size is Specified Indirectly
Method 1: Array Size Specified Directly
In this method, we try to specify the Array Size directly.
int num [5] = {2,8,7,6,0};
In the above example we have specified the size of array as 5 directly in the initialization
statement. Compiler will assign the set of values to particular element of the array.
num[0] = 2; num[1] = 8; num[2] = 7; num[3] = 6; num[4] = 0;
As at the time of compilation all the elements are at specified position So This initializationscheme is
Called as “Compile Time Initialization“.
Graphical Representation:

Method 2: Size Specified Indirectly


In this scheme of compile time Initialization, We do not provide size to an array but instead weprovide
set of values to the array.
int num[ ] = {2,8,7,6,0};
Explanation:
1. Compiler Counts the Number Of Elements Written Inside Pair of Braces and Determinesthe Size
of An Array.
2. After counting the number of elements inside the braces, The size of array is consideredas 5
during complete execution.
3. This type of Initialization Scheme is also Called as “Compile Time Initialization“
Example Program
#include <stdio.h>
int main()
int num[] = {2,8,7,6,0};
int i;
for (i=0;i<5;i++) {
printf(“\n Array Element num [%d] = %d”,i, num[i]); }
return 0; }
Output:

Array Element num[0] = 2


Array Element num[1] = 8
Array Element num[2] = 7
Array Element num[3] = 6
Array Element num[4] = 0

Accessing Array
1. We all know that array elements are randomly accessed using the subscript variable.
2. Array can be accessed using array-name and subscript variable written inside pair ofsquare
brackets [ ].
Consider the below example of an array

In this example we will be accessing array like this


arr[3] = Forth Element of Array
arr[5] = Sixth Element of Array
whereas elements are assigned to an array using below way
arr[0] = 51; arr[1] = 32; arr[2] = 43; arr[3] = 24; arr[4] = 5; arr[5] =26;
Example Program1: Accessing array
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[] = {51,32,43,24,5,26};
int i;
for(i=0; i<=5; i++) {
printf("\nElement at arr[%d] is %d",i,arr[i]);
}
getch();
}
Output:
Element at arr[0] is 51

Element at arr[1] is 32

Element at arr[2] is 43

Element at arr[3] is 24

Element at arr[4] is 5

Element at arr[5] is 26

How a[i] Works?


We have following array which is declared like int arr[] = { 51,32,43,24,5,26};
As we have elements in an array, so we have track of base address of an array. Below thingsare
important to access an array.

Expression Description Example

Arr It returns the base address of an array Consider 2000

*arr It gives zeroth element of an array 51


Expression Description Example

*(arr+0) It also gives zeroth element of an array 51

*(arr+1) It gives first element of an array 32

So whenever we tried accessing array using arr[i] then it returns an element at the location*(arr
+ i)
Accessing array a[i] means retrieving element from address (a + i).
Example Program2: Accessing array
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[] = {51,32,43,24,5,26};
int i;
for(i=0; i<=5; i++) {
printf("\n%d %d %d %d",arr[i],*(i+arr),*(arr+i),i[arr]);
}
getch();
}
Output:

51 51 51 51
32 32 32 32
43 43 43 43
24 24 24 24
5 5 5 5
26 26 26 26

Operations with One Dimensional Array


1. Deletion – Involves deleting specified elements form an array.
2. Insertion – Used to insert an element at a specified position in an array.
3. Searching – An array element can be searched. The process of seeking specific
elements in an array is called searching.
4. Merging – The elements of two arrays are merged into a single one.
5. Sorting – Arranging elements in a specific order either in ascending or in descendingorder.
Example Programs:

1. C Program for deletion of an element from the specified location


from an Array
#include<stdio.h>int main()
{
int arr[30], num, i, loc;
printf("\nEnter no of elements:");
scanf("%d", &num);
//Read elements in an array
printf("\nEnter %d elements :", num);
for (i = 0; i < num; i++)
{
scanf("%d", &arr[i]);
}
//Read the location
printf("\nLocation of the element to be deleted :");

scanf("%d", &loc);
/* loop for the deletion */
while (loc < num)
{
arr[loc - 1] = arr[loc];
loc++;
}
num--; // No of elements reduced by 1
//Print Array
for (i = 0; i < num; i++)

printf("\n %d", arr[i]);

return (0);
}
Output:
Enter no of elements: 5 Enter 5 elements:
34178

Location of the element to be deleted: 3

3478
1. C Program to delete duplicate elements from an array
int main()
{
int arr[20], i, j, k, size;

printf("\nEnter array size: ");

scanf("%d", &size);

printf("\nAccept Numbers: ");

for (i = 0; i < size; i++)

scanf("%d", &arr[i]);

}
printf("\nArray with Unique list: ");
for (i = 0; i < size; i++)
{
for (j = i + 1; j < size;)
{
if (arr[j] == arr[i])
{
for (k = j; k < size; k++)
{
arr[k] = arr[k + 1];
}
size--;
}
else
j++;
}
}
for (i = 0; i < size; i++)

printf("%d ", arr[i]);

return (0);
}
Output:
Enter array size: 5 Accept Numbers: 1
3453
Array with Unique list: 1 3 4 5
2. C Program to insert an element in an array
#include<stdio.h>int main()
{

int arr[30], element, num, i, location;

printf("\nEnter no of elements:");

scanf("%d", &num);
for (i = 0; i < num; i++)
{
scanf("%d", &arr[i]);
}
printf("\nEnter the element to be inserted:");
scanf("%d", &element);
printf("\nEnter the location");
scanf("%d", &location);
//Create space at the specified location
for (i = num; i >= location; i--)
{
arr[i] = arr[i - 1];
}
num++;
arr[location - 1] = element;
//Print out the result of insertion
for (i = 0; i < num; i++)
printf("n %d", arr[i]);
return (0);
}
Output:
Enter no of elements: 51 2 3 4 5
Enter the element to be inserted: 6
Enter the location: 2
162345
3. C Program to search an element in an array
#include<stdio.h>int main()
{
int a[30], ele, num, i;
printf("\nEnter no of elements:");
scanf("%d", &num);
printf("\nEnter the values :");
for (i = 0; i < num; i++)
{
scanf("%d", &a[i]); }
//Read the element to be searched
printf("\nEnter the elements to be searched :");
scanf("%d", &ele);
//Search starts from the zeroth location
i = 0;
while (i < num && ele != a[i])
{
i++;
}
//If i < num then Match found
if (i < num)
{
printf("Number found at the location = %d", i + 1);
}
else
{
printf("Number not found");

return (0);
}
Output:
Enter no of elements: 5

11 22 33 44 55
Enter the elements to be searched: 44
Number found at the location = 4
4. C Program to copy all elements of an array into another array
#include<stdio.h>int main()
{
int arr1[30], arr2[30], i, num;
printf("\nEnter no of elements:");
scanf("%d", &num);
//Accepting values into Array
printf("\nEnter the values:");
for (i = 0; i < num; i++)
{
scanf("%d", &arr1[i]);
}
/* Copying data from array 'a' to array 'b */
for (i = 0; i < num; i++)
{
arr2[i] = arr1[i];
}
//Printing of all elements of array

printf("The copied array is:");

for (i = 0; i < num; i++)


printf("\narr2[%d] = %d", i, arr2[i]);return (0);
}
Output:
Enter no of elements: 5
Enter the values: 11 22 33 44 55
The copied array is: 11 22 33 44 55
5. C program to merge two arrays in C Programming
#include<stdio.h>int main()
{
int arr1[30], arr2[30], res[60];
int i, j, k, n1, n2;
printf("\nEnter no of elements in 1st array:");
scanf("%d", &n1);
for (i = 0; i < n1; i++)
{
scanf("%d", &arr1[i]);
}
printf("\nEnter no of elements in 2nd array:");
scanf("%d", &n2);
for (i = 0; i < n2; i++)
{
scanf("%d", &arr2[i]);
}
i = 0;
j = 0;
k = 0;
/ Merging starts
while (i < n1 && j < n2)

if (arr1[i] <= arr2[j])

{
res[k] = arr1[i];i++;
k++;
}
Else
{
res[k] = arr2[j];

k++;
j++;
}
}
/*Some elements in array 'arr1' are still remaining where as the array'arr2' is exhausted*/
while (i < n1)
{
res[k] = arr1[i];
i++;
k++;
}
/*Some elements in array 'arr2' are still remaining where as the array'arr1' is exhausted */
while (j < n2)
{
res[k] = arr2[j];
k++;
j++;
}
//Displaying elements of array 'res'

printf("\nMerged array is:");


for (i = 0; i < n1 + n2; i++)
printf("%d ", res[i]);
return (0);
}
Enter no of elements in 1st array: 4

11 22 33 44
Enter no of elements in 2nd array: 3
10 40 80
Merged array is: 10 11 22 33 40 44 80

ii. Two dimensional array


The 2D array is organized as matrices which can be represented as the collection of
rows andcolumns.
The syntax to declare the 2D array is given
below. data_type
array_name[rows][columns];
example
int rain[4][3];
the above example will create 4rows and 3 columns at main memory. So we can store
up to 12 integers.
Row\Column 1 2 3
1
2
3
4

Multidimensional arrays

In C programming, you can create an array of arrays known as multidimensional array.


For example,
float x[3][4];
Here, x is a two-dimensional (2d) array. The array can hold 12 elements. You can
think the arrayas table with 3 row and each row has 4 column.
Similarly, you can declare a three-dimensional (3d) array. For
example, float y[2][4][3];
Here, The array y can hold 24 elements.
Example : Each 2 elements have 4 elements, which makes 8 elements and each 8
elements can have 3 elements. Hence, the total number of elements is 24.

/* ................... Largest Number among N numbers */


#include<std
io.h>
#include<co
nio.h> void
main()
{
int
a[100],i,l
,n;
clrscr();
printf("Enter N
value ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
l=a[1];
for(i=2;i<=n;i++)
{
if(a[i]>l)
l=a[i];
}
printf("Large =
%d",l);getch();
}

/* Output */

Enter Number of numbers 7


45 67 78 136 12 34 67

Large = 136

Arrays as Function arguments:


Passing array to function:
Array can be passed to function by two ways:
1. Pass Entire array
2. Pass Array element by element
1. Pass Entire array
Here entire array can be passed as a argument to function.
Function gets complete access to the original array.
While passing entire array address of first element is passed to function, any changes madeinside
function, directly affects the Original value.
Function Passing method : “Pass by Address“
2. Pass Array element by element
Here individual elements are passed to function as argument.
Duplicate carbon copy of Original variable is passed to function.
So any changes made inside function do not affect the original value.
Function doesn‟t get complete access to the original array element.
Function passing method is “Pass by Value“
Passing entire array to function:
Parameter Passing Scheme : Pass by Reference
Pass name of array as function parameter.
Name contains the base address i.e. ( Address of 0th element )
Array values are updated in function.
Values are reflected inside main function also.
Example Program #1:
#include<stdio.h>
#include<conio.h>
void fun(int arr[ ])
{
int i;
for(i=0;i< 5;i++)
arr[i] = arr[i] + 10;
}
void main( )
{
int arr[5],i;
clrscr();
printf("\nEnter the array elements : ");
for(i=0;i< 5;i++)
scanf("%d",&arr[i]);
printf("\nPassing entire array..... ");
fun(arr); // Pass only name of array
for(i=0;i< 5;i++)
printf("\nAfter Function call a[%d] : %d",i,arr[i]);
getch();
}

Output :
Enter the array elements : 1 2 3 4 5
Passing entire array .....
After Function call a[0] : 11
After Function call a[1] : 12
After Function call a[2] : 13
After Function call a[3] : 14
After Function call a[4] : 15

Passing array element by element to function:


1. Individual element is passed to function using Pass By Value parameter passing scheme
2. An original Array element remains same as Actual Element is never passed to Function.Thus
function body cannot modify Original Value.
3. Suppose we have declared an array „arr[5]‟ then its individual elements are
arr[0],arr[1]…arr[4]. Thus we need 5 function calls to pass complete array to a function.
Consider an array int arr[5] = {11, 22, 33, 44, 55};

Iteration Element Passed to Function Value of Element


1 arr[0] 11
2 arr[1] 22
3 arr[2] 33
4 arr[3] 44
5 arr[4] 55

Example Program #1:


#include< stdio.h>
#include< conio.h>
void fun(int num)
{
printf("\nElement : %d",num);
}
void main() {int arr[5],i;
clrscr();
printf("\nEnter the array elements : ");for(i=0;i< 5;i++)
scanf("%d",&arr[i]);
printf("\nPassing array element by element ........................................ ");
for(i=0;i< 5;i++)fun(arr[i]);
getch();
}
Output:
Enter the array elements : 1 2 3 4 5Passing array element by
element.....
Element : 1
Element : 2
Element : 3
Element : 4
Element : 5

Character Array or C Strings


The string can be defined as the one-dimensional array of characters terminated by a null ('\0').
The character array or the string is used to manipulate text such as word or sentences. Each character in
the array occupies one byte of memory, and the last character must always be 0. The termination
character ('\0') is important in a string since it is the only way to identify where the string ends. When
we define a string as char s[10], the character s[10] is implicitly initialized with the null in the memory.

There are two ways to declare a string in c language.


 By char array
 By string literal
Let's see the example of declaring string by char array in C language.

char ch[10]={'c', 'o', 'm', 'p', 'u', 't', 'e', 'r', ''\0'};
or
char ch[10]="computer”
As we know, array index starts from 0, so it will be represented as in the figure given below.

Index 0 1 2 3 4 5 6 7 8
Value c o m p u t e r \0
ch

Example
Let's see an example of counting the number of vowels in a string.
#include<stdio.h>
void main ()
{
char s[11] = "computer";
int i = 0;
int count = 0;
while(i<11)
{
if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o')
{
count ++;
}
i++;
}
Output
printf("The number of vowels %d",count);
}
Output :The number of vowels 3

STRINGS

A string is a sequence of character enclosed with in double quotes (“”) but ends with
\0. The compiler puts \0 at the end of string to specify the end of the string.
To get a value of string variable we can use the two different types of formats.
Using scanf() function as: scanf(“%s”, string variable);

Using gets() function as : gets(string variable);

STRING HANDLING FUNCTIONS

C library supports a large number of string handling functions. Those functions are stored underthe header
file string.h in the program.

Let us see about some of the string handling functions.


(i) strlen() function
strlen() is used to return the length of the string , that means counts the number of
characters present in a string.
Syntax
integer variable = strlen (string variable);
Example:
#include<stdio.h>#include<conio.h>void main()
{
char str[20];int
strlength;clrscr();
printf(‚Enter String:‛);gets(str);
strlength=strlen(str);
printf(‚Given String Length Is: %d‛, strlength);getch();
}
Output:
Enter String
Welcome
Given String Length Is:7

(ii) strcat() function


The strcat() is used to concatenate two strings. The second string will be appended tothe end of
the first string. This process is called concatenation.
Syntax
strcat (StringVariable1, StringVariable 2);
Example:
#include<stdio.h>
#include<conio.h> void
main()
{
char str1[20],str2[20];clrscr();
printf(‚Enter First String:‛);
scanf(‚%s‛,str1);
printf(‚Enter Second String:‛);
scanf(‚%s‛,str2);
printf(‚ Concatenation String is:%s‛, strcat(str1,str2));getch();
}
Output:
Enter First String:Good
Enter Second String
Morning
Concatenation String is: GoodMorning
(iii) strcmp() function
strcmp() function is used to compare two strings. strcmp() function does a case sensitive
comparison between two strings. The two strings are compared character by character until there is a
mismatch or end of one of the strings is reached (whichever occurs first). If the two strings are identical,
strcmp( ) returns a value zero. If they‟re not, it returns the numeric difference between the ASCII values
of the first non-matching pairs of characters.

Syntax
strcmp(StringVariable1, StringVariable2);
Example:
#include<stdio.h>
#include<conio.h> void
main()
{
char str1[20], str2[20];int res;
clrscr();
printf(‚Enter First String:‛);
scanf(‚%s‛,str1);
printf(‚Enter Second String:‛);
scanf(‚%s‛,str2);
res = strcmp(str1,str2);
printf(‚ Compare String Result is:%d‛,res);

getch();
}
Output:
Enter First StringGood
Enter Second StringGood
Compare String Result is: 0

(iv) strcmpi() function

strcmpi() function is used to compare two strings. strcmpi() function is not case sensitive.

Syntax
strcmpi(StringVariable1, StringVariable2);
Example:
#include<stdio.h>
#include<conio.h> void
main()
{
char str1[20], str2[20];

int res;
clrscr();
printf(‚Enter First String:‛);
scanf(‚%s‛,str1);
printf(‚Enter Second String:‛);
scanf(‚%s‛,str2);
res = strcmpi(str1,str2);
printf(‚ Compare String Result is:%d‛,res);getch();
}
Output:
Enter First String
WELCOME
Enter Second String
welcome
Compare String Result is: 0
(v) strcpy() function:

strcpy() function is used to copy one string to another. strcpy() function copy the contents ofsecond
string to first string.

Syntax
strcpy(StringVariable1, StringVariable2);

Example:
#include<stdio.h>
#include<conio.h> void
main()
{
char str1[20], str2[20];int res;
clrscr();
printf(‚Enter First String:‛);
scanf(‚%s‛,str1);
printf(‚Enter Second String:‛);
scanf(‚%s‛,str2); strcpy(str1,str2)
printf(‚ First String is:%s‛,str1);printf(‚ Second
String is:%s‛,str2);getch();
}
Output:
Enter First StringHello
Enter Second String
welcome
First String is: welcomeSecond
String is: welcome
(vi) strlwr () function:
This function converts all characters in a given string from uppercase to lowercase letter.
Syntax
strlwr(StringVariable);

Example:

#include<stdio.h>
#include<conio.h> void
main()
{
char str[20];
clrscr();
printf(‚Enter String:‛);gets(str);
printf(‚Lowercase String : %s‛, strlwr(str));getch();
}
Output:
Enter String
WELCOME
Lowercase String : welcome
(vii) strrev() function:
strrev() function is used to reverse characters in a given string.

Syntax

strrev(StringVariable);

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
char str[20];
clrscr();
printf(‚Enter String:‛);gets(str);
printf(‚Reverse String : %s‛, strrev(str));getch();
}
Output:
Enter String
WELCOME
Reverse String : emoclew
(viii) strupr() function:
strupr() function is used to convert all characters in a given string from lower case to
uppercase letter.

Syntax

strupr(Stringvariable);

Example:

#include<stdio.h>
#include<conio.h> void
main()
{
char str[20];
clrscr();
printf(‚Enter String:‛);gets(str);
printf(‚Uppercase String : %s‛, strupr(str));getch();
}
Output:
Enter String
welcome
Uppercase String : WELCOME

Structures
A structure is a user defined data type in C. A structure creates a data type that can be used to
groupitems of possibly different types into a single type.
Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a new
datatype, with more than one member. The format of the struct statement is as follows −
struct [structure
tag] {member
definition;
member
definition;
...
member definition;
} [one or more structure variables];
The structure 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 structure's
definition, before the final semicolon, you can specify one or more structure variables but it is
optional. Here is the way we would declare the Book structure −
struct Books
{ char
title[50];
char
author[50]
;
char
subject[100];
int book_id;
} book;
How to access structure elements?
Structure members are accessed using dot (.) operator.
Syntax

STRUCTURE_Variable.STRUCTURE_Members

#include<st
dio.h>struct
Point
{
int x, y;
};
void main()
{
struct Point p1 = {0, 1};
/* Accessing members of point
p1 */p1.x = 20;
printf ("x = %d, y = %d", p1.x, p1.y);
}

Output:
x = 20, y = 1
Structure Initialization
Like variables, structures can also be initialized at the compile time.
Example
main()
{
struct
{
int rollno; int
attendance;
}
s1={786, 98};
}

The above example assigns 786 to the rollno and 98 to the attendance.Structure

variable can be initialized outside the function also.

Example

main()
{
struct student
{
int rollno; int
attendance;
};
struct student s1={786, 98};struct
student s2={123, 97};
}
Note:
Individual structure members cannot be initialized within the template. Initialization ispossible
only with the declaration of structure members.

Nested Structures or Structures within Structures


Structures can also be nested. i.e A structure can be defined inside another structure.

Example

struct employee
{
int empid;
char empname[20];
int basicpay;int
da;
int hra;int
cca;
} e1;

In the above structure, salary details can be grouped together and defined as a
separate structure.

Example

struct employee
{
int empid;
char empname[20];
struct
{
int basicpay;int
da;
int hra;int
cca;
} salary;
} e1;
The structure employee contains a member named salary which itself is another structure
that contains four structure members. The members inside salary structure can be referred
as below:

e1.salary.basicpay
e1.salary.da;
e1.salary.hra;
e1.salary.cca;
However, the inner structure member cannot be accessed without the inner structure variable.

Example

e1.basicpay
e1.da e1.hra
e1.cca
are invalid statements
Moreover, when the inner structure variable is used, it must refer to its inner structure member.
If it doesn‟t refer to the inner structure member then it will be considered as an error.

Example

e1.salary (salary is not referring to any inner structure member. Hence it is wrong)

Array of structures
Like other primitive data types, we can create an array of structures.

Example
struct Point
{
int x, y;
};

void main()
{
// Create an array of structures
struct Point p[10];

// Access array members


p[0].x = 10;
p[0].y = 20;
p[1].x = 12;
p[1].y = 25;

printf("%d %d", p[0].x, p[0].y);


printf("%d %d", p[1].x, p[1].y);
}

Structure and Function


A structure can be passed to any function from main function or from any sub function

PASSING STRUCTURE TO FUNCTION IN C:


It can be done in below 3 ways.
1. Passing structure to a function by value
2. Passing structure to a function by address(reference)
3. No need to pass a structure – Declare structure variable as global

In the below example student structure having id, name and percentage attributes. record
is the variable to control entire structure.

Example:
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student record)
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}

int main()
{
struct student record;

record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;

func(record);
return 0;
}

OUTPUT:
Id is: 1
Name is: Raju
Percentage is: 86.5

You might also like