Lab 2: More Advanced C

Similar documents
CMPT 300. Operating Systems. Brief Intro to UNIX and C

Intermediate Programming, Spring 2017*

Personal SE. C Struct & Typedef Make

PRINCIPLES OF OPERATING SYSTEMS

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

CIS 190: C/C++ Programming. Lecture 2 Pointers and More

Software project Gnome Graphics

Programming in C - Part 2

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

CS Basics 15) Compiling a C prog.

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

CSE 351. Introduction & Course Tools

Introduction to Supercomputing

Kurt Schmidt. October 30, 2018

Lab 03 - x86-64: atoi

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CS240: Programming in C

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

Overview of today s lecture. Quick recap of previous C lectures. Introduction to C programming, lecture 2. Abstract data type - Stack example

Main differences with Java

Personal SE. Functions, Arrays, Strings & Command Line Arguments

MIDTERM EXAM. CS 217 October 28, Name: Precept: Honor Code: Score: Problem Score Max

INTRODUCTION 1 AND REVIEW

The Basics of C Programming

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

CS240: Programming in C. Lecture 2: Overview

Intermediate Programming, Spring 2017*

Student Number: Computer Science 211b Final Examination. 28 April hours

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C

Data and File Structures Laboratory

Makefiles SE 2XA3. Term I, 2018/19

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSE 333 Midterm Exam 7/27/15 Sample Solution

Getting Started. Project 1

TDDB68 Concurrent Programming and Operating Systems. Lecture 2: Introduction to C programming

EL2310 Scientific Programming

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

FUNCTIONS POINTERS. Pointers. Functions

SWEN-250 Personal SE. Introduction to C

Week 7 Part I. Kyle Dewey. Monday, August 6, 12

GDB and Makefile. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

COMP26912: Algorithms and Imperative Programming

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

CSE 333 Midterm Exam 7/29/13

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

Introduction to the C Programming Language

15213 Recitation Section C

CSE 303, Spring 2009 Final Exam Wednesday, June 10, 2009

Link Edits and Relocatable Code

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

CS 61c: Great Ideas in Computer Architecture

Outline. COMP 2718: Software Development Tools: gcc and make. C and Java 3/28/16

Final CSE 131B Spring 2004

Introduction to Computer Systems. Exam 2. April 11, Notes and calculators are permitted, but not computers.

CSE 333 SECTION 3. POSIX I/O Functions

C introduction: part 1

Lecture 03 Bits, Bytes and Data Types

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

EL6483: Brief Overview of C Programming Language

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

(T) x. Casts. A cast converts the value held in variable x to type T

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Programming in C S c o t t S c h r e m m e r

Systems Programming/ C and UNIX

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

Final assignment: Hash map

Tutorial 1 C Tutorial: Pointers, Strings, Exec

Programming in C week 1 meeting Tiina Niklander

CS3157: Advanced Programming. Announcement

Homework Assignment #2 (revised)

Lecture 8: Structs & File I/O

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

Combinational vs. Sequential. State Machine. Computing Layers. Chapter 3 Digital Logic Structures. ! Another type of sequential circuit

Midterm CSE 131B Spring 2005

CSCI 2132 Final Exam Solutions

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

CSE 333 Midterm Exam 5/9/14 Sample Solution

Question 1. Part (a) CSC 209H1 S 2017

CS24 Week 2 Lecture 1

Creating a Shell or Command Interperter Program CSCI411 Lab

Programming in C First meeting

C mini reference. 5 Binary numbers 12

Data Representation and Storage

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

CSE 333 Midterm Exam 7/25/16. Name UW ID#

A Fast Review of C Essentials Part I

Exercise Session 2 Systems Programming and Computer Architecture

CS201 - Lecture 1 The C Programming Language

Exercise 1: Basic Tools

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Contents of Lecture 3

C Language Summary (Continued)

Transcription:

Lab 2: More Advanced C CIS*2520 Data Structures (S08) TA: El Sayed Mahmoud This presentation is created by many TAs of previous years and updated to satisfy the requirements of the course in this semester.

Outline Part 1 The exercise "squish" Part 2 Pointers Typedef Advanced Aggregate Data Types Unions More structs Makefiles

