Unit III Arrays and Structure 1
Unit III Arrays and Structure 1
1 of 14
Depending on dimensions array can be of three types as,
- One-dimensional array
(Array with one dimension)
- Two-dimensional array
(Array with two dimensions)
- Multi-dimensional array
(Array with more than two dimensions)
interest_rate[4]
interest_rate[3]
interest_rate[2]
interest_rate[1]
interest_rate[0]
-
- 0 Memory Location
Figure 3.2: Memory Allocation for float array interest_rate[5]
In C, index always starts with 0. So the last element of the array has index one less
than Size-of-array.
3 of 14
22 and 63 are initialized to the elements with indices 0, 1, 2, 3, 4 and 5 of
array a respectively */
float b[ ]={15.43,17,8.65};
/* Here size-of-array is omitted. As three elements are initialized, compiler
allocates memory space for three float elements and the values 15.43, 17.0 and
8.65 are initialized to the elements with indices 0, 1, and 2 of array b
respectively */
Partial initialization is allowed at compile-time. In this case, whatever values are
mentioned, those values get initialized to respective elements (starting from index 0) and the
remaining elements are initialized to 0.
int a[5]={23,45};
/* Here the values 23 and 45 are initialized to the elements with indices 0 and
1. Remaining elements of array are initialized to 0 */
float b[10]={15.43,17,8.65};
/* Here the values 15.43, 17.0 and 8.65 are initialized to the elements with
indices 0, 1, and 2. Remaining elements of array are initialized to 0.0 */
Arrays can also be explicitly initialized at run-time. This can be done for initializing
large arrays. One example is shown here.
for(i=0;i<150;i++)
a[i]=i;
4 of 14
- Highest Memory Location
-
m[2][2]
m[2][1]
m[2][0]
m[1][2]
m[1][1]
m[1][0]
m[0][2]
m[0][1]
m[0][0]
-
- 0 Memory Location
Figure 3.4: Memory Allocation for integer array m[3][3]
If array is initialized completely with all values, there is no need to specify first
dimension. An example is shown below.
int m[ ][3] = {{1,2,3},{4,5,6}}; /* or int m[ ][3]={1,2,3,4,5,6}; */
/* An array with 2 rows and 3 columns get created. Three elements of 0th row
are initialized to 1, 2 and 3 and three elements of 1st row are initialized to 4, 5
and 6. */
Partial initialization is also allowed. In such case uninitialized elements are initialized
to 0. Some examples are shown below.
5 of 14
int m[2][3] = {
{1},
{2,3}
};
/* Three elements of 0th row are initialized to 1, 0 and 0 and three elements of
1st row are initialized to 2, 3 and 0. */
int t[3][3] = {0,0};
/* All the elements are initialized to 0. */
m[0][2]=62;
/* Value 62 will be assigned to 2nd location in the 0th row of array m. */
6 of 14
3.3 Character Array/ Strings
We can create character arrays just like any other data types. Character arrays are used
to implement strings. String is a null-terminated sequence of characters. One example of
declaration of character array or strings is shown below.
char name[10];
Character arrays or strings can be initialized as shown in following examples.
char name[10]={„V‟,„e‟,„n‟,„k‟,„a‟,„t‟,„e‟,„s‟,„h‟,„\0‟};
/* Here name is declared as array of 10 characters and is initialized to a string
“Venkatesh” ending with a null character („\0‟). */
Figure 3.5 shows how the memory is allocated to the string variable name and how
the characters are stored in it.
- Highest Memory Location
-
„\0‟ name[9]
„h‟ name[8]
„s‟ name[7]
„e‟ name[6]
„t‟ name[5]
„a‟ name[4]
„k‟ name[3]
„n‟ name[2]
„e‟ name[1]
„V‟ name[0]
-
- 0 Memory Location
Figure 3.5: Memory Allocation for name
Some other examples are shouwn below
7 of 14
char first_name[15];
char last_name[20];
As string is assumed to be null-terminated, when a sting is assigned to a character
array, a null character („\0‟) is appended at the end of the string. So, while declaring size of
string variable, it should be mentioned as one more than the maximum number of characters
in the string.
e.g. If you want to store a string “Programming” (which contains 11 characters), the
size of character array should be at least 12.
8 of 14
3.3.1.2 Reading string using getchar( ) function
We can use getchar( ) function (defined in stdio.h file) for reading a single character.
This function can be read repeatedly for reading a string. General form of using this function
is given below.
char ch;
ch=getchar( );
We can use following code segment for reading a line of text from user.
char text[60],c;
int i=0;
do
{
c=getchar( );
text[i]=c;
i++;
}while(c!=„\n‟);
text[i–1]=„\0‟;
9 of 14
putchar(text[i]);
i++;
}
Examples:
1. Student: roll, name, branch
2. Employee: ID, name, department, salary
3. Date: day, month, year
4. Time: seconds, minutes, hours
5. Bank_Customer: account_number, name, mobile, balance
struct struct-name
{
data-type membername;
---
---
};
Example 1:
struct student
{
int roll;
char name[15];
float percent;
};
Example 2:
struct date
{
short day;
short month;
int year;
10 of 14
};
Examples:
struct student s1;
struct date b_date;
One may also combine definition of structure and declaration of a structure variable
as shown in following example.
struct date
{
short day;
short month;
int year;
}admission_date;
Examples:
b_date.day=26;
scanf(“%d”,&admission_date.year);
Sequence of values enclosed in braces must match with the sequence in which
members are declared in the structure. Initialization of members cannot be done inside the
structure definition.
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
11 of 14
char name[20];
float percent;
};
main( )
{
struct student s1,s2;
clrscr( );
getch( );
}
struct date
{
short d;
short m;
int y;
};
struct student
{
int roll;
12 of 14
char name[20];
struct date admission_date; //variable of another structure
};
Example:
struct student s[75];
Sample Code:
struct student s[5];
int i;
clrscr( );
getch( );
3.6.1 typedef
C supports a type-definition feature that allows user to define his/ her own data type
identifier. This user-defined identifier can then be further used for declaring variables. This
can be achieved by using following syntax.
typedef type identifier;
e.g.
typedef int my_int;
Then the variables can be declared by using the user-defined identifier (in above
example it is my_int) as,
my_int x,y,z;
13 of 14
3.6.2 Enumerated data type
It helps to create our own data type and define possible values that a variable of that
data-type can take.
Example:
enum gender
{
male,female,other
};
When we create enum variable, it can accept any one value from the listing only.
Sample Questions
1. Define array. [2M]
2. Define array. List its types. [2M]
3. Define i) Two-dimensional array ii) Multi-dimensional array [2M]
4. Explain one-dimensional and two-dimensional array. [4M]
5. Illustrate initialization of two-dimensional array with example. [4M]
6. Design a program in C to read n number of values in an array and display it in reverse
order. [6M]
7. State difference between array and string. [2M]
8. Differentiate between character array and integer array with respect to size and
initialization. [4M]
9. Write a program to accept ten numbers and print average of them. [6M]
10. Write a program to accept 10 numbers in array and arrange them in ascending order.
[4M]
11. Write a program to sort elements of an array in ascending order. [6M]
12. Define array. Write a program to accept ten numbers in array. Sort array elements and
display them. [6M]
13. With suitable example, explain how two-dimensional arrays can be created. [4M]
14. Write a program for addition of two 3X3 matrices. [6M]
15. Declare structure student with elements roll-no and name. [2M]
16. Write a program to declare structure employee having data member name, age, street
and city. Accept data for two employees and display it. [6M]
17. Write a program to declare structure student having rollno, name and marks. Accept
and display data for three students. [4M]
18. Develop a program using structure to print data of three students having data
members name, class and percentage. [4M]
19. Give a method to create, declare and initialize structure. Develop a program to
demonstrate nested structure. [6M]
14 of 14