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.

Slides:



Advertisements
Similar presentations
UNIONS IN C.  Union Data Type Union Data Type  Defining of Union Defining of Union  Memory Space Allocation Memory Space Allocation  Example of Union.
Advertisements

Structure.
Structures Spring 2013Programming and Data Structure1.
Sort the given string, without using string handling functions.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
Sorting Leena A PGT Comp SC KV No:2 Jalahalli. Introduction Common problem: sort a list of values, starting from lowest to highest. –List of exam scores.
Data Management and File Organization
Nested Loops. Problem Print The only printfs you can use are: –printf(“*”); –printf(“\n”); *****
1 Array Knowledge Understand the execute technique of array Skill Can write application program using one and two dimensional array.
Tutorial #7 Summer sizeof int main() { char x; sizeof(x); sizeof(int); }
Multidimensional Arrays. Example Write a program to keep track of all warmup scores for all students. Need a list of a list of scores Student – score.
ICS 324 Students Marks and Grades
1 CSE1301 Computer Programming Lecture 24 Structures (Part 2)
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Create your own types: typedef #define N 3 typedef double scalar; /* note defs outside fns */ typedef scalar vector[N]; typedef scalar matrix[N][N]; /*
1 CSE1301 Computer Programming Lecture 23 Structures (Part 2)
22&23-2 Understand how structure data types are implemented in C. Use “.” operator to access members of a structure. See how to declare pointers to structure.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
1 Principles of Programming I Note Set #12. 2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters.
SNPL1 GAUSS ELIMINATION & BACK SUBSTITUTION GAUSS ELIMINATION & BACK SUBSTITUTION Dayun Yu Seungmuk Ji.
Functions, Pointers, Structures Keerthi Nelaturu.
Advanced Data types and Sorting
Summary. Data Types int A; I am declaring a variable A, which is an integer.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
Khalid Rasheed Shaikh Computer Programming Theory 1.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
 Complex data types  Structures  Defined types  Structures and functions  Structures and pointers.
Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear.
Functions & Pointers in C Jordan Erenrich
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Principle Prog Revision. Question 1 (a) Identify errors in the following program segment and how the errors can be corrected. void main(){ constant int.
Computer Programming for Engineers
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Chapter 1 Basic C Programming
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
2009/12/8 Report 報告學生 : 黃健瑋 指導教授 : 李正帆 1. Content seq_file structure proc file Data structure(not completed) 2.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
CE-2810 Dr. Mark L. Hornick 1 “Classes” in C. CS-280 Dr. Mark L. Hornick 2 A struct is a complex datatype that can consist of Primitive datatypes Ints,
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
Structures, Unions, Enumerations
Module 2 Arrays and strings – example programs.
CSCI206 - Computer Organization & Programming
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
מבוא כללי למדעי המחשב תרגול 2
S. Kiran, PGT (CS) KV, Malleswaram
ساختار ها در زبان C Structures in C.
Structure ការណែនាំអំពី Structure
Lecture 10 Arrays.
EECE.2160 ECE Application Programming
Introduction to Computer Organization & Systems
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Structures In C Programming By Rajanikanth B.
Structures in c By Anand George.
EECE.2160 ECE Application Programming
Presentation transcript:

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. 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

How to implement the last shape? 3

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]); }

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]); }