Structs, Enums, Unions Rudra Dutta CSC 230 - Spring 2007, Section 001.

Slides:



Advertisements
Similar presentations
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Advertisements

C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
C Language.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Structure of a C program
Structures, Unions, and Typedefs CS-2301 D-term Structures, Unions, and Typedefs CS-2301 System Programming D-term 2009 (Slides include materials.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Data Types.
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.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Programming in C Structs and Unions. No Classes in C Because C is not an OOP language, there is no way to combine data and code into a single entity.Because.
C++ for Java Programmers Chapter 2. Fundamental Data Types Timothy Budd.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Functions in C. Consider #include main() { int i; for(i=1; i
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
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.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
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.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
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.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
C++ Lesson 1.
The Machine Model Memory
Structures, Unions, Enumerations
Data types Data types Basic types
Computing and Statistical Data Analysis Lecture 2
C Programming Tutorial – Part I
Revision Lecture
C Basics.
C Structures, Unions, Bit Manipulations and Enumerations
Built-In (a.k.a. Native) Types in C++
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
C Language B. DHIVYA 17PCA140 II MCA.
Presentation transcript:

Structs, Enums, Unions Rudra Dutta CSC Spring 2007, Section 001

Copyright Rudra Dutta, NCSU, Spring, Derived Data Types  Arrays  Pointers  Structs Enums Unions

Copyright Rudra Dutta, NCSU, Spring, struct : A Single Variable Type with Multiple Components Example: a person has a – Weight – Height – Gender – ID number – Age in years – Etc. To indicate these are all part of the same “entity”, we define a struct data type

Copyright Rudra Dutta, NCSU, Spring, Example Makes more sense than simply defining these fields individually, not indicating how they are related struct person { int weight; int height; char gender; int idnum; short age; … }; struct person person1;

Copyright Rudra Dutta, NCSU, Spring, Compared with Java instance fields of a class in Java are about the same thing as members of a struct in C – Except there is no access specifier ( public, private ) in C for struct members – they are all public Syntax for referring to both is the same person1.height = 72; person1.weight = 180; person1.gender = ‘M’; …

Copyright Rudra Dutta, NCSU, Spring, Initializing Structures Unitialized struct person person1; struct person person1 = {72, 180, ‘M’, , 20}; struct person person1 = {72, 180, ‘M’}; struct person person1 = {.height = 72,.gender = ‘M’,.idnum = }; Fully initialized Partly initialized by member order Partly initialized by member name

Copyright Rudra Dutta, NCSU, Spring, Referring to structs and members Simple assignment to a struct member person3.weight = person2.weight; Assignment to an entire struct person2 = person1; person4 = (struct person) {person2.height, person3.weight, person1.gender, , person1.age+65};

Copyright Rudra Dutta, NCSU, Spring, struct s as Input Parameters Structs are passed by value, as usual – i.e., a copy is made and passed to the function void printperson ( struct person ); int main () { struct person person1 = {…}; … (void) printperson (person1); … } void printperson ( struct person p ) { … }

Copyright Rudra Dutta, NCSU, Spring, struct s as Return Values struct person getperson ( void ) { struct person np = {0, 0, ‘ ‘, 0}; (void) printf (“Enter person characteristics: “); scanf(“%d %d %c %d”, &(np.height), &(np.weight), &(np.gender), &(np.age); return (np); /* function returns multiple values * in a single struct */ } int main () { struct person person1; … person1 = getperson (); … } Parentheses needed?

Copyright Rudra Dutta, NCSU, Spring, Arrays containing struct s Example … int main () { struct person persons[100]; … persons[1] = getperson (); persons[2].age = persons[1].age; … } Parentheses needed, i.e., should it be… … (persons[2]).age = (persons[1]).age; …

Copyright Rudra Dutta, NCSU, Spring, Initializing Arrays of Structs Example struct person persons[100] = { { 72, 180, ‘M’, 20 }, { 66, 100, ‘F’, 19 }, { 76, 240, ‘M’, 25}, [10] = {.height = 70,.gender = ‘M’} };

Copyright Rudra Dutta, NCSU, Spring, struct s Containing struct s Are parentheses needed, e.g., (person1.phone).areacode ? struct phonenumber { short countrycode; short areacode; short exchange; short number; }; struct person { int height; int weight; struct phonenumber phone; … } person1; … if (person1.phone.areacode == 919) … struct phonenumber { short countrycode; short areacode; short exchange; short number; }; struct person { int height; int weight; struct phonenumber phone; … } persons[100]; … if (persons[5].phone.areacode == 919) …

Copyright Rudra Dutta, NCSU, Spring, struct s Containing Arrays struct person { int weight, height; char gender; int idnum; short age; short coursenums[6]; }; struct person persons[100];

Copyright Rudra Dutta, NCSU, Spring, struct s Containing Arrays (cont’d) How query if course number of first class taken by third person is 230? How assign 246 to 4 th class of 10 th person? How initialize 12 th person’s data?

Copyright Rudra Dutta, NCSU, Spring, struct person { int weight, height; char gender; int idnum; short age; short coursenums[6]; }; struct person persons[100]; struct person *p_array; p_array = persons; /* point to first struct */ (*p_array).weight = 175; p_array->height = 170; Pointers to struct s Pointer access to structs is very common – Special notation defined for the purpose

Copyright Rudra Dutta, NCSU, Spring, The const Keyword Indicates to the compiler that a value should not change during program execution – Statements that attempt to change this value will cause a compiler warning – The only way to set variable: initialize during definition const int twopowfive = 32; const float pi; twopowfiv = 64; /* ERROR */ pi = ; /* ERROR */

Copyright Rudra Dutta, NCSU, Spring, The const Keyword (cont’d) Is this better than macros? #define TWOPOWFIV 32 #define PI How about this? struct pet { char *name; unsigned short weight; unsigned char age; unsigned char type; }; const struct pet mypet = { “Fluffy”, 30, 5, DOG };

Copyright Rudra Dutta, NCSU, Spring, const and Pointers Changeable pointer to changeable character Unchanging pointer to changeable character Changeable pointer to unchanging character Unchanging pointer to unchanging character char * cp = &c; *cp++ = ‘A’; /* no problems */ char * const cp = &c; cp = dp ; /* ERROR, changes pointer */ const char * cp = &c; *cp = ‘Z’ ; /*ERROR, changes value pointed to */ const char * const cp = &c; *cp++ = ‘Z’ ; /* ERROR, changes both */

Copyright Rudra Dutta, NCSU, Spring, Enumerated Data Type Use for variables with small set of possible values, where binary encoding of value is unimportant enum colors {red, blue, green, purple}; enum colors mycolors; mycolors = blue; if (mycolors == blue) printf("color == blue\n"); else if (mycolors == green) printf("color == green\n");

Copyright Rudra Dutta, NCSU, Spring, Enumerated Data Type (cont’d) Don’t compare variables of different enumerated types! enum {blue, red, green, purple} newcolors; enum {black, brown, orange, yellow} morecolors; newcolors = red; morecolors = brown; if (morecolors == newcolors) printf("Same color\n"); Although you can interpret enumerated data types as integers, this is not recommended

Copyright Rudra Dutta, NCSU, Spring, Enumerated Data Types (cont’d) Is this better than macros? #define BLUE 0 #define RED 1 #define GREEN 2 #define PURPLE 3 int colors; colors = RED; if (colors == RED) {…};

Copyright Rudra Dutta, NCSU, Spring, The typedef Statement Assigns an alternate name (synonym) to a C data type – More concise, more readable typedef char * cptr; cptr cp, dp; typedef struct mystruct { int val; cptr name; struct mystruct *next; } llnode; llnode entries[100];

Copyright Rudra Dutta, NCSU, Spring, The typedef Statement (cont’d) Sometimes typedefs used to provide machine portability – To retarget a program for a different architecture, just redefine the typedefs and recompile Usually, typedefs are collected in a header file that is #include ’d in all source code modules typedef int values[20]; values tbl1, tbl2;

Copyright Rudra Dutta, NCSU, Spring, _Bool variables Defines an integer variable that is restricted to store only the values 0 (false) and 1 (true) – Attempt to assign any non-zero value will actually store the value 1 #include … _Bool test1; test1 = ((c = getchar()) && (c != ‘n’)); if (test1) …

Copyright Rudra Dutta, NCSU, Spring, Bit Fields in C Way to pack bits into a word – When useful? Bit fields are defined like (integer) members of a structure

Copyright Rudra Dutta, NCSU, Spring, Bit Fields Example ( Frequently devices and OS communicate by means of a single word struct Disk_register { unsigned ready:1; unsigned error_occured:1; unsigned disk_spinning:1; unsigned write_protect:1; unsigned head_loaded:1; unsigned error_code:8; unsigned track:9; unsigned sector:5; unsigned command:5; };

Copyright Rudra Dutta, NCSU, Spring, Example (cont’d) struct Disk_register * dr = (struct Disk_register * ) MEMADDR; /* Define sector and track to start read */ dr->sector = new_sector; dr->track = new_track; dr->command = READ; /* ready will be true when done, else wait */ while ( ! dr->ready ) ; if (dr->error_occured) /* check for errors */ { switch (dr->error_code) }

Copyright Rudra Dutta, NCSU, Spring, Bit Fields (cont’d) Restrictions – My recommendation: always make unsigned – # of bits determines maximum value – Cannot have array of bit fields – Cannot have pointer to a bit field Danger: bit-fields are non-portable! – Order in which bit-fields stored within a word is system dependent

Copyright Rudra Dutta, NCSU, Spring, The union Statement Defined like a structure, but only stores exactly one of the named members – Reason: save storage – Important? Nothing in the union tells you which member is stored there! – Usually: programmer defines another field to indicate

Copyright Rudra Dutta, NCSU, Spring, union Example union info { unsigned short speed_of_flight; // bird _Bool freshwater; // fish enum {VERY, SOME, LITTLE} hairiness; // mammal }; struct { unsigned char type; char * name; union info info; } animals[10]; animals[0].type = MAMMAL; animals[0].name = "Polar Bear"; animals[0].info.hairiness = VERY;