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()

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.
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.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
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.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
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.
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 Structure part 1 & File Processing. 2 Structures.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
C Tokens Identifiers Keywords Constants Operators Special symbols.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Programming in C Structs and Unions. Java vs C Suppose you were assigned a write an application about points and straight lines in a coordinate plane.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Topic:  Reading Input  identifier,  if statement,  Arithmetic Operation CSE 102 – Programming Fundamentals.
Array, Structure and Union
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Today’s Material Aggregate Data Types: Structures and Unions
Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C.
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.
Data Types Declarations Expressions Data storage C++ Basics.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
1 Structs. 2 Defining a Structure Often need to keep track of several pieces of information about a given thing. Example: Box We know its length width.
Pointers *, &, array similarities, functions, sizeof.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Lecture 10 union, enumeration, bit
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
User-Written Functions
Structures, Unions, Enumerations
CSE 220 – C Programming Pointers.
Data types Data types Basic types
CS1010 Programming Methodology
C Programming Tutorial – Part I
Introduction to C Programming
Variables, Types and Expressions
C Structures, Unions, Bit Manipulations and Enumerations
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Variables, Types and Expressions
Introduction to C Programming
CSE 100 Data Types Declarations Displays.
UMBC CMSC 104 – Section 01, Fall 2016
EECE.2160 ECE Application Programming
Lecture 8.
EECE.2160 ECE Application Programming
Presentation transcript:

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() { Box mybox; Circle c; mybox.width = 10; mybox.length = 30; mybox.height = 10; c.radius = 10; }

CSE 251 Dr. Charles B. Owen Programming in C2 The idea Box width length height I want to describe a box. I need variables for the width, length, and height. I can use three variables, but wouldn’t it be better if I had a single variable to describe a box? That variable can have three parts, the width, length, and height.

CSE 251 Dr. Charles B. Owen Programming in C3 Structs A struct (short for structure) in C is a grouping of variables together into a single type – Similar to structs in Matlab struct nameOfStruct { type member; … }; Note the semicolon at the end. To declare a variable: struct nameOfStruct variable_name;

CSE 251 Dr. Charles B. Owen Programming in C4 Example Box width length height Circle radius #include struct Box { int width; int length; int height; }; struct Circle { double radius; }; int main() { struct Box b; struct Circle c; } You can declare variables Data structure definition A

CSE 251 Dr. Charles B. Owen Programming in C5 Example Box width length height int main() { struct Box b; b.width = 10; b.length = 30; b.height = 10; } You can assign values to each member #include struct Box { int width; int length; int height; }; We use a period “.” to get to the elements of a struct. If x is a struct, x.width is an element in a struct.

CSE 251 Dr. Charles B. Owen Programming in C6 Another Example You can use mixed data types within the struct (int, float, char []) struct bankRecordStruct { char name[50]; float balance; }; struct bankRecordStruct billsAcc;

CSE 251 Dr. Charles B. Owen Programming in C7 Accessing values struct bankRecordStruct { char name[50]; float balance; }; struct bankRecordStruct billsAcc; printf(“My balance is: %f\n”, billsAcc.balance); float bal = billsAcc.balance; Access values in a struct using a period: “.” B

CSE 251 Dr. Charles B. Owen Programming in C8 Assign Values using Scanf() struct BankRecord { char name[50]; float balance; }; int main() { struct BankRecord newAcc; /* create new bank record */ printf(“Enter account name: “); scanf(“%50s”, newAcc.name); printf(“Enter account balance: “); scanf(“%d”, &newAcc.balance); }

CSE 251 Dr. Charles B. Owen Programming in C9 Copy via = You can set two struct type variables equal to each other and each element will be copied struct Box { int width, length, height; }; int main() { struct Box b, c; b.width = 5; b.length=1; b.height = 2; c = b;// copies all elements of b to c printf(“%d %d %d\n”, c.width, c.length, c.height); }

CSE 251 Dr. Charles B. Owen Programming in C10 Passing Struct to a function You can pass a struct to a function. All the elements are copied If an element is a pointer, the pointer is copied but not what it points to! int myFunction(struct Person p) { … }

CSE 251 Dr. Charles B. Owen Programming in C11 Using Structs in Functions Write a program that – Prompts the user to enter the dimensions of a 3D box and a circle – Prints the volume of the box and area of the circle Sample run:

CSE 251 Dr. Charles B. Owen Programming in C12 #include struct Box { int width, height, length; }; int GetVolume(struct Box b) { return b.width * b.height * b.length; } int main() { struct Box b; printf("Enter the box dimensions (width length height): "); scanf("%d %d %d", &b.width, &b.length, &b.height); printf("Box volume = %d\n", GetVolume(b)); } C

CSE 251 Dr. Charles B. Owen Programming in C13 Note: == Comparison doesn’t work struct Box { int width, length, height; }; int main() { struct Box b, c; b.width = 5; b.length=1; b.height = 2; c = b; if (c == b)/* Error when you compile! */ printf(“c and b are identical\n”); else printf(“c and b are different\n”); } t Error message: invalid operands to binary == (have 'Box' and 'Box')

CSE 251 Dr. Charles B. Owen Programming in C14 Create your own equality test #include struct Box { int width, height, length; }; int IsEqual(struct Box b, struct Box c) { if (b.width==c.width && b.length==c.length && b.height==c.height) return 1; else return 0; } struct Box b, c; b.width = 5; b.length=1; b.height = 2; c = b; if (IsEqual(b,c)) printf("c and b are identical\n"); else printf("c and b are different\n"); D

CSE 251 Dr. Charles B. Owen Programming in C15 typedef typedef is a way in C to give a name to a custom type. typedef type newname; typedef int dollars; typedef unsigned char Byte; I can declare variables like: dollars d; Byte b, c; It’s as if the type already existed.

CSE 251 Dr. Charles B. Owen Programming in C16 typedef for Arrays There is a special syntax for arrays: typedef char Names[40]; typedef double Vector[4]; typedef double Mat4x4[4][4]; Now, instead of: double mat[4][4]; I can do: Mat4x4 mat;

CSE 251 Dr. Charles B. Owen Programming in C17 Using Structs with Typedef typedef struct [nameOfStruct] { type member; … } TypeName; To declare a variable: TypeName variable_name; optional

CSE 251 Dr. Charles B. Owen Programming in C18 Example Box width length height Circle radius #include typedef struct { int width; int length; int height; } Box; typedef struct { double radius; } Circle; int main() { Box b; /* instead of struct Box */ Circle c;/* instead of struct Circle */ b.width = 10; b.length = 30; b.height = 10; c.radius = 10; } E

CSE 251 Dr. Charles B. Owen Programming in C19 Arrays of structs You can declare an array of a structure and manipulate each one typedef struct { double radius; int x; int y; char name[10]; } Circle; Circle circles[5];

CSE 251 Dr. Charles B. Owen Programming in C20 Size of a Struct: sizeof typedef struct { double radius;/* 8 bytes */ int x;/* 4 bytes */ int y;/* 4 bytes */ char name[10];/* 10 bytes */ } Circle; printf(“Size of Circle struct is %d\n”, sizeof(Circle));

CSE 251 Dr. Charles B. Owen Programming in C21 Size of a Struct = 26 – But sizeof() reports 28 bytes!!! Most machines require alignment on 4-byte boundary (a word) – last word is not filled by the char (2 bytes used, 2 left over) D D I I C C C C X X 8 byte, 2 word double4 byte, 1 word integer 4 byte, 1 word integer 10 byte char array, 2 bytes of the last word unused

CSE 251 Dr. Charles B. Owen Programming in C22 Pointers to structs typedef struct { int width; int length; int height; } Box; Box b;/* A variable of type Box */ Box *c;/* A pointer to a Box */ double w; b.width = 5; b.height = 7; b.length = 3; c = &b;/* Same as before */ w = c->width; To access the members of a struct, we use:. for a variable of the struct’s type -> for a pointer to a struct

CSE 251 Dr. Charles B. Owen Programming in C23 struct Concepts struct Box { double wid, hit; }; typedef struct { double radius; int x; int y; char name[10]; } Circle; struct Box b; /* No typedef */ Circle c;/* typedef */ struct Box *pBox;/* Pointer to Box */ Circle *pCirc;/* Pointer to Circle */ pBox = &b;/* Get pointer to a Box */ b.wid = 3; pBox->wid = 7; pCirc = &c; (*pCirc).radius = 9; 1