C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = 01000001) Field –Group of characters (character string “Fred”)

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
Programming in C Chapter 10 Structures and Unions
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
Data Types in C. Data Transformation Programs transform data from one form to another –Input data  Output data –Stimulus  Response Programming languages.
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.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Programming and Data Structure
Programming Languages and Paradigms The C Programming Language.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
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.
Introduction to Data Structures Systems Programming.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Introduction to Data Structures Systems Programming Concepts.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
CPS120: Introduction to Computer Science Lecture 15A Structures.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
StructureStructure. Outline Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
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 *
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Object Oriented Programming Lecture 2: BallWorld.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
פרטים נוספים בסילבוס של הקורס
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Structures, Unions, and Enumerations
Structures Declarations CSCI 230
Presentation transcript:

C Structures Basics of structures Typedef

Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”) Record –Composed of related fields (struct) File –Group of related records (student record file) Database –Group of related files (students, faculty, and staff files)

Structure Declaration Structure –Collection of related variables Structure tag –“student” Structure’s members –first, last, age, gpa struct student{ char first[20]; char *last; int age; double gpa; };

Structure Definition Define & initialize with list of variables Define & initialize using the dot operator (structure member operator) struct student s1 = {"Ted","Tanaka", 22, 2.22}; struct student s2; strcpy(s2.first,"Sally"); s2.last="Suzuki"; s2.age = 33; s2.gpa = 3.33;

Accessing Members Structure member operator, or dot operator –For direct variables (.) Structure pointer operator –For pointers (->) struct student s2; struct student *sPtr =&s2; printf("%s %d\n", s2.last, s2.age); /*Tanaka 22*/ printf("%s %d\n", (*sPtr).last, sPtr->age); /*Suzuki 33*/ *See complete program at accessingMembers.caccessingMembers.c

sizeof Operator Returns the size in bytes of a data type char a, b[10]; int c, d[10]; double e, f[10]; printf("sizeof(a) = %d\n",sizeof(a)); printf("sizeof(b) = %d\n",sizeof(b));... /* sizeof(a) = 1 sizeof(b) = 10 */ /* sizeof(c) = 4 sizeof(d) = 40 */ /* sizeof(e) = 8 sizeof(f) = 80 */

sizeof Operator Examples with pointers char b[10], *p1 = b; int d[10], *p2 = d; double f[10], *p3 = f; printf("sizeof(p1) = %d ",sizeof(p1)); printf("sizeof(*p1) = %d\n",sizeof(*p1));... /* sizeof(p1) = 4 sizeof(*p1) = 1 */ /* sizeof(p2) = 4 sizeof(*p2) = 4 */ /* sizeof(p3) = 4 sizeof(*p3) = 8 */

sizeof Operator Examples with structures struct student s1={"Ted","Tanaka", 22,2.22}; struct student s2; struct student *s3 = &s2; struct student s4[10]; printf("sizeof(s1) = %d\n", sizeof(s1));... /*sizeof(s1) = 40 sizeof(s2) = 40 sizeof(s3) = 4 sizeof(s4) = 400 */

sizeof Operator struct student{ char first[20]; /* 20 bytes */ char *last; /* 4 bytes */ int age; /* 4 bytes */ double gpa; /* 8 bytes */ };/*total = 36 bytes ?? */ Structures may have “extra padding”, because computers may store specific data types on certain memory boundaries –See complete program at sizeof.csizeof.c

Structures & Functions You can pass a structure to a function by value struct student s1={"Ted","Tanaka", 22, 2.22}; incAge1(s1); printStudent(s1); /* Name = Ted Tanaka age = 22 gpa = 2.22 */ void incAge1(struct student s){s.age++;} void printStudent(struct student s){ printf("Name = %s %s \n age = %d gpa = %.2f\n",s.first, s.last, s.age, s.gpa);}

Structures & Functions Or pass by reference (pointer to a structure) struct student s1={"Ted","Tanaka", 22, 2.22}; struct student *p = &s1; incAge2(p); printStudent(s1); /* Name = Ted Tanaka age = 23 gpa = 2.22 */ void incAge2(struct student *s){s->age++;} –See complete program at functions.cfunctions.c

Arrays of Structures Can also have an array of a structure main(){ struct student s[100]={"","", 0, 0.0}; printStudent(s[0]); } /* Name = age = 0 gpa = 0.00 */

struct A{/*See struct1.c*/struct1.c char b; int c; }; struct A fun(struct A); main(){ struct A d = {'e', 71}; /* sizeof(d)=8 */ struct A e[20]; printf("sizeof(e)=%d\n", sizeof(e)); e[0] = fun(d); printf("b=%c c=%d\n", d.b, d.c); printf("b=%c c=%d\n", e->b, (*e).c); } struct A fun(struct A a){ printf("b=%c c=%d\n", a.b++, ++a.c); return a;}

typedef Creates new data type name –Integer used as a synonym for int –IntegerPtr used as synonym for int * (See program at typedef.c)typedef.c typedef int Integer; typedef int* IntegerPtr; main(){ Integer a = 10; IntegerPtr b = &a; a++; printf("a = %d\n",a); printf("b = %d\n",*b); } /*a = 11 b = 11*/

typedef Creates synonyms for previously declared data types –Can be defined after the struct struct student{ char first[20]; char *last; int age; double gpa; }; typedef struct student Student;

typedef Creates synonyms for previously declared data types –Or defined in one statement typedef struct student{ char first[20]; char *last; int age; double gpa; } Student;

typedef Does not create a new type Instead adds a new name for an exiting type Similar to #define, except it is interpreted by the compiler Student s; strcpy(s.first,"Fred"); s.last = "Tanaka"; s.age = 22; s.gpa = 2.22;

Example Program See fig10_03.c from D & D textbookfig10_03.c –Represents a deck of cards as an array of structures –To shuffle the cards, the program randomly swaps the cards –Output displays 52 cards in two columns –Note the use of the dot operator (.) and the use of typedef