1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.

Slides:



Advertisements
Similar presentations
Programming in C Chapter 10 Structures and Unions
Advertisements

C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
C Language.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Programming and Data Structure
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structure.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
CS Winter 2011 Further Introduction to the C programming language.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
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.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Functions, Pointers, Structures Keerthi Nelaturu.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
Structures and Functions Ghulam Nasir Khan. Important /\/otes Programming is a fundamental course of IT as well as BICSE, so it will be very difficult.
M.T.Stanhope Oct Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 1 C++ Basics u Data types. u Variables and Constants.
Learners Support Publications Classes and Objects.
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
Array, Structure and Union
Today’s Material Aggregate Data Types: Structures and Unions
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
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.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
1 CS161 Introduction to Computer Science Topic #15.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
CSE 251 Dr. Charles B. Owen Programming in C1 structs Aggregating associated data into a single variable Box width length height Circle radius int main()
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
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.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
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.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Chapter 10-1: Structure.
Structured Data (Lecture 07)
CS150 Introduction to Computer Science 1
Basic notes on pointers in C
Lecture 8.
Computers & Programming Languages
Classes and Objects.
Unions A union is a custom data type used for storing several variables in the same memory space. Although you can access any of those variables at any.
Chapter 10 Structures and Unions
Programming Language C Language.
Structures In C Programming By Rajanikanth B.
Structure (i.e. struct) An structure creates a user defined data type
Structures, Unions, and Enumerations
Structures Declarations CSCI 230
Presentation transcript:

1 C Language Structures

2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration Structure variable definition Structure variable definition Structure usage Structure usage Member access Member access Allowed operations Allowed operations Functions Functions

3 Concept of a Structure A programmer-defined data type Logical grouping of data Can consist of different types of data Useful for organizing data Sometimes called a record elsewhere

4 Concept of a Structure john.smith john.smith f 70.5f m service service Employee Employee: name age pension salary sex years service character array integer floating point (double) floating point character int

5 Structures in C Use the struct keyword A structure declaration creates a new type A structure variable definition creates the actual space for a structure Data items of a structure are called its members

6 Structure Declaration struct employee { char name [ 30 ]; int age; double pension; float salary; char sex; int service; }; a new type! tag members semicolon

7 Structure Variable Definition struct employee staff; struct employee partTime; variables of the new type! tagstructure variable names

8 Structure Variable Definition with Initializer struct employee newbie = { “john.smith”, 35, 3.14, 70.5f, ‘m’, 0 }; variable with initializer list

9 Structure Declaration and Variable Definition struct employee { char name [ 30 ]; int age; double pension; float salary; char sex; int service; } salesDept[100 ]; a new type and a variable definition Note: this is an array variable declaration tag structure variable name

10 Tag-less Structure Declaration and Variable Definition struct { char name [ 30 ]; int age; double pension; float salary; char sex; int service; } newEmployee; a new tag-less type and a variable definition No tag structure variable name Note: can’t define another structure variable later with an unnamed type

11 Structure Usage Member access Allowed operations Functions

12 Member Access General format: structVariable.memberName Examples: newEmployee.salary salesDept[22].age salesDept[i].service salesDept[employeeNumber].salary

13 Structure Member Access struct employee { char name [ 30 ]; int age; double pension; float salary; char sex int service; } finDept[100 ]; void print_ages (void) { int i; for ( i = 0; i < 100; i++) { printf (“Age is %d\n”, finDept[i].age); }

14 Allowed Operations Can copy or assign as a unit (but not allowed to compare 2 structures) Access its members Use sizeof() operator to obtain size in bytes sizeof (struct employee)

15 Functions Can pass to/from a function: a member of a structure an entire structure Passes a whole structure