Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

Similar documents
Lecture 04 Introduction to pointers

Today s Learning Objectives

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Pointers. Pointers. Pointers (cont) CS 217

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Lecture 07 Debugging Programs with GDB

First of all, it is a variable, just like other variables you studied

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Pointers, Dynamic Data, and Reference Types

Arrays and Pointers in C. Alan L. Cox

Goals of this Lecture

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Lab 3. Pointers Programming Lab (Using C) XU Silei

[0569] p 0318 garbage

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

today cs3157-fall2002-sklar-lect05 1

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Pointers. Pointer References

CSCI 171 Chapter Outlines

CS 61c: Great Ideas in Computer Architecture

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Homework #3 CS2255 Fall 2012

Variation of Pointers

Understanding Pointers

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Language comparison. C has pointers. Java has references. C++ has pointers and references

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

Introduction to C. Systems Programming Concepts

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

Pointers. Memory. void foo() { }//return

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

High Performance Programming Programming in C part 1

Introduction to Computer Science Midterm 3 Fall, Points

ESC101N Fundamentals of Computing

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

Fundamental of Programming (C)

C Programming Language Review and Dissection III

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

EM108 Software Development for Engineers

Lectures 5-6: Introduction to C

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

Array Initialization

POINTER & REFERENCE VARIABLES

Computer Programming Unit 3

Intermediate Programming, Spring 2017*

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

DECLARAING AND INITIALIZING POINTERS

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

ECE 15B COMPUTER ORGANIZATION

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Lecture 03 Bits, Bytes and Data Types

Pointers in C/C++ 1 Memory Addresses 2

Kurt Schmidt. October 30, 2018

What is an algorithm?

Compiling and Running a C Program in Unix

Pointers and Arrays 1

Arrays and Pointers (part 1)

PDS Class Test 2. Room Sections No of students

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

CSC 2400: Computer Systems. Arrays and Strings in C

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Pointers. 10/5/07 Pointers 1

Computers Programming Course 10. Iulian Năstac

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Lectures 5-6: Introduction to C

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Approximately a Test II CPSC 206

Dynamic Memory Allocation

Low-Level C Programming. Memory map Pointers Arrays Structures

SYSC 2006 C Winter 2012

C-LANGUAGE CURRICULAM


Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

Computer System and programming in C

by Pearson Education, Inc. All Rights Reserved.

Transcription:

15-123 Effective Programming in C and Unix Announcements SL2 is due Thursday 1/20 midnight Complete the Academic Honesty Form in class All code downloads are from /afs/andrew/course/15/123/download All code handin sare at /afs/andrew/course/15/123/handin All SL/lab feedback are given at /afs/andrew/course/15/123/handback Topics so far Review How data is read scanf/fscanf How do we read a file that contains one integer per line? RAM File in hard drive How do we read a file that contains two intsper line? How do we read a file that contains an intchar and a string? What happens if scanf( %d, x) (where x is an int) is executed? 1

Question What is the purpose of these function? What is the bug in the code? next is the index of the next available location in the array. For example if array has 5 elements then next is 5 When will these functions fail? Efficiency in operations What is the relation between x = x + 1 x+= 1 x++ or ++x Semantics eg : inti=1; while (i++ < 10) {statement} how many times statement executed? eg : inti=1; while (++i < 10) {statement} how many times statement executed? A pointer to a character denoted by: char* char* reads address of a character (a.k.astring) G U N A \0 char* s means s is a pointer to character or address of a character char* s char *s char * s Are all equivalent Does not matter where the * is placed Strings A String is an array characters char word[n]; Strings are mutable word[0] = a ; Strings ends with \0 the null character (IMPORTANT) Reading a String fscanf(fp, %s, word); Comparing Strings strcmp(s1, s2) > 0 or = 0 or < 0 (need <string.h>) Strings ctd.. char word[n]; word is the name of the array word is also the starting address of word array word is also a char*, that is a pointer to a character word is a const pointer to the array Is it possible to say: word = w1 if w1 is another string? How do we copy strings? strcpy(s1, s2) 2

Important String functions Arrays Syntax <type> name_of_array[size_of_array]; Eg: int A[n] <type>[ ] name_of_array; Eg: int[] A; Semantics A contiguous block of memory that holds <size_of_array> units of <type> variables Name of the array is a pointer to the block Boundaries Indices vary from 0,1, size-1 (why?) C does not check array boundaries 2D Arrays Syntax <type> A[rows][cols]; char A[10000][32]; <type>[][] A; char[][] A; Semantics A contiguous block of memory to hold (rows*cols) values of <type> Memory representation Since memory is 1D, 2D arrays are represented as a block long block of a 1D array How does it then know the segmentation of columns? 2D Arrays ctd.. Conceptually 12 34 56 57 78 90 Actually 12 34 56 57 78 90 Hence column dimension is important int foo(char A[][32],..) Suppose you are given n(rows) by m(cols) intarray A as a 1D array of m*n ints. What expression represent the (i,j)-th element of the array? 2D array of char s Read file Me Guna Him Hello There file Array in RAM 3

Insert in order (strings) Write to file Learning Objectives New material Understand how pointers work Understand how to access memory using pointers Understand pointer arithmetic Understand relation between arrays and pointers Understand the dangers of indirect memory access Definition of a pointer A pointer is an address of a memory location Pointer variables <type>* variable_name; int* ptr; char* ptr; intx = 10 Initializing int* ptr= &x ; Dereferencing *ptr= x; 4

Pointer Arithmetic <type>* ptr; ptr address of a memory location ptr+ 1 address of the next <type> variable ptr1 ptr2 number of type variables between ptr1 and ptr2 Accessing Memory using pointers Suppose an intis defined as intx = 10 How do we print the address of x? How do we access the first byte of x? How do we access the second byte of x? How do we access the next 4 bytes after x? What are the dangers? Arrays Definition int A[10] char* A[10] int* A[10] Array Memory allocation Allocates a Contiguous block of memory The name of the array A is a constant pointer to the first element of the array inta[10]; printf( %x, A); Array as a pointer Calculate the addresses of each element Understanding Arrays A address of the first element of the array A + i = address of A[i] = &A[i] Dynamic Arrays int* A = (int*)malloc(n*sizeof(int)); for (i=0; i<n; i++) A[i] = i; Resizing an array 5

C programming include <stdio.h> in all your programs Declare functions and variables before using them increment and decrement with ++ and operators. Use x += 5 instead of x = x + 5 A string is an array of characters ending with a \0. Don t ever forget the null character. Array of size n has indices from 0 to n-1. Although C will allow you to access A[n] it is very dangerous. A character can be represented by an integer (ASCII value) and can be used as such. The unary operator & produces an address The unary operator * dereference a pointer Arguments to functions are always passed by value. But the argument can be an address of just a value For efficiency, pointers can be passed to or return from a function. Logical false is zero and anything else is true You can do things like for(;;) or while(i++) for program efficiency and writability Use /*.. */ instead of // Always compile your program with ansi pedantic Wall flags Coding Examples 6