BCA Sem1 C Unit-IV
BCA Sem1 C 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.
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.
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
2. Syntax:
data type array_name[size];
3. example:
int n[12];
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’
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.
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}} ;
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.
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] ) ;
}
}
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.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;
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);
return 0;
}
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.
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.
Typical Efficient for saving memory when only one Used for organizing and grouping
Operations member is used at a time. related data.