X
Array declaration that
char Name_ID[20]; represent each data.
char No_Matrix[12];
int Age_S; Single dimension array .
Array versus Structure
Name_ID No_Matrix Age_S
Aminah BK11330022 20
Bangla BK11332021 20 Name_ID[3][8]
Mohamad BK12122331 19 [0] [1] [2] [3] [4] [5] [6] [7]
[0] A m i n a h \0
[1] B a n g l a \0
[2] M o h a m a d \0
char Name_ID[3][8];
char No_Matrix[3][10];
int Age_S[3];
No_Matrix[3][12]
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Age_S[3]
[0] B K 1 1 3 3 0 0 2 2
[0] 20 [1] B K 1 1 3 3 2 0 2 1
2-dimensional
[1] 20 [2] B K 1 2 1 2 2 3 3 1
arrays
[2] 19
Array versus Structure
Name_ID No_Matrix Age_S What if we want to select/refer
Aminah BK11330022 20 to a group of dissimilar data
type?
Bangla BK11332021 20
Mohamad BK12122331 19
struct student_struct {
char Name_ID[8];
char No_Matrix[10];
int Age_S;
};
Array versus Structure
• Structure is a collection of different data types
which are grouped together and each element
in a C structure is called member.
struct student_struct {
Examples
char Name_ID[8];
char No_Matrix[10];
int Age_S;
};
Structure Variable Declaration
#include<stdio.h>
#include<conio.h>
struct student_struct {
char Name_ID[8]; int main(void) {
char No_Matrix[10];
int Age_S; ...
}; ...
...
struct student_struct {
char Name_ID[8];
char No_Matrix[10];
int Age_S;
};
int main(void) {
getch();
return 0; }
Structure Variable Declaration
#include<stdio.h>
#include<conio.h>
struct student_struct {
char Name_ID[8]; int main(void) {
char No_Matrix[10];
int Age_S; ...
} S1, S2, S3[10]; ...
...
getch();
return 0; }
Example
#include<stdio.h>
#include<conio.h>
struct student_struct {
char Name_ID[8];
char No_Matrix[10];
int Age_S;
} S1, S2, S3[10];
int main(void) {
. . .
. . .
. . .
getch();
return 0; }
Access members/components
• There are two types of operators used for
accessing members of a structure.
– Member operator(.)
– Structure pointer operator(->)
• int main(){
• printf("1st distance\n"); printf("Enter feet: ");
• scanf("%d",&d1.feet); printf("Enter inch: ");
• scanf("%f",&d1.inch);
• printf("2nd distance\n"); printf("Enter feet: ");
• scanf("%d",&d2.feet); printf("Enter inch: ");
• scanf("%f",&d2.inch);
• sum.feet=d1.feet+d2.feet; sum.inch=d1.inch+d2.inch;
• if (sum.inch>12){
• ++sum.feet; sum.inch=sum.inch-12;
• }
• printf("Sum of distances=%d\'-%.1f\"",sum.feet,sum.inch);
• getch(); return 0; }
Outputs
1st distance
Enter feet: 12
Enter inch: 7.9
2nd distance
Enter feet: 2
Enter inch: 9.8
Sum of distances= 15'-5.7"
Keyword typedef while using
structure
• Programmer generally use typedef while using
structure in C language. For example:
1) scanf(“%d”, &f1);
2) printf(“%d”, f2);
3) f1 = f2 + f3;
4) if (f1 == f2)
. . . ;
Structures within Structures
• Structures can be nested within other structures
in C programming.
struct complex {
int imag_value;
n1 or n2
float real_value;
c1; complex
};
imag_value;
Syntax:
enum type_name{ value1, value2,...,valueN };
enum suit{
club=0;
diamonds=10;
hearts=20;
spades=3;
};
Enumerator
#include <stdio.h>
int main(){
enum week today;
today=wednesday;
printf("%d day",today+1);
return 0;
}
THE END
Tips for Final
• Q1: 30 marks (11 sub questions)
• Q2: 10 marks (5 sub questions)
• Q3: 10 marks (4 sub questions)
• Q4: 10 marks (1 question)
• Q5: 10 marks (1 question)
• Q6: 20 marks (5 sub questions)
• Q7: 10 marks (4 sub questions)
Tips for Final
• Q1: 30 marks (Quick Review exercises – 1
question each chapter)
• Q2: 10 marks (Logical true/false)
• Q3: 10 marks (Determine program outputs)
• Q4: 10 marks (Write a simple program)
• Q5: 10 marks (Debug errors – 5 errors)
• Q6: 20 marks (Determine program outputs &
then change the program structure)
• Q7: 10 marks (Write a simple program based on
instructions given)