The exercise : "squish" Write a program called "squish" that replaces sequences of one or more blanks with a single blank. This program reads its input from standard input, and writes its output to standard output (in the process of copying its input to its output it replaces the sequences of multiple blanks by a single blank.

Hints for The exercise : "squish" Use Character by character processing. Read from stdin like any other stream Redirect a file on the command-line if desired; i.e. squish < somefile.txt Use gets to read string from user input or getchar to read a character by character. Process the entire input not just a single string. Print the result back using printf or puts.

Part2 : Pointers Essentially an unsigned integer type Stores the address of data in memory The offset from the first byte of memory Consider: int x = 42; int *p = &x; Pointer p stores the address of variable x

What about different variable sizes? But variables are different sizes Integers are 4 bytes Strings (character arrays) are long In the previous example, p stores the address of the first byte of x

Using Pointers Consider: int y = *p; Take the 4 bytes pointed to by p and store them in variable y Consider: *p = 24; Store the value 24 in the 4 bytes pointed to by p The old variable x is now 24

Pointer Manipulation Let p be a pointer. Consider: p = 1000; p++; What value does p have? It depends on the type of pointer! (int *) then 1004 (char *) then 1001 (void *) then a runtime error

Void pointers? Just a generic address No size information Cannot be used without casting Sidebar: The value NULL is #define NULL ((void*)0) Located in stddef.h

Arrays of Pointers Can be declared dynamically or statically Static: int * arr[2]; arr[0] = &x; Dynamic: int ** arr = (int**)malloc(sizeof(int*)*2); arr[0] = &x; free(arr);

Pointers and Parameters By default, C uses pass-by-value Function creates a copy of the value you pass in Changes to the value within the function don t change the original Can use pointers to pass-by-reference Function receives pointer to original value Changes made within the function are retained after Not truly pass-by-reference because a copy is made of the pointer, but it the result is the same

Pointers and Parameters (cont) Example: void foo (int x, int * y) { x = 3; (*y) = 4; } void main () { int a = 1; int b = 2; foo (a, &b); /* a == 1 */ /* b == 4 */ }

Typedef Allows you to create your own custom types typedef <custom type> <new identifier> Not-useful example: typedef int CustomInt; Somewhat-useful example: typedef int* IntPtr; Most useful example: typedef struct rec record;

Combining Typedef with Struct Why use 2 lines when you could use 1? typedef struct rec { char[10] name; } record;

Unions Similar to struct in form Uses overlapping memory While structure enables us treat a number of different variables stored at different in memory, a union enables us to treat the same space in memory as a number of different variables Example: union PersonalID { char [30] name; long sin; }

Unions (cont.) Continuing example: union PersonalID pid; pid.name = Andrew Baker ; pid.sin = 123456789; At the end of this execution, accessing pid.name will not give you Andrew Baker

Copying Structs Example: struct TA { char * name; int empnum; } struct TA ab, jn; ab.name = (char*)malloc(10); strcpy (ab.name, Andrew ); ab.empnum = 123; jn = ab; strcpy (jn.name, JingBo ); jn.empnum = 234; At the end of this execution? What are the values of the following: ab.name? ab.empnum? jn.name? jn.empnum?

Copying Structs Example: struct TA { char * name; int empnum; } struct TA ab, jn; ab.name = (char*)malloc(10); strcpy (ab.name, Andrew ); ab.empnum = 123; jn = ab; strcpy (jn.name, JingBo ); jn.empnum = 234; At the end of this execution? What are the values of the following: ab.name? JingBo ab.empnum? 123 jn.name? JingBo jn.empnum? 234 Assignment (=) only does a shallow copy Pointers are copied, but end up pointing to the same place

Single Source file Compilation prg.c - Source Code -Variable def. Lib.h - Struct -Constant def. -Function declarations Compile prg.s Assemble Compiling a Source File prg.o Link prg.out

Several Source file Compilation prg.c - Source Code -Variable def. gcc prg.c Lib.h - Struct -Constant def. -Function declarations prg.s prg1.s prg.o prg1. o proj.out prg1.c - Source Code -Variable def. gcc prg1.c gcc prg.o prg1.o

Makefiles A makefile is a file (script) containing : Project structure (files, dependencies) Instructions for files creation Basic structure: Variables <target> : <dependencies> <\tab> <command> Can list numerous variables Can list multiple targets, each with multiple dependencies and commands By default, the first target is run when you type make in the console

Makefile Variables Generally, use all capitals Declared as: CC = gcc (compiler selection) LIBS= -lm (Lib files used) OPTIONS= -g - Wall To use: $(CC)$(LIBS)$(OPTIONS)

Sample Makefile CC = gcc LIBS = -lm FLAGS = -Wall -std=c99 - ansi OBJFILES = main.o util.o PROJFILES = main.h main.c util.h util.c PROGNAME = myprog all: $(PROGNAME) $(PROGNAME): $(OBJFILES) $(CC) $(OBJFILES) -o $(PROGNAME) $(LIBS) #build the main object main.o: main.c pgm.h util.h $(CC) $(FLAGS) -c main.c #build the utility object util.o: util.c util.h $(CC) $(FLAGS) -c util.c #remove files of compilation clean: rm $(OBJFILES) $(PROGNAME) #creates a date/time-stamped #backup backup: $(PROJFILES) mkdir 00-backup-`date +'%Y- %m-%d-%h%m'` cp $(PROJFILES) 00-backup- `date +'%Y-%m-%d-%H%M'`

Thank you Any questions?