0% found this document useful (0 votes)
13 views9 pages

BCA Sem1 C Unit-IV

Uploaded by

cm9373549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views9 pages

BCA Sem1 C Unit-IV

Uploaded by

cm9373549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C PROGRAMMING UNIT IV

Unit IV
Array and Structure

4.1 Array
1. Arrays used to store multiple values in a single variable, instead of declaring separate
variables for each value.
2. An array is a group of similar type of data elements stored in contiguous memory
location. These data elements addressed by common name.
3. Instead of declaring individual variables, such as number0, number1, ..., and number99,
you declare one array variable such as numbers and use numbers[0], numbers[1], and
..., numbers[99] to represent individual variables.

4. A specific element in an array is accessed by an index.


5. Index of first element of array is 0 and index of last element is array size-1.

4.2 Array Declaration And Initialization

1. An array is declared by writing the data types, followed by the name, followed by the
size in brackets.
2. Syntax:
data type array_name[size];

3. Example:
int n[12];
4. Here declares the n as an array to contain a maximum of 12 integer constants.
5. The c language treats character strings simply as arrays of characters.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 1


www.nandigramiit.org
C PROGRAMMING UNIT IV

6. The size in a character string represents the maximum number of characters that the
string can hold.
7. Example:

char n[20];

8. Here declare the variable n as a character array variable that can hold a maximum of 20
characters. Suppose we read the following string constant into the string variable name.
“Nandigram Institute”
9. Each character of the string is treated as an element of the array name and is stored in
the memory as follow.

n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] ….. …..n[20]
‘N ‘a ‘n ‘d ‘i ‘g ‘r ‘a ‘m ‘ ‘I ‘n ‘s ‘t ‘i ‘t ‘u ‘t ‘e ‘/n
’ ’ ’ ’ ’ ’ ’ ’ ’ ‘ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’

10. When the compiler finds a character string. It terminates it with an additional null
character. The element n[20] holds the null character ‘\0’.
11. When declaring character arrays, we must allow one extra element space for the null
terminator.

 Types of Arrays
There are three types of arrays
1. One dimensional array
2. Two dimensional array
3. Multi-dimensional array

4.3. One dimensional array


1. One-dimensional arrays, are arrays with only one dimension or a single row.
2. Generally, values in list of same data types are stored as single dimensional arrays.
 Declaration of one dimensional array
1. An array is declared by writing the data types, followed by the name, followed by the
size in brackets.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 2


www.nandigramiit.org
C PROGRAMMING UNIT IV

2. Syntax:
data type array_name[size];

3. example:
int n[12];

 Initialization of one dimensional array


1. We can initialize the elements of arrays in the same way as the ordinary variables
when they are declared.
2. Syntax:
Data type array-name[size]={list of values};

The values in the list are separated by commas

3. Ex- int number[3]={0,0,0};

4. Here declare the variable number as an array of size 3 and will assign zero to each
elements.
5. If the number of values in the list is less than the number of elements will be set to
zero automatically.

float total[5]={0.0,15.75,-10.0};

6. Here initialize the first three elements to 0.0, 15.75 and -10.0 and the remaining two
elements to 0.0.
7. We also declare the array as variant. In that no need to enter the size of array.

Ex int a[]={1,3,4};

8. That is called as variant and in that we can store the number of element.
9. Similar, We can also declare the character array but we must end the string with ‘\0’

ex- char name[]={‘b’,’h’,’o’,’s’,’l’,’e’,’\0’}

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 3


www.nandigramiit.org
C PROGRAMMING UNIT IV

 Entering data into an array

int marks[30];
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ;
}

The ‘for’ loop causes the process of asking and receiving a student’s marks from the user to be
repeated 30 times.

 Reading Data from an Array


Suppose we want to read the above array of 30 student’s marks and calculate the average of
marks.

int sum=0, avg;


for ( i = 0 ; i <= 29 ; i++ )
{
sum=sum+marks[i];
}
Avg=sum/30;
printf ( "\nAverage of marks = %d ", avg ) ;

4.4. Two dimensional array


1. Two-dimensional array is array with two dimensions or with row and column.
2. Generally, values in table of same data types are stored as two dimensional arrays.
 Declaration of two-dimensional array
1. An array is declared by writing the data types, followed by the name, followed by the
row-size in first brackets and column-size in second brackets.
2. Syntax:
data type array_name[row-size][colum-size];

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 4


