Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reading Pointers: Chapter 4, pages 144 - 172. Strings as Arrays of Characters string Greeting = "hello"; vs. char *Greeting = "hello";

Similar presentations


Presentation on theme: "Reading Pointers: Chapter 4, pages 144 - 172. Strings as Arrays of Characters string Greeting = "hello"; vs. char *Greeting = "hello";"— Presentation transcript:

1 Reading Pointers: Chapter 4, pages 144 - 172

2 Strings as Arrays of Characters string Greeting = "hello"; vs. char *Greeting = "hello";

3 pointers imagine passing the following string to a function: void printStr( string x ); x = "Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure. We are met on a great battle- field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate— we can not consecrate—we can not hallow—this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us — that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion — that we here highly resolve that these dead shall not have died in vain — that this nation, under God, shall have a new birth of freedom — and that government of the people, by the people, for the people, shall not perish from the earth." printStr( x );

4 in computer memory fourscoreandsevenyearsagoour ……… shallnotperishfromtheearth.0

5 computer memory is ADDRESSED 100four 104score 109and 112seven 117years addressdata

6 a shortcut we could pass our huge string "x" to any function simply by passing the address of the first character. the address of the first character is referenced like this: " *x ". If x is defined as *x the x is a pointer to a string of characters.

7 value vs. reference a string defined as: string x = "hello"; is passed around and used as a complete value. a string defined as: char *x = "hello"; is passed around and used as a reference, or pointer, to a string. much quicker

8 note:&x &x refers to the actual address in memory of x… in our example, &x = 100; if a function expects a pointer, and all you have is a variable, sometimes you can send the address and it won't complain why? because a pointer IS an address.

9 we will use references where appropriate strcmp, strcpy, strcat library functions only take pointers as arguments char *x = "hawk"; char *y = "eagle"; strcmp( x, y ) yields a 3…. why/ - if hawk comes before eagle (it doesn't) 0 if they're identical strings (they're not) + if hawk comes after eagle (it does)

10 let's alphabetize a list … this may take a while… const int arraySize = 8; char *birds[ arraySize ] = { "eagles", "hawks", "bluejays", "robins", "cardinals", "owls", "doves", "ducks" };

11 our menu of choices

12 we'll need to print the list the call from main:

13 Why is printAnyArray passed an array, rather than just using the global birds[ ] array? - it is more useful then, because it can print any array, not just birds[ ] birds is defined as a pointer a pointer to an array is expected

14 we'll need to swap around array elements

15 finally, the bubble-sort, in main

16 strings vs. char pointers when you define a string: string userInput; you cannot deal with individual characters…. but when you define a pointer to the first character of a string: char *userInput; it becomes a character array, and you can deal with it in BOTH ways

17 strlen char *p = "This is a whole sentence."; int length = strlen( p ); cout << p << " is " << length << " characters long "; for (x=0; x<result4; x++) { // find the spaces if ( p[x] == ' ') { cout << x << " "; // will list the spaces } }

18 tell if a string contains only digits char *q = "8243715487621564"; bool containsLetters = false; for (x=0; x<rstrlen(q); x++) { if ( (q[x] != '0') && (q[x] != '1') && (q[x] != '2') && (q[x] != '3') && (q[x] != '4') && (q[x] != '5') && (q[x] != '6') && (q[x] != '7') && (q[x] != '8') && (q[x] != '9') ) { containsLetters = true; } } // end for

19 convert strings to char* // our first member function: string userInput; cout << "Enter a string: "; cin >> userInput; const char *q = userInput.c_str( ); // you can now use q in strlen and strcmp

20 member function? #include string userInput; const char *p = userInput.c_str( ); the authors of the string library provided a way to get the pointer value of any string, to be used when converting a string to a character pointer, for use in strcmp and strlen


Download ppt "Reading Pointers: Chapter 4, pages 144 - 172. Strings as Arrays of Characters string Greeting = "hello"; vs. char *Greeting = "hello";"

Similar presentations


Ads by Google