Anda di halaman 1dari 26

Structures & Enum

Array versus Structure


Name_ID No_Matrix Age_S
Aminah BK11330022 20
Bangla BK11332021 20
Mohamad BK12122331 19

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.

• Array is a collection of variables belongings to


the same data type. You can store group of
data of same data type in an array.
Structure Syntax
Programmer defined

Syntax struct StructureTypeName {


List of Members/Components
};

You can add any


component.

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 S1, S2, getch();


S3[10]; return 0; }
Example
#include<stdio.h>
#include<conio.h>

struct student_struct {
char Name_ID[8];
char No_Matrix[10];
int Age_S;
};

int main(void) {

struct student_struct S1, S2, S3[10];

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(->)

• Any member of a structure can be accessed


as: structure_variable_name.member_name
Access members/components
struct student_struct {
char Name_ID[8];
char No_Matrix[10];
int Age_S;
} S1, S2, S3[10];

• Suppose, we want to access Name_ID for


variable S1,
S1.Name_ID
Examples
• #include <stdio.h>
• #include <conio.h>
• struct Distance{
• int feet;
Defining structure and variable
• float inch;
• }d1,d2,sum;

• 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:

typedef struct complex{


int imag;
float real;
}comp;

Inside main: comp c1,c2;


Keyword typedef while using
structure
• The typedef keyword is used in creating a
type comp (which is of type as struct
complex). Then, two structure variables c1
and c2 are created by this comp type.
Operation on Structure Variable
• Apart from copying and passing to functions,
none of the operators that you have studied
until this point can be applied to structure
variables.

• These include input-output functions, all


arithmetic operators, and comparison
operators.
Operation on Structure Variable
• If given,
struct faculty_struct f1, f2, f3;

• All of the following statements are illegal:

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;

struct number{ real_value;


struct complex c1; real
int real;
}n1,n2;
Structures within Structures
• Suppose you want to access imag_value
for n2 structure variable then, structure
member n1.c1.imag_value is used.
Enumerator
• Enumeration type allows programmer to define
their own data type . Keyword enum is used to
defined enumerated data type.

Syntax:
enum type_name{ value1, value2,...,valueN };

• By default, value1 will be equal to 0, value2 will


be 1 and so on but, the programmer can change
the default value.
Enumerator
enum suit{ club; diamonds;
hearts; spades; };
By default, the value in club is 0, diamonds is 1, hearts is 2, and spades is 3.

enum suit{
club=0;
diamonds=10;
hearts=20;
spades=3;
};
Enumerator
#include <stdio.h>

enum week { sunday, monday, tuesday, wednesday,


thursday, friday, saturday};

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)

Anda mungkin juga menyukai