www.nandigramiit.org
C PROGRAMMING UNIT IV

3. example:
int n[12][10];
 Initialization of one dimensional array
1. We can initialize the elements of arrays in the same way as the ordinary variables
when they are declared.
2. Syntax:
Data type array-name[row-size][column-size]={{list of values in first row},
{list of values in second row}, . . . . , {list of values in row-size th row}} ;

The values in the list are separated by commas

3. Ex- int number[3][4]={{0,0,0,0}, {1,2,5,4}, {5,9,8,7}};

4. Here declare the two dimensional array number as an array of row-size 3 and colum-
size 4 will assign value as shown in { } each elements.
5. We also declare the array as variant. In that no need to enter the size of array.

Ex int a[ ][ ] = { {0,0,0,0}, {1,2,5,4}, {5,9,8,7} };

6. That is called as variant and in that we can store the number of element.
Entering data into an two dimensional array

int marks[30][4];
for ( i = 0 ; i <= 29 ; i++ )
{
for(int j=0; j<4; j++}
{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ;
}
}

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 5


www.nandigramiit.org
C PROGRAMMING UNIT IV

The ‘for’ loop causes the process of asking and receiving a 30 student’s marks of four subject
from the user to be repeated 30 times.

4.5 Standard String library functions

4.6 Creating structures

4.7 Accessing structure members (dot Operator)

4.8 Unions

1. In C, a union is a user-defined data type that allows you to store different types of data
in a single memory location, but only one of them at a time.
2. It is similar to a structure in that it can hold multiple variables of different data types,
but unlike a structure, a union uses the same memory location for all its members.
3. This means that only one member of the union can be used at any given time, and
accessing a different member will overwrite the current value.
4. Here's the basic structure of a union in C:

union union_name {
data_type member1;
data_type member2;
// ...
};

5. Here's an example of a union that can store either an integer or a float value:

#include <stdio.h>

union NumericValue {
int intValue;

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 6


www.nandigramiit.org
C PROGRAMMING UNIT IV

float floatValue;
};

int main() {
union NumericValue number;

number.intValue = 42;
printf("Integer value: %d\n", number.intValue);

number.floatValue = 3.14;
printf("Float value: %f\n", number.floatValue);

// Accessing the other member of the union


printf("Integer value after setting a float: %d\n", number.intValue);

return 0;
}

In the example above, we define a union named NumericValue.


This union has two members: intValue of type int and floatValue of type float.
When we assign a value to one of the members, it uses the same memory location to store the
value.
This means that setting the floatValue overwrites the intValue, and accessing intValue after
setting floatValue will yield unpredictable results.

Unions are commonly used in situations where you need to store different types of data in a
single variable but only use one type at a time.
However, you must be careful when using unions to ensure that you access the correct member,
depending on the context, as there's no built-in mechanism to determine which member is
currently valid.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 7


www.nandigramiit.org
C PROGRAMMING UNIT IV

 Difference between union and structure in C


Here's a table highlighting the key differences between unions and structures in C:

Characteristic Union Structure

Shares the same memory location for all Allocates separate memory for each
members. It is as large as the largest member. The size is the sum of all
Memory Usage member. members' sizes.

Only one member can be accessed at a time


Members because they share the same memory All members are accessible
Accessibility location. simultaneously.

Members do not interfere with each


Members can overwrite each other's values. other. Each member has its own
Storage of Data Changing one member can affect others. memory location.

Used when you need to store different types


of data in a single variable but only use one Used when you want to group related
Use Case type at a time. data together as a composite data type.

union NumericValue { int intValue; float


Example floatValue; }; struct Point { int x; int y; };

Size is the sum of the sizes of all


Size Calculation Size is determined by the largest member. members.

Accessing Use the . (Dot) operator to access


Members Use the . (Dot) operator to access members. members.

Storing data in a database where different


Use Case fields can be of various types (e.g., integers, Representing a point with x and y
Example floats, strings). coordinates in a Cartesian plane.

Typical Efficient for saving memory when only one Used for organizing and grouping
Operations member is used at a time. related data.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 8


www.nandigramiit.org
C PROGRAMMING UNIT IV

In summary, unions and structures serve different purposes in C.


Unions are used when you want to store different types of data in the same memory location,
while structures are used to group related data elements together.
The choice between unions and structures depends on the specific requirements of your program and
how you intend to use the data.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 9


www.nandigramiit.org

You might also like