Structures CSE 2031 Fall 2011 19 March 2016. Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.

Slides:



Advertisements
Similar presentations
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Advertisements

Programming in C Chapter 10 Structures and Unions
Lectures 10 & 11.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
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.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Programming and Data Structure
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 A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
1 6.1 Basics of Structures 6.2 Structures and Functions 6.3 Arrays of Structures 6.4 Pointers to Structures 6.5 Self-referential Structures + malloc +
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
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.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
ISP - 2 nd Recitation Functions Pointers Structs Files Code Examples Homework!
C and Data Structures Baojian Hua
C language issues CSC 172 SPRING 2002 EXTRA LECTURE.
C Slides and captured lecture (video and sound) are available at:
C Slides and captured lecture (video and sound) are available at:
1/18 Strukture. 2/ Osnove struktura struct point { int x; int y; }; y x0, 0 (4, 3) struct { … } x, y,
8. Structures, File I/O, Recursion 18 th October IIT Kanpur 1C Course, Programming club, Fall 2008.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Introduction to Data Structures Systems Programming.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
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.
1 6.1 Basics of Structures 6.2 Structures and Functions 6.3 Arrays of Structures 6.4 Pointers to Structures 6.5 Self-referential Structures + Linked List.
Introduction to Data Structures Systems Programming Concepts.
Struct Advanced Programming Language. Struct Struct allows us to create a new data type We can now declare “struct Point” as a new data type and can use.
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.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
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.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 5.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
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.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
D. GotsevaPL-Lectures1 Programming Languages Lectures Assoc. Prof. PhD Daniela Gotseva
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
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
C Structures and Memory Allocation
Computer Organization and Design Pointers, Arrays and Strings in C
5.13 Recursion Recursive functions Functions that call themselves
Homework / Exam Continuing K&R Chapter 6 Exam 2 after next class
CS1100 Computational Engineering
EECE.2160 ECE Application Programming
Review & Lab assignments
CS111 Computer Programming
Homework Continuing K&R Chapter 6.
Structures CSE 2031 Fall February 2019.
Programming Language C Language.
Structures CSE 2031 Fall April 2019.
Structures CSE 2031 Fall May 2019.
CS1100 Computational Engineering
Structures EECS July 2019.
C Structures and Memory Allocation
Presentation transcript:

Structures CSE 2031 Fall March 2016

Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration. point : structure tag x, y : members The same member names may occur in different structures. Now struct point is a valid type. Defining struct variables: struct point pt; struct point maxpt = {320, 200}; A struct declaration defines a type. struct {... } x, y, z; or struct point x,y,z; is syntactically analogous to int x, y, z; 2

