Tuesday, January 16, 2007 How would a car function if it were designed like a computer? Occasionally, executing a maneuver would cause your car to stop.

Slides:



Advertisements
Similar presentations
 Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?
Advertisements

EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Lecture 20 Arrays 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.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
CS 192 Lecture 12 Winter 2003 December 31, January 1, 2004 Dr. Shafay Shamail.
Friday, January 12, 2007 Parkinson's Law “Work expands so as to fill the time available for its completion."
Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
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.
Lecture 08 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 24, 2002.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 6 Pointers C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
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”
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
© Oxford University Press All rights reserved. CHAPTER 6 STRINGS.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
A Computer Science Tapestry 12.1 Pointers, Memory, Abstractions l A pointer is the a variable/value that is a memory address  Addresses like 1, 2, 3,
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
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.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Pointers and Arrays Dynamic Variables and Arrays.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Strings (Continued) Chapter 13
Lecture 8 String 1. Concept of strings String and pointers
Pointer Basics Psst… over there.
Popping Items Off a Stack Lesson xx
String What it is Why it’s useful
Dynamic Memory A whole heap of fun….
C++ Pointers and Strings
Pointer & Memory Allocation Review
C++ Programming Lecture 20 Strings
The Stack.
Pointer Basics Psst… over there.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
C++ Pointers and Strings
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Tuesday, January 16, 2007 How would a car function if it were designed like a computer? Occasionally, executing a maneuver would cause your car to stop and fail and you would have to re-install the engine, and the airbag system would say, “Are you sure?” before going off. - Katie Hafner

§Handout 1

char n[25] = "Pointers Are Funny!!"; char *name, *temp; name = n; temp = name+2; cout << n << endl; cout << name << endl; cout << *name << endl; cout << *temp << endl; cout << n[10] << endl; cout << ++name << endl; cout << ++(*n) << endl; cout << *temp << endl; cout << ++name << endl; cout << *name << endl; cout << n + 15 << endl; cout << name + 15 << endl; cout << ++name << endl; cout << name[2] << endl; Make a memory drawing!

Pointers Are Funny!! P i r ointers Are Funny!! Q i inters Are Funny!! i nny!! y!! nters Are Funny!! e

char n[25] = "Pointers Are Funny!!"; char *name; name = n; cout << n << endl; cout << name << endl; cout << *name << endl; cout << *(name+2) << endl; cout << n[10] << endl; cout << ++name << endl; cout << ++(*n) << endl; cout << *(n+2) << endl; cout << ++name << endl; cout << *name << endl; cout << n + 15 << endl; cout << name + 15 << endl; cout << ++name << endl; cout << name[2] << endl; Make a memory drawing!

For most kinds of linguistic processing, we need to identify and categorize the words of the text. e.g. Frequency parts of speech Tokens are the building blocks of text.

§This program scans the input string, copying characters from the string into another array, called token, until a space is encountered. It prints the token and repeats the process until null at end of string is encountered. §e.g “This is a test: This is a test Tokenizing Example

char str[80], token[80]; int i=0, j=0; cout << "Enter a sentence: "<<flush; gets(str); Tokenizing Example Using Arrays

while(str[i]){ j=0; while(str[i]!= ' ' && str[i]){ token[j]=str[i]; i++; j++; } token[j]='\0'; if(str[i]) i++; cout << token << '\n'; } Tokenizing Example Using Arrays

while(str[i]){ j=0; while(str[i]!= ' ' && str[i]){ token[j]=str[i]; i++; j++; } token[j]='\0'; //null terminate the token if(str[i]) i++; // advance past the space cout << token << '\n'; } Tokenizing Example Using Arrays

int main() { char str[80], token[80]; char *p, *q; cout<<"Enter a sentence: "<<flush; gets(str); p = str; Tokenizing Example Using Pointers

while(*p) { q = token; //set q pointing to start of token /* Read characters until either a space or the null terminator is encountered. */ while(*p!=' ' && *p) { *q = *p; q++; p++; } *q = '\0'; // null terminate the token if(*p) p++; // advance past the space cout << token << '\n'; } return 0; } Tokenizing Example Using Pointers

int main() { char str[80], token[80]; int i, j; cout << "Enter a sentence: "; gets(str); // Read a token at a time from the string. for(i=0; ; i++) { /* Read characters until either a space or the null terminator is encountered. */ for(j=0; str[i]!=' ' && str[i]; j++, i++) token[j] = str[i]; token[j] = '\0'; // null terminate the token cout << token << '\n'; if(!str[i]) break; } return 0; } Self Test: Tokenizing Example Using Arrays

The for loop for (x=0, y=10; x<=10; ++x, --y) cout << x << “ ” << y << “\n”;

int main() { char s[80], *p1; p1 = s; do { cout << "Enter a string: “<<flush; gets(p1); // read a string // print the ASCII values of each character while(*p1) cout << (int) *(p1++) << ' '; cout << '\n'; } while(strcmp(s, "done")); return 0; } //Error? Print ASCII values of each character in a string

§Un-initialized pointers, dangling pointers, bad pointers §A bad pointer is a pointer that contains a random address. Just like un-initialized int variable contains a random int value. §They operate on whatever random memory area they happen to have address of. §Bugs caused by bad pointers are hard to find. §It may accidentally point to wrong data and a pointer operation may alter this data. Problem with this may show up much later in program execution. Null Pointer

// p is now a null pointer int *p =NULL; // succeeds if p is not null if(p) { } // succeeds if p is null if(!p) { } Null Pointer