1 Homework Continue with K&R Chapter 5 –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Continue on HW5.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
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
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Programming in C Pointers and Arrays. 1/14/102 Pointers and Arrays In C, there is a strong relationship between pointers and arrays.In C, there is a strong.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
C Programming. C vs C++ C syntax and C++ syntax are the same but... C is not object oriented * There is no string class * There are no stream objects.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
CS1061 C Programming Lecture 15: More on Characters and Strings A. O’Riordan, 2004.
Programming in C Pointers and Arrays. 1/14/102 Pointers and Arrays In C, there is a strong relationship between pointers and arrays.In C, there is a strong.
Arrays and Pointers in C Alan L. Cox
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Some Example C Programs. These programs show how to use the exec function.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
0 Chap. 5 Pointers and Arrays 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers and Functions 5.6Pointer Arrays; Pointers to Pointers.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
1 Homework HW6 On line – due next class Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Lecture Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Homework / Exam Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next Continue HW5.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Today’s Material Strings Definition Representation Initialization
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Arrays and Pointers (part 2) CSE 2031 Fall March 2016.
Programming in C Pointers and Arrays. 1/14/102 Pointers and Arrays In C, there is a strong relationship between pointers and arrays.In C, there is a strong.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
CSE 333 – SECTION 2 Memory Management. Questions, Comments, Concerns Do you have any? Exercises going ok? Lectures make sense? Homework 1 – START EARLY!
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Content Coverity Static Analysis Use cases of Coverity Examples
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Chapter 5, Pointers and Arrays
Stack and Heap Memory Stack resident variables include:
5.13 Recursion Recursive functions Functions that call themselves
A bit of C programming Lecture 3 Uli Raich.
Characters and Strings
Command line arguments
Command Line Arguments
CSE 303 Concepts and Tools for Software Development
Programmazione I a.a. 2017/2018.
Pointers.
Arrays and Pointers CSE 2031 Fall September 2018.
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Introduction to Static Analyzer
Homework Continue with K&R Chapter 5 Skipping sections for now
Programming in C Pointers and Arrays.
Arrays and Pointers (part 2)
Arrays and Pointers (part 2)
Characters and Strings
Presentation transcript:

1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5

2 Review Pointers. int a[ ] ={1,3,5,7,9,11,13,15,17,19}; int *pa = &a[4],*pb = &a[1]; What is the value of: *(a + 2)?Same as a[2] What is the value of: pb - pa? Integer -3 What is the value of: pb[1]?Same as a[2] What is the effect of: *pa += 5?a[4] += 5 What is the effect of: *(pa += 2)?pa = &a[6], value is a[6] What is the effect of: *(a += 2)?Illegal, can't modify an array name such as a What is the value of: pa[3]? Same as a[9]

3 Valid Pointer Arithmetic Set one pointer to the value of another pointer of the same type. pa = pb; Add or subtract a pointer and an integer: pa + 3 pa - 5 Subtract two pointers to members of same array: pa – pbNote: Result is an integer Compare two pointers to members of same array: if (pa <= pb)

4 Valid Pointer Arithmetic Assign a pointer to zero (called NULL in stdio) pa = NULL; same as pa = 0; Compare a pointer to zero (called NULL in stdio) If (pa != NULL); BUT NOT if (pa > NULL) Note: a NULL pointer doesn't point to anything (When used as a return value, it indicates failure of a function that is defined to return a pointer) All other pointer arithmetic is invalid.

5 Valid Pointer Arithmetic If we add new declarations to ones on slide #2, char s[ ] = "Hello, world!", *cp = &s[4]; Which assignments below are valid?: cp = cp - 3; YES pa = cp; NO (Possible alignment problem, int’s on 4 byte boundaries) pa = pa + pb; NO s[4] = (cp < pa)? 'a': 'b'; NO s[4] = (cp > 0)? 'a': 'b'; NO cp = NULL;YES

6 Pointer Arrays, K&R 5.6 Recall that if we define char a[10]; We are setting aside space in memory for the elements of array a, but a can be treated as a pointer. We can write *a or *(a + 5). Now think about the declaration: char *a[10];

7 Pointers to Pointers What does the array a contain now? Pointers to char variables or strings! Though hard to think about, we can write: **a/* First char in string ptd to by a[0] */ *(*(a + 5) + 2) /* Third char in string ptd to by a[5] */

8 Pointers to Pointers Now what is the use of keeping an array of pointers to char strings? K&R gives an example: –Reading in a sequence of lines –Placing them in blocks of memory (e.g. malloc) –Building an array of pointers to the blocks –Sorting by moving pointers – not strings

9 Pointers to Pointers Example of pointers to unsorted char strings char *lineptr[MAXLINES]; lineptr[0]lineptr[1]lineptr[2]lineptr[3]lineptr[4] de\0 abc... k\0

10 Pointers to Pointers To initialize the array with fixed values char x[] = “k”; char y[] = “abc”; char z[] = “de”; lineptr[0] = x; /* or = &x[0]; */ lineptr[1] = y; /* or = &y[0]; */ lineptr[2] = z; /* or = &z[0]; */

11 Pointers to Pointers Examples of pointers to sorted char strings char *lineptr[MAXLINES]; lineptr[0]lineptr[1]lineptr[2]lineptr[3]lineptr[4] de\0 abc... k\0

12 Pointers to Pointers Write out lines in pointer order (easy way) void writelines(char * lineptr[], int nlines) { int i = 0; while (i < nlines) printf("%s\n", lineptr[i++]); }

13 Pointers to Pointers Write out lines in pointer order (efficiently) void writelines(char **lineptr, int nlines) { while (nlines-- > 0) printf("%s\n", *lineptr++); }

14 Command-line Arguments, K&R 5.10 The main( ) function can be called with arguments Must declare them to use them: main (int argc, char *argv[ ]) OR main (int argc, char **argv) The value of argc is the number of char strings in the array argv[ ] which is an array of pointers to the command line tokens separated by white space Element argv[0] always points to the command name typed in by the user to invoke the program If there are no other arguments, argc = 1

15 Command-line Arguments If the program was compiled as echo and the user types: echo hello, world argc will be 3 (the number of pointers in argv[]) argv[0]argv[1]argv[2]argv[3]argv[4] ‘e’‘c’‘h’‘\0’ ‘h’‘e’‘l’... ‘w’‘\0’ ?? ‘o’ ‘l’‘o’‘,’ ‘o’‘l’‘r’‘d’

16 Command-line Arguments The program can print back the arguments typed in by the user following the echo command: int main (int argc, char *argv[ ]) { /* envision argc = 3, *argv[0]=“echo”, …*/ while (--argc > 0) printf("%s%s", *++argv, (argc > 1) ? " " : ""); printf("\n"); return 0; }

17 Parsing Arguments Nice example given in Section The program compiled with the run file name “find” looks for a pattern in standard input and prints out lines found. Want options -x and -n for invocation: find [-x] [-n] pattern OR find [-xn] pattern Program will parse option flags: except and number. Loop to parse options and set up flags is:

18 Parsing Arguments Loop through argv entries with first char == '-' (maybe find -x -n pattern) while (--argc > 0 && (*++argv)[0] == '-') while (c = *++argv[0]) switch (c) { case 'x' : except = 1; break; case 'n' : number = 1; break; default : printf("illegal option %c\n", c);break; }

19 Parsing Arguments How (*++argv)[0] and *++argv[0] differ? (*++argv)[0] –Pre-increments argv pointer (points to next string) –De-references pointer (to get pointer to string) –De-references pointer with 0 offset to get first character *++argv[0] –De-references argv pointer with 0 offset (first string) –Pre-increments argv[0] (points to next char in string) –De-references updated pointer to get second character

20 Recursive Main Program int main(int argc, char *argv[ ])/* recursive main program */ { /* calculate factorial of arg[1]*/ char array[100]; char *nargv[2] = {"", array}; unsigned int i; if (argc != 2) return 0; if (!strcmp(argv[1], "1")) return 1; else { itoa(atoi(argv[1]) - 1, nargv[1]); i = atoi(argv[1]) * main(argc, nargv); printf("Returning: %d\n", i); return i; } }/* see Lecture 7 for function itoa – not in library */

21 Stack Frames for main() Recursion UNIX shell calls main with: main (2, {“rmain”, “3”}); main calls main with: main (2, {“”, “2”}); main calls main with: main (2, {“”, “1”}); main returns 3 times 2 Stack Pointer Stack Pointer Stack Pointer [0]=“rmain” [1]=“3” [0]=“” [1]=“2” 2 [0]=“” [1]=“1” argc argv [0]=“rmain” [1]=“3” [0]=“rmain” [1]=“3” [0]=“” [1]=“2”

22 Recursive Main Program blade64(32)% gcc -o rmain rmain.c blade64(33)% rmain 10 Returning: 2 Returning: 6 Returning: 24 Returning: 120 Returning: 720 Returning: 5040 Returning: Returning: Returning: blade64(34)%