CSE 303 Concepts and Tools for Software Development

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Arrays and Pointers in C Alan L. Cox
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Introduction to Programming
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
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”
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
Object Oriented Programming Lecture 2: BallWorld.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
CSE 374 Programming Concepts & Tools
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 8 Arrays, Strings and Pointers
CSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools
A bit of C programming Lecture 3 Uli Raich.
Characters and Strings
Memory, Data, & Addressing II CSE 351 Autumn 2017
Lecture 8 String 1. Concept of strings String and pointers
C Programming Tutorial – Part I
Command Line Arguments
Objectives Identify the built-in data types in C++
Lecture-5 Arrays.
CSE 374 Programming Concepts & Tools
Command-Line Arguments
C Basics.
Arrays in C.
typedef typedef int Index; typedef char Letter; Index i; i = 17;
Programming Languages and Paradigms
Lecture 6 C++ Programming
Computer Science 210 Computer Organization
Lecture 8b: Strings BJ Furman 15OCT2012.
C Stuff CS 2308.
Chapter 15 Pointers, Dynamic Data, and Reference Types
C Arrays.
7 Arrays.
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Pointers, Dynamic Data, and Reference Types
CSE 303 Concepts and Tools for Software Development
Memory, Data, & Addressing II CSE 351 Winter 2018
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Outline Defining and using Pointers Operations on pointers
Chapter 16 Pointers and Arrays
Memory, Data, & Addressing II CSE 351 Winter 2018
EECE.2160 ECE Application Programming
Chapter 9: Pointers and String
Programming in C Pointers and Arrays.
Functions Reasons Concepts Passing arguments to a function
Characters and Strings
Pointers, Dynamic Data, and Reference Types
C Pointers Another ref:
CSE 303 Concepts and Tools for Software Development
SPL – PS2 C++ Memory Handling.
CSE 303 Concepts and Tools for Software Development
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Presentation transcript:

CSE 303 Concepts and Tools for Software Development CSE 303 Lecture 9 10/16/2006 CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/16/2006 Lecture 9 – C Arrays and Strings

Administravia Homework #2 is due! Societal Implications Assignment #1 Due next lecture! 10/16/2006 CSE 303 Lecture 9

Tip of the Day Emacs Macros C-x ( : Start recording Macro All keystrokes recorded C-x ) : Stop recording Macro C-x e : run macro C-u <num> C-x e : run macro <num> times C-u runs any command multiple times 10/16/2006 CSE 303 Lecture 9

Last Time C-Program Structure Syntax Printf Declarations and Scope Argument Passing Syntax Booleans and arrays Confusing Cases Printf 10/16/2006 CSE 303 Lecture 9

Today Syntax Command-line Arguments Type names Arrays Strings 10/16/2006 CSE 303 Lecture 9

Type Names Can usually tell from declaration int i; : i has type (int) float f; : f has type (float) Pointers are a little more confusing int *p; : p has type (int*) or (int *) int **p; : p has type (int**) or (int **) int* p; : p has type (int*) or (int *) int* p,q; : q has type (int) We’ll see later when we discuss casting 10/16/2006 CSE 303 Lecture 9

Arrays in C Array : group of memory locations with Example: Same name Same type Example: int i; int c[3]; int j=23; for (i=0; i<3; i++) { c[i]=0; } Stack i 3 c[0] Increasing Addresses c[1] c[2] j 23 Note: Outside of the array, the order of variables is not defined. 10/16/2006 CSE 303 Lecture 9

Array Bounds Checking No array bounds check Not done by compiler Not done by run time system You must add checks or live without them Big source of errors 10/16/2006 CSE 303 Lecture 9

Pointer Arithmetic int i; int c[3]; int j=23; for (i=0; i<3; i++) { printf("%d\n", c[i]); printf("%d\n", *(c+i)); } Type of array name is pointer! Refers to address of first element in array Examples in simpleArray.c Stack i 3 c c[0] c+1 c[1] c+2 c[2] j 23 10/16/2006 CSE 303 Lecture 9

Example 1 c[0] = 13; c[2] = 42; int *p = &c[4]; *p = 54; p++; *p = 64; Stack c[0] 1 2 3 4 5 6 7 8 10/16/2006 CSE 303 Lecture 9

Example 1 c[0] = 13; c[2] = 42; int *p = &c[4]; *p = 54; p++; *p = 64; Stack c[0] 13 1 42 3 4 5 6 7 8 10/16/2006 CSE 303 Lecture 9

Example 1 c[0] = 13; c[2] = 42; int *p = &c[4]; *p = 54; p++; *p = 64; Stack c[0] 13 1 42 3 p 4 5 6 7 8 10/16/2006 CSE 303 Lecture 9

Example 1 c[0] = 13; c[2] = 42; int *p = &c[4]; *p = 54; p++; *p = 64; Stack c[0] 13 1 42 3 p 54 5 6 7 8 10/16/2006 CSE 303 Lecture 9

Example 1 c[0] = 13; c[2] = 42; int *p = &c[4]; *p = 54; p++; *p = 64; Stack c[0] 13 1 42 3 54 p 5 6 7 8 10/16/2006 CSE 303 Lecture 9

Example 1 c[0] = 13; c[2] = 42; int *p = &c[4]; *p = 54; p++; *p = 64; Stack c[0] 13 1 42 3 54 p 64 6 7 8 10/16/2006 CSE 303 Lecture 9

Example 2 int i; for (i = 0; i <= 8; i++) { c[i] = c[i] + 10; } 1 2 3 4 5 6 7 8 int i; for (i = 0; i <= 8; i++) { c[i] = c[i] + 10; } c 10 11 12 13 14 15 16 17 18 10/16/2006 CSE 303 Lecture 9

Example 3 int i; for (i = 0; i <= 8; i++) { *(c+i) = *(c+i) + 10; } 1 2 3 4 5 6 7 8 int i; for (i = 0; i <= 8; i++) { *(c+i) = *(c+i) + 10; } c c+1 c+3 c 10 11 12 13 14 15 16 17 18 10/16/2006 CSE 303 Lecture 9

Example 4 int *p; for (p = c; p <= c+8; p++) { *p = *p + 10; } c[0] 1 2 3 4 5 6 7 8 int *p; for (p = c; p <= c+8; p++) { *p = *p + 10; } c c+8 c 10 11 12 13 14 15 16 17 18 10/16/2006 CSE 303 Lecture 9

Passing Arrays to Functions To pass array to function: Give name without brackets Common idiom: pass size in other arg Example: modify(c,size); Two function definition styles void modify(int c[], int size); void modify(int *c, int size); Examples in arrayArg.c 10/16/2006 CSE 303 Lecture 9

Strings Strings: Null terminator: no need to specify size Problems: An array of characters Terminated with "null character" Denoted with '\0' Character with ASCII value 0 Null terminator: no need to specify size Problems: Size of array must include space for '\0' Common bug: Overwrite '\0' 10/16/2006 CSE 303 Lecture 9

String Constants String Constants have type (char*) Automatically null terminated Example: char *str = "Hello"; str 'H' 'e' 'l' 'l' 'o' '\0' 10/16/2006 CSE 303 Lecture 9

string.h Various string utility functions Example See them with "man string" Also on p470 of Programming in C Example char s1[20] = "blue "; char s2[] = "gray"; char *s3 = strcat(s1, s2); 10/16/2006 CSE 303 Lecture 9

Arrays of Ponters Common idiom: combining multiple values char *s[3] = { "Hello", "World", "!" }; 'H' 'e' 'l' 'l' 'o' '\0' s[0] s[1] 'W' 'o' 'r' 'l' 'd' '\0' s[2] '!' '\0' 10/16/2006 CSE 303 Lecture 9

Command Line Arguments Main has other forms int main(int argc, char** argv); int main(int argc, char* argv[]); argc holds number of strings (first is cmd) argv holds strings Note use of common idioms array + length array of pointers Examples in commandLine.c 10/16/2006 CSE 303 Lecture 9

Summary Syntax Command-line Arguments Type names Arrays Strings 10/16/2006 CSE 303 Lecture 9

Reading Programming in C Chapter 7: Arrays Chapter 10: Strings pp259-271: Pointers and Arrays pp380-383: Command-Line Arguments 10/16/2006 CSE 303 Lecture 9

Next Time Structs The Heap Manual Memory Management 10/16/2006 CSE 303 Lecture 9