Pointers and strings
This presentation is the property of its rightful owner.
Sponsored Links
1 / 14

Pointers and Strings PowerPoint PPT Presentation


  • 47 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Pointers and strings

Pointers and Strings

CS250 Introduction to Computer Science II


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

  • 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


Strings1

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


Strings and pointers

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


Strings and pointers1

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


Pointers and strings1

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


Pointer arithmetic

Pointer Arithmetic

int strLength2 (char *pStr)

{

char *pTemp = pStr;

while (*pTemp)

pTemp ++;

return pTemp - pStr;

}

CS250 Introduction to Computer Science II


What is happening

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


Constant pointers

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


Constant pointers1

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


Bubble sort

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


Bubble sort1

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


Arrays of pointers

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


Summary

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


  • Login