Using Structures Members are accessed using operator “.” structure-name.member printf(“%d,%d", pt.x, pt.y); double dist, sqrt(double); dist = sqrt((double)pt.x * pt.x + (double)pt.y * pt.y); Structures cannot be assigned. struct point pt1, pt2; pt1.x = 0; pt1.y = 0; pt2 = pt1; /* WRONG !!! */ 3

Structure Name Space Structure and members names have their own name space separate from variables and functions. struct point point; /* both are valid */ or struct point { int x; int y; } x; 4

Nested Structures struct rect { struct point pt1; struct point pt2; }; struct rect screen; screen.pt1.x = 1; screen.pt1.y = 2; screen.pt2.x = 8; screen.pt2.y = 7; 5

Structures and Functions (6.2) Returning a structure from a function. /* makepoint: make a point from x and y components */ struct point makepoint(int x, int y) { struct point temp; temp.x = x; temp.y = y; return temp; } struct rect screen; struct point middle; struct point makepoint(int, int); screen.pt1 = makepoint(0,0); screen.pt2 = makepoint(XMAX, YMAX); middle = makepoint((screen.pt1.x + screen.pt2.x)/2, (screen.pt1.y + screen.pt2.y)/2); 6

Structures and Functions (cont.) Passing structure arguments to functions: structure parameters are passed by values like int, char, float, etc. (a copy of the structure is sent to the function). /* addpoints: add two points */ struct point addpoint(struct point p1, struct point p2) { p1.x += p2.x; p1.y += p2.y; return p1; } Note: the components in p1 are incremented rather than using an explicit temporary variable to emphasize that structure parameters are passed by value like any others (no changes to original struct). 7

Pointers to Structures If a large structure is to be passed to a function, it is generally more efficient to pass a pointer than to copy the whole structure. struct point *pp; struct point origin; pp = &origin; printf("origin is (%d,%d)\n", (*pp).x, (*pp).y); Note: *pp.x means *(pp.x), which is illegal (why?) 8

Pointers to Structures: Example /* addpoints: add two points */ struct point addpoint (struct point *p1, struct point *p2) { struct point temp; temp.x = (*p1).x + (*p2).x; temp.y = (*p1).y + (*p2).y; return temp; } main() { struct point a, b, c; /* Input or initialize structures a and b */ c = addpoint( &a, &b ); } 9

Pointers to Structures: Shorthand (*pp).x can be written as pp->x printf("origin is (%d,%d)\n", pp->x, pp->y); struct rect r, *rp = &r; r.pt1.x = 1; rp->pt1.x = 2; (r.pt1).x = 3; (rp->pt1).x = 4; Note: Both. and -> associate from left to right. 10

Pointers to Structures: More Examples The operators. and -> along with ( ) and [ ] have the highest precedence and thus bind very tightly. struct { int len; char *str; } *p; ++p->len  ++(p->len) (++p)->len (p++)->len  p++->len *p->str *p->str++ (*p->str)++ *p++->str 11

Arrays of Structures (6.3) struct dimension { float width; float height; }; struct dimension chairs[2]; struct dimension *tables; tables = (struct dimension*) malloc (20 * sizeoff(struct dimension)); 12

Initializing Structures struct dimension sofa = {2.0, 3.0}; struct dimension chairs[] = { {1.4, 2.0}, {0.3, 1.0}, {2.3, 2.0} }; 13

Arrays of Structures: Example struct key { char *word; int count; }; struct key keytab[NKEYS]; struct key *p; for (p = keytab; p < keytab + NKEYS; p++) printf("%4d %s\n", p->count, p->word); struct key { char *word; int count; } keytab[] = { "auto", 0, "break", 0, "case", 0, "char", 0, "const", 0, "continue", 0, "default", 0, /*... */ "unsigned", 0, "void", 0, "volatile", 0, "while", 0 }; 14

Pointers to Structures (6.4) struct key keytab[NKEYS]; struct key *p; for (p = keytab; p < keytab + NKEYS; p++) printf("%4d %s\n", p->count, p->word); p++ increments p by the correct amount (i.e., structure size) to get the next element of the array of structures. struct { char c; /* one byte */ int i; /* four bytes */ }; What is the total structure size? Use the sizeof operator to get the correct structure size. 15

Self-referential Structures (6.5) Example: (singly) linked list struct list { int data; struct list *next; }; 16 3

Linked List Pointer head points to the first element Last element pointer is NULL Example (next slide): build a linked list with data being non-negative integers, then search for a number.  Insertion at the end (rear) of the list. We also learn how to dynamically allocate a structure NULL head 17

Linked List Implementation #include main() { struct list { int data; struct list *next; } *head, *p, *last; int i; /* Create a dummy node, which simplifies insertion and deletion */ head = (struct list *) malloc ( sizeof( struct list ) ); head─>data = -1; head─>next = NULL; last = head; scanf( “%d”, &i ); /* input 1 st element */ while( i >= 0 ) { p = (struct list *) malloc( sizeof( struct list ) ); p─>data = i; p─>next = NULL; last─>next = p; last = p; scanf( “%d”, &i ); } /* while */ printf(“Enter the number to search for “); scanf( “%d”, &i ); for( p = head; p != NULL; p = p─>next ) if( p─>data == i ) printf( "FOUND %d \n“, i ); } /* main */ 18

typedef (6.7) For creating new data type names typedef int Length; Length len, maxlen; Length *lengths[]; typedef char *String; String p, lineptr[MAXLINES]; p = (String) malloc(100); int strcmp(String, String); 19

typedef with struct We can define a new type and use it later typedef struct { int x,y; float z; } mynewtype; mynewtype a, b, c, x; Now, mynewtype is a type in C just like int or float. 20

Self-referential Structures: More Examples Binary trees (6.5) Hash tables (6.6) To be covered later if time permits. 21

Reminders Midterm (Oct. 31) Lab test 1 (Oct. 28 and 31) Next lecture: Pointers part 2 22