Download presentation
Presentation is loading. Please wait.
1
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
2
§Handout 1
3
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!
4
Pointers Are Funny!! P i r ointers Are Funny!! Q i inters Are Funny!! i nny!! y!! nters Are Funny!! e
5
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!
6
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.
7
§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
8
char str[80], token[80]; int i=0, j=0; cout << "Enter a sentence: "<<flush; gets(str); Tokenizing Example Using Arrays
9
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
10
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
11
int main() { char str[80], token[80]; char *p, *q; cout<<"Enter a sentence: "<<flush; gets(str); p = str; Tokenizing Example Using Pointers
12
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
13
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
14
The for loop for (x=0, y=10; x<=10; ++x, --y) cout << x << “ ” << y << “\n”;
15
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
16
§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
17
// 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.