8. Structures, File I/O, Recursion 18 th October IIT Kanpur 1C Course, Programming club, Fall 2008.

Slides:



Advertisements
Similar presentations
C: Advanced Topics-II Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Advertisements

A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
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.
Programming in C Chapter 10 Structures and Unions
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
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.
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”)
Structures Spring 2013Programming and Data Structure1.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
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 +
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
C and Data Structures Baojian Hua
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
Structures and Unions Chapter 6. Structure A structure is an aggregate data type  Composed of two or more related variables called member/field/element.
1 Functions and Structured Programming. 2 Structured Programming Structured programming is a problem-solving strategy and a programming methodology. –The.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Functions, Pointers, Structures Keerthi Nelaturu.
1 Homework HW6 due class 22 K&R 6.6 K&R 5.7 – 5.9 (skipped earlier) Finishing up K&R Chapter 6.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
File IO and command line input CSE 2451 Rong Shi.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
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.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Homework Finishing up K&R Chapter 6 today Also, K&R 5.7 – 5.9 (skipped earlier)
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.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
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.
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.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
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
C Structures and Memory Allocation
5.13 Recursion Recursive functions Functions that call themselves
A bit of C programming Lecture 3 Uli Raich.
Structure, Unions, typedef and enumeration
Functions and Structured Programming
Chapter7 Structure & C++
CSE 374 Programming Concepts & Tools
14th September IIT Kanpur
Lecture 9 Structure 1. Concepts of structure Pointers of structures
C Structures, Unions, Bit Manipulations and Enumerations
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Chapter 14 - Advanced C Topics
File Handling in C.
Chapter 9: Pointers and String
C Structures and Memory Allocation
Structures Declarations CSCI 230
Presentation transcript:

8. Structures, File I/O, Recursion 18 th October IIT Kanpur 1C Course, Programming club, Fall 2008

Basic of Structures Definition: A collection of one or more different variables with the same handle (same name). C Course, Programming club, Fall struct point { char name[30]; int x; int y; double temperature; } struct point pt; struct point { …. } pt, pt1;

Basic of Structures contd… Access an element Example {program: basic_of_structures.c} C Course, Programming club, Fall structure-name.member printf(“x = %d, y = %d\n”, pt.x, pt.y);

Basic of Structures contd… Structs can also contain other structs. To access its element: C Course, Programming club, Fall struct rectangle { struct point pt1; struct point pt2; }; struct rectangle rect; rect.pt1.x;

Structures and Functions When structures are passed into functions all of their values are copied. (pass by value) A function must return the structure to affect the target structure. {program: structures_and_functions.c} {program: structures_and_functions1.c} This is a lot of copying of variable values onto and off the stack. (inefficient) Pointers will be used to make this better. C Course, Programming club, Fall 20085

Arrays of Structures Array of Structures act like any other array. Memory occupied: the dimensions of the array multiply by sizeof(struct tag) – (Remember) sizeof() is compile time function C Course, Programming club, Fall struct point pt[3]; pt[0].name = “A”; pt[0].x = 0; pt[0].y = 1; pt[1].name = “B”; pt[1].x = 4; pt[1].y = 1; pt[2].name = “mid”; pt[2].x = (pt[0].x + pt[1].x)/2; pt[2].y = (pt[0].y + pt[1].y)/2;

Pointers to Structures Pointers are an easier way to manipulate structure members by reference The entire structure is not passed by value, only the address of the first member Use arrow operator for accessing the struct element C Course, Programming club, Fall struct Date MyDate, *DatePtr; DatePtr = &MyDate; DatePtr->month = 2; DatePtr->day = 22;

Pointer to Structures contd… Example {program: structures_and_functions_wPtr.c} C Course, Programming club, Fall struct Date { int month; int day; int year; }; void AddDecade(struct Date *tmp) { tmp->year += 10;// or (*tmp).year += 10; }

Self referencing Structures Useful in data structures like trees, linked lists. It is illegal for a structure to contain an instance of itself. – Soln: Have a pointer to another instance. C Course, Programming club, Fall struct tnode { /* the tree node */ char *word; int count; struct tnode *left; /* left child */ struct tnode *right; /* right child */ };

Typedef Use typedef for creating new data type names this the name length a synonym for int. Afterwards, you can do: In context of structs, you can do: C Course, Programming club, Fall typedef int length; length number = 4; typedef struct tnode *TreePtr; typedef struct tnode {. } TreeNode;

Unions A union is a memory location that is shared by two or more different types of variables. Each of ival, fval, cval have the same location in memory. Usage is similar to that of structs: C Course, Programming club, Fall union u_tag { int ival; float fval; char cval; } u; u.ival or u.cval

Bit-fields When storage is high cost affair, we need to use memory efficiently (e.g in embedded systems) Here each of the element takes a bit of memory (1 bit) The number following the colons represent the field length in bits. C Course, Programming club, Fall struct { unsigned pin1 : 1; unsigned pin2 : 1; unsigned pin3 : 1; } flags;

FILE I/O The file pointer Opening a file Modes – r : read, w: write, a: append, r+ : read and create if file does not exist, w+, a+, rb, wb, ab, r+b, r+w, r+a Closing a file FILE *fp; FILE *fp = fopen(“data.txt”, “r”); fclose(fp); 13C Course, Programming club, Fall 2008

FILE I/O contd… Some functions for file I/O fopen()opens a file fclose()closes a file fputc()writes a character to a file fgetc()reads a character from a file fputs()writes a string to a file fgets()reads a string to a file fseek()change file position indicator ftell()returns to file position indicator fprintf()similar to printf(), but to a file instead of console fscanf()similar to scanf(), but to a file instead of console remove()deletes the file fflush()flushes the file pipe 14C Course, Programming club, Fall 2008

Supplement topic – I/O from console Reading from console During program execution – printf(), scanf(), putc(), getc() Just before execution starts (parameters passed to the program) – argc: number of arguments (in above case, 5) – argv: pointer to array of char pointers $./a.out 3 santa_singh banta_singh happy_singh int main(int argc, char *argv[]) 15C Course, Programming club, Fall 2008

More supplement - Recursion Recursion is when a function calls itself. – Great Utility – Makes the code easier Requirements to use recursion – A condition to cease at otherwise the program would never terminate the condition is usually written at the beginning of the recursive method 16C Course, Programming club, Fall 2008

Recursion contd… example: /* recursive */ int factr(int n) { int answer; if(n==l) return(1); answer = factr(n-l)*n; /* recursive call */ return(answer); } /* non-recursive */ int fact(int n) { int t, answer; answer = 1; for(t=1; t<=n; t++) answer=answer*(t); return(answer); } 17C Course, Programming club, Fall 2008