Presentation is loading. Please wait.

Presentation is loading. Please wait.

Struct 1. Definition: Using struct to define a storage containing different types. For example it can contain int, char, float and array at the same time.

Similar presentations


Presentation on theme: "Struct 1. Definition: Using struct to define a storage containing different types. For example it can contain int, char, float and array at the same time."— Presentation transcript:

1 struct 1

2 Definition: Using struct to define a storage containing different types. For example it can contain int, char, float and array at the same time. You can imagine the shape of the storage like this: struct employee int ID float grade char name[10] int marks[5] Name of the struct content of the struct 2

3 How to implement the last shape? 3

4 Example for reading and writing struct elements: #include struct employee{ int ID; float grade; char name[10]; int marks[5]; } emp1, emp2; void main() { int i; float sum=0; struct employee emp; printf(“enter your ID: ”); scanf(“%d”,&emp.ID); printf(“enter your name: ”); scanf(“%s”,emp.name); 4 printf(“enter your 5 marks: ”); for(i=0; i<5; i++){ scanf(“%d”,emp.marks[i]); sum = sum + emp.marks[i]; } emp.grade =sum; printf(“your ID: %d\n”, emp.ID); printf(“ your name: %s\n”, emp.name); printf(“your grade: %f”, emp.grade); printf(“ your 5 marks: ”); for(i=0; i<5; i++){ printf(“ \n mark[%d] = %d”,emp.marks[i]); }

5 Array of struct : #include struct employee{ int ID; float grade; char name[10]; int marks[5]; } emp1, emp2; void main() { int i; float sum; struct employee emp[10]; for(j=0; j<10; j++){ sum=0; printf(“enter your ID: ”); scanf(“%d”,&emp[j].ID); printf(“enter your name: ”); scanf(“%s”,emp[j].name); 5 printf(“enter your 5 marks: ”); for(i=0; i<5; i++){ scanf(“%d”,emp[j].marks[i]); sum = sum + emp[j].marks[i]; } emp[j].grade =sum; } for(j=0; j<10; j++){ printf(“your ID: %d\n”, emp[j].ID); printf(“ your name: %s\n”, emp[j].name); printf(“your grade: %f”, emp[j].grade); printf(“ your 5 marks: ”); for(i=0; i<5; i++){ printf(“ \n mark[%d] = %d”,emp[j].marks[i]); }


Download ppt "Struct 1. Definition: Using struct to define a storage containing different types. For example it can contain int, char, float and array at the same time."

Similar presentations


Ads by Google