1 / 14

Pointers and Strings

Pointers and Strings. Strings. A string can be declared as: char pet[] = “cat”; char *pPet = “dog”; In the second declaration above, pPet points to the first letter ( d ) of the string on the right

hidi
Télécharger la présentation

Pointers and Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pointers and Strings CS250 Introduction to Computer Science II

  2. Strings • A string can be declared as: • char pet[] = “cat”; • char *pPet = “dog”; • In the second declaration above, pPet points to the first letter (d) of the string on the right • Depending on the compiler, the space in memory that contains dog may or may not be modifiable • If a string literal is to be modified, it should always be stored in a character array CS250 Introduction to Computer Science II

  3. Strings • Assuming that the string pet has been declared as: • char pet[] = “cat”; • Write a function that will output the contents of the string. The function should accept the array and its size • Write a function that will output the contents of the string. The function should accept a pointer to char CS250 Introduction to Computer Science II

  4. Strings and Pointers • Write a function strLength that accepts a string (as a pointer) and returns the length of the string CS250 Introduction to Computer Science II

  5. Strings and Pointers int strLength (const char *pStr) { int index; for (index = 0; *(pStr + index) != '\0'; index ++); return index; } • What is the purpose of const in the function header? • Why is the null character \0 in single quotes? • Is the ; at the end of the for loop a mistake? • What would happen if the ; was eliminated? CS250 Introduction to Computer Science II

  6. Pointers and Strings int strLength1 (const char *pStr) { int index; for (index = 0; *(pStr++) != '\0'; index ++); return index; } • Will the above function give the same output as the function on the previous slide? CS250 Introduction to Computer Science II

  7. Pointer Arithmetic int strLength2 (char *pStr) { char *pTemp = pStr; while (*pTemp) pTemp ++; return pTemp - pStr; } CS250 Introduction to Computer Science II

  8. What is happening? int sumInts (int *pArray, int size) { int sum = 0; int index; for (index = 0; index < size; index ++) sum += * pArray ++; return sum; } • int array[] = {10, 20, 30, 40, 50}; creates an array as follows: Address Value Element 2000 10 0 2004 20 1 2008 30 2 2012 40 3 2016 50 4 CS250 Introduction to Computer Science II

  9. Constant Pointers • So far we have seen: • Nonconstant pointers to nonconstant data • Nonconstant pointers to constant data • What about constant pointers? • We said that array names are constant pointers to the first element in the array. What does that mean? CS250 Introduction to Computer Science II

  10. Constant Pointers int * const pNum, num, num2; num = 9; num2 = num + 8; pNum = &num; *pNum *= 2; pNum = &num2; // ERROR • pNum has been declared as a constant pointer • It cannot point to any other memory location CS250 Introduction to Computer Science II

  11. Bubble Sort 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3 7 4 1 4 7 4 4 1 7 7 4 7 1 4 4 4 4 1 4 4 1 1 1 4 4 7 1 4 1 7 4 4 7 1 1 7 7 1 7 7 7 7 1 1 1 7 7 1 First pass Second pass Continue while cells are being swapped……. CS250 Introduction to Computer Science II

  12. Bubble Sort • Let’s look through the code that will perform the bubble sort described on the previous slide • http://zeus.cs.pacificu.edu/shereen/cs250/lectures/04bubble.html CS250 Introduction to Computer Science II

  13. Arrays of Pointers • What do you make of the following declaration? char *cardSuits[4] = {"Clubs", "Diamonds", "Hearts", "Spades"}; • What gets output in each of the following cases? cout << cardSuits[1] << endl; cout << *cardSuits[1] << endl; CS250 Introduction to Computer Science II

  14. Summary • Today I introduced • Pointers and strings • Constant pointes • Bubble sort • Arrays of pointers • We have covered: • All of chapter 5 CS250 Introduction to Computer Science II

More Related