CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
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 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
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”)
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.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Structures, Unions, and Typedefs CS-2301 D-term Structures, Unions, and Typedefs CS-2301 System Programming D-term 2009 (Slides include materials.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Programming with Collections Collections in Java Using Arrays Week 9.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Introduction to C Programming CE Lecture 10 Data Structures typedef and struct.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
Structures and UnionsCS-2301 B-term Structures and Unions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
1 Structures. 2 C gives you several ways to create a custom data type. –The structure, which is a grouping of variables under one name and is called an.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
LECTURE 4: INFORMATION REPRESENTATION (PART II) COMP26120: ALGORITHMS AND IMPERATIVE PROGRAMMING.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Stack and Heap Memory Stack resident variables include:
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
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:
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.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
Structures Lesson CS1313 Spring Structures Lesson Outline 1.Structures Lesson Outline 2.Beyond Arrays 3.A Company and Its Employees #1 4.A Company.
1 Structures Etter Chapter 7 In engineering, we need to process or visualize data –Some of you have done Matlab simulations and visualizations Sometimes.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
CPS120: Introduction to Computer Science Lecture 15A Structures.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
CPS120: Introduction to Computer Science Data Structures.
CSC Programming for Science Lecture 34: Dynamic Pointers.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
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.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
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 *
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
User-Written Functions
5.13 Recursion Recursive functions Functions that call themselves
Chapter 10-1: Structure.
ENEE150 Discussion 07 Section 0101 Adam Wang.
UNIT-3 LINKED LIST.
CS1010 Discussion Group 11 Week 12 – Structured Structures.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Lecture 18 Arrays and Pointer Arithmetic
Presentation transcript:

CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C

C structures We are going to discuss defining more complex data structures –Structures are “user-defined types” Data structure design is a very important part of programming –We learned many program design concepts Use of control flow structures such as loops and if statements Use of functions and recursion –Data structures design is as important as any of these concepts

C structures For representing the data in our program, so far we used –basic data types int a ; char c ; double x; –arrays int a[10][10] ; –pointers int *b; Sometimes we need a richer data representation –For example: For each student, we may want to keep the following data: perm number, first name, last name So each data item consists of an integer value and two string values

C structures – some basics Structures are user-defined types with multiple data fields –e.g., define structure to hold a char and a double : struct example { /* type is 2 words: struct example */ char c; double d; }; /* the semicolon is mandatory */ Create objects and assign pointers like any other type struct example e, *ep = &e; Effect of assignment statement just like other data types struct example e2 = e; /* makes copy of whole object */ struct example *ep2 = ep; /* now both ep and ep2 point at e */

More structure basics Access internal fields with the dot ‘. ’ operator –By using the structure’s name: e.d = 2.5; –Or the pointer: (*ep).c = 'a'; /* parenthesis needed */ Easier to use the arrow ‘ -> ’ operator for pointers –ep->c = 'a'; Can initialize all fields in one step: –struct example e2 = {'c', }; Note – size of structure >= sum of field sizes: –sizeof e >= sizeof(char) + sizeof(double)

We can give names to types We can give names to complicated types to make it easier to use them later on in declarations Precede a declaration with typedef to give a new name to a type Useful for complicated type declarations

We can give names to types Example, we define a new type “ structure temperature ” struct temperature { char unit; /* e.g., 'F' or 'C' */ double value; }; Now we can give it a new name (like a nickname) “ Temperature ”: typedef struct temperature Temperature; Temperature t, *tp; Can even use one defined type to define another: typedef Temperature *TemperaturePointer; TemperaturePointer tp; /* tp same as above */

Why need user-defined types? Answer: to represent more complicated things than numbers, characters and other primitives For example, a structure like the one in our example could represent temperatures for particular units: struct temperature { char unit; /* e.g., 'F' or 'C' */ double value; }; struct temperature bodyNormal = {'F', 98.6}; Example from the book: struct hurricane hurricane.c, and hurricane2.c

Passing structures to functions Actually passing a copy of the whole structure – just like assignment statement –Unlike passing array name (which passes the pointer value, not the contents) Example: hurricane3.c Often want to pass pointers instead –Avoids copy costs – some structures very big –Gives the function access to internal fields of the original object (by –> ) –Careful though: sometimes it’s not what you want

Returning structures Function get_info() for example (end of Section 7.2) struct tsunami get_info(void) {…} –Returns temporary struct (calling program copies it by assignment) Often return pointers too: struct tsunami* aFunction(void) {…} –But only okay if dynamic memory allocation used – do not return a pointer to a local structure variable! BTW: notice how defined types aid readability: –First: typedef struct tsunami Tsunami; –Above becomes: Tsunami* aFunction(void) {…}

Collections of structures Arrays of structures – an alternative to “parallel arrays” –Mostly handle like all other array types Temperature temps[7]; /* store a week’s worth */ temps[0].value = 74; /* sets field inside first element */ Example hurricane4.c Also note: you can have arrays in structures Often use array of pointers to structures (with malloc)