CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Pointers in C Rohit Khokher
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
Kernighan/Ritchie: Kelley/Pohl:
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Introduction to Systems Programming - Recitation Omer Kotlicki Two instances: 1.Tuesdays 15:00-16:00; Kitot Wednesdays.
CSSE221: Software Dev. Honors Day 27 Announcements Announcements Projects turned in? Projects turned in? The 2 required Angel surveys are due by 9 pm tonight.
1 CSSE 332 Preprocessor, Array and Strings. 2 External library files libname.a or libname.so Special functionality is provided in the form of external.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
CSSE221: Software Dev. Honors Day 29 Announcements Announcements Any questions on strings? Any questions on strings? Section 2: pass in quiz after question.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Introduction to C programming
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
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.
Lecture 10 Introduction to Programming in C Prof. Dr. Arne Kutzner Hanyang University / Seoul Korea.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Memory allocation & parameter passing
Computer Organization and Design Pointers, Arrays and Strings in C
Day 03 Introduction to C.
C Characters and Strings
Day 02 Introduction to C.
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Programming in C Pointer Basics.
Strings #include <stdio.h>
Presentation transcript:

CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday night, 11:59 pm All C Projects due Friday night, 11:59 pm Final Exam next Monday, 6-10 pm, here. Final Exam next Monday, 6-10 pm, here.

Thursday’s daily quiz Show me for completion credit. Show me for completion credit. Check in your code to your personal repos now. (Any checkins after we start going over it will be too late.) Check in your code to your personal repos now. (Any checkins after we start going over it will be too late.) Let’s go over together. Let’s go over together.

#3: Swap int main() { int a = 10; b = 20; swap(&a, &b) } void swap(int* x, int* y) { int temp = *x; *x = *y; *y = temp; } 1020 ab 22ff60 22ff5c 22ff60 22ff5c xy Key concept: this is still call-by-value: the parameters, x and y, didn’t change. Do you see why?

#4: Scanf 4a. What would happen if scanf expected and received regular variables instead of addresses? 4a. What would happen if scanf expected and received regular variables instead of addresses? 4b. What happens with the real scanf if we forget the &? 4b. What happens with the real scanf if we forget the &?

#5: What’s wrong with this code? float *ptr = 0; printf("%4.2f\n", *ptr);

#6: Pointer arithmetic What is another expression for element ar[5] using pointers?

#7: Dynamic memory Whenever you allocate memory using ptr = malloc(...), you need also to: Whenever you allocate memory using ptr = malloc(...), you need also to:

Any questions? on pointers? on pointers? on C Projects #1-3? on C Projects #1-3?

Recap: Declarations Reserve Space Variable declarations reserve space in memory: Variable declarations reserve space in memory: int x; /* reserves enough space for an int, names it x */ int x; /* reserves enough space for an int, names it x */ double d; /* reserves enough space for a double, names it d */ double d; /* reserves enough space for a double, names it d */ Formal parameter declarations do the same: Formal parameter declarations do the same: void average(double sum, int count) {…} void average(double sum, int count) {…} /* reserves enough space for a double (named sum) and an int (named count)*/ /* reserves enough space for a double (named sum) and an int (named count)*/

Recap: Variables with "Pointer Types" Store Addresses Besides holding "things" like ints and doubles, variables in C can also hold memory addresses Besides holding "things" like ints and doubles, variables in C can also hold memory addresses Samples: Samples: int *xPtr; /* reserves enough space for an address, names it xPtr, says that xPtr can store the address of another variable that holds an int */ int *xPtr; /* reserves enough space for an address, names it xPtr, says that xPtr can store the address of another variable that holds an int */ double *dPtr; /* reserves enough space for an address, names it dPtr, says that dPtr can store the address of another variable that holds a double */ double *dPtr; /* reserves enough space for an address, names it dPtr, says that dPtr can store the address of another variable that holds a double */

Recap: Pointer Operators, & The address operator, &: The address operator, &: &var gives the address where var's value is stored &var gives the address where var's value is stored Examples: Examples: xPtr = &x;/* Read "xPtr gets the address of x" */ xPtr = &x;/* Read "xPtr gets the address of x" */ dPtr = &d; /* Read "dPtr gets the address of d" */ dPtr = &d; /* Read "dPtr gets the address of d" */

Recap: Pointer Operators, * Use * two ways: Use * two ways: In type declarations, * says that the name refers to address of something: int *xPtr; double *dPtr; In type declarations, * says that the name refers to address of something: int *xPtr; double *dPtr; In expressions, *var gives the "thing" pointed to by var In expressions, *var gives the "thing" pointed to by var Examples: Examples: printf("%d", *xPtr); printf("%d", *xPtr); *dPtr = ; *dPtr = ; The format string, "%d", says that we want to print an int. *xPtr is the thing pointed to by xPtr. That is, *xPtr is the value of x. This says that the thing pointed to by dPtr should get the value So the result is the same as d =

Pointer Assignments int x=3, y = 5; int *px = &x; int *py = &y; printf("%d %d\n", x, y); *px = 10; printf("%d %d\n", x, y); px = py; *px = 12; printf("%d %d\n", x, y); /* x is changed */ /* y is changed */

This week: More C Monday: Monday: Strings in C (capsule) Strings in C (capsule) ??? ??? Tuesday: Tuesday: Linked list implementation in C Linked list implementation in C Thursday: Thursday: Wrap-up C unit Wrap-up C unit Opportunity for questions about final exam Opportunity for questions about final exam

String – an array of characters String – an array of characters ___________________ /* declare a string called name of 10 characters */ of 10 characters */ /* Initialize “name” to “ALICE” */ /* Print “name” to the screen – Would you print all 10 characters or only the first 5 characters? Hint: use %s */

String – an array of characters String – an array of characters _char name[10];___ /*declare a string called name of 10 characters */ 10 characters */ /* Initialize “name” to “ALICE” */ name[0] = ‘A’; name[1] = ‘l’; name[2] = ‘i’; name[3] = ‘c’; name[4] = ‘e’; name[5] = ‘\0’; /* Print “name” to the screen – Would you print all 10 characters or only the first 5 characters? Hint: use %s */ printf(“%s”, name);

Strings – character array /* Declaring and Initializing strings */ char name[ ] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; char name [ ] = “Alice”; char name [6] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; /* Using scanf read an input string value and save it in “name”*/ scanf(____________, _________); An exception for scanf:

Strings – character array /* Declaring and Initializing strings */ char name[ ] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; char name [ ] = “Alice”; char name [6] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; /* Using scanf read an input string value and save it in “name”*/ scanf(”%s”, &name); An exception for scanf: No ampersand

More arrays - Strings char name[10]; //declaration name = {‘A’,’l’,’i’,’c’,’e’,’\0’}; //initialization /* ’\0’= end of string */ char name [] = “Alice”; //declaration and initialization char name [] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; // ditto scanf(“%s”,name); //Initialization // ERROR: scanf(“%s”,&name); // ERROR: scanf(“%s”,&name); printf(“%s”, name); /* print until ‘\0’ */

What is the output of the following code segment? Use the “man” pages to determine what “strlen” does? char name[ ] = {‘A’,’l’,’e’,’x’,’\0’,’a’,’n’,’d’,’e’,’r’); printf(“ The length of the string is %d\n”, strlen(name)); printf(“ And the string is %s\n”,name); CAUTION: Strings lengths need not match the size of the array. So, always assign sufficient space to hold the longest string for that situation. No out-of-bounds exception!

What is the output of the following code segment? Use the man pages to determine what “strlen” does? char name[ ] = {‘A’,’l’,’e’,’x’,’\0’,’a’,’n’,’d’,’e’,’r’); printf(“ The length of the string is %d\n”, strlen(name)); printf(“ And the string is %s\n”,name); The length of the string is 4. And the string is Alex. CAUTION: Strings lengths need not match the size of the array. So, always assign sufficient space to hold the longest string for that situation. No out-of-bounds exception!

Other string functions Functions to operate on strings ________________________ compare two strings ________________________ break a string into tokens ________________________ finds the first occurrence of a character in a string string ________________________ make a copy of a string What are the libraries that handle strings and characters? Do you need a special flag when compiling a program that uses a function from either string library?

Other string functions Functions to operate on strings _strcmp, strncmp________ compare two strings _strtok__________________ break a string into tokens _strchr _________________ finds the first occurrence of a character in a string string _strcpy_________________ make a copy of a string What are the two string libraries? string.h strings.h stdlib.h ctype.h Do you need a special flag when compiling a program that uses a function from either string library? We can get this information from the “man” pages. And, the answer is that a special flag is not needed.

Strings contd. Functions to operate on strings Functions to operate on strings strcpy, strncpy, strcmp, strncmp, strcat, strncat, substr, strlen,strtok strcpy, strncpy, strcmp, strncmp, strcat, strncat, substr, strlen,strtok #include or at program start #include or at program start CAUTION: C allows strings of any length to be stored. Characters beyond the end of the array will overwrite data in memory following the array. CAUTION: C allows strings of any length to be stored. Characters beyond the end of the array will overwrite data in memory following the array.

Break

Array of Pointers Variable length strings Variable length strings char *card[4]; // card[4] => array of 4 elements //char* => element is a pointer to a character. //char* => element is a pointer to a character. // card[4] => array of 4 char pointers // card[4] => array of 4 char pointers card[0] card[1] card[2] card[3]

card[0] = (char*)malloc(6*sizeof(char)); card[1] = (char*)malloc(3*sizeof(char)); and so on. Static allocation of a 2D array: Static allocation of a 2D array: char card[4][10]; //waste of space

For the following code segment, complete the diagrams provided to graphically represent the pointer creations and assignments. Again, line numbers are provided. You may assume that the malloc on line 2 allocates space starting at address 5000 and the malloc on line 3 allocates space starting at address char *card[4]; card[0] = (char*)malloc(8*sizeof(char)); line 2 card[3] = (char*)malloc(9*sizeof(char)); line 3 strcpy(card[0],”hearts”); line 4 strcpy(card[3],”diamonds”); line card[0] card[1] card[2] card[3]