Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial 8 Pointers and Strings

Similar presentations


Presentation on theme: "Tutorial 8 Pointers and Strings"— Presentation transcript:

1 Tutorial 8 Pointers and Strings
NUS School of Computing CS1010E Programming Methodology CS1010E Tutorial Slides Prepared by Wu Chao

2 CS1010E Tutorial Slides Prepared by Wu Chao
Quick Summary <string.h> library: strlen(const char* str): returns the number of characters before ‘\0’ strcmp(const char* str1, const char* str2): string comparison returns a positive number if str1 proceeds str2 and vice versa strcat(char* str1, const char* str2): append str2 to the back of str1 Pointers and Array duality Arr[1]  *(Arr + 1) &Arr[1]  Arr + 1 CS1010E Tutorial Slides Prepared by Wu Chao

3 CS1010E Tutorial Slides Prepared by Wu Chao
Q1: Pointers Tracing CS1010E Tutorial Slides Prepared by Wu Chao

4 CS1010E Tutorial Slides Prepared by Wu Chao
Q1: Pointers Tracing int i1, i2; int *p1, *p2; i1 = 5; p1 = &i1; i2 = (*p1)/2 + 10; p2 = p1; i1 5 i2 12 p1 p2 001 001 12 CS1010E Tutorial Slides Prepared by Wu Chao

5 CS1010E Tutorial Slides Prepared by Wu Chao
Q1: Pointers Tracing How to print out the values for tracing in our main()? (void *): typecast in order to prevent warning as we are printing out the address values of the memory location. CS1010E Tutorial Slides Prepared by Wu Chao

6 Q2: Binary Search with Pointers
CS1010E Tutorial Slides Prepared by Wu Chao

7 Q2: Binary Search with Pointers
3 5 7 8 9 12 15 19 21 30 Left Mid Right Left Found! Search for: 19 CS1010E Tutorial Slides Prepared by Wu Chao

8 Q2: Binary Search with Pointers
Can we use: mid = (right + left)/2; Address arithmetic only allows for calculation of differences of address values CS1010E Tutorial Slides Prepared by Wu Chao

9 Q2: Binary Search with Pointers (Optional)
Use of recursion in Binary Search CS1010E Tutorial Slides Prepared by Wu Chao

10 CS1010E Tutorial Slides Prepared by Wu Chao
Q3: Word Palindrome CS1010E Tutorial Slides Prepared by Wu Chao

11 CS1010E Tutorial Slides Prepared by Wu Chao
Q3: Word Palindrome isPalindrome1(…) If condition is always fulfilled till right pointer bypassed left, then it is true If jump out of loop too early, then it is false CS1010E Tutorial Slides Prepared by Wu Chao

12 CS1010E Tutorial Slides Prepared by Wu Chao
Q3: Word Palindrome isPalindrome2(…) To reverse the string, we copy from the back to the front. Initialize right to the end of the str. CS1010E Tutorial Slides Prepared by Wu Chao

13 Q4: Get Drunk (array index simulation)
CS1010E Tutorial Slides Prepared by Wu Chao

14 Q4: Get Drunk (array index simulation)
Missing “&”: we need to pass in the address for scanf function to work, since both “numTrials” and “numSteps” are int pointers, they store the address of the variable to be scanned. CS1010E Tutorial Slides Prepared by Wu Chao

15 Q4: Get Drunk (array index simulation)

16 Q4: Get Drunk (array index simulation)
Additional helper function: Equal probability for the person to walk one step forward as for the person to walk one step backward. How to perform drunken walk? Alternatively:

17 Q4: Get Drunk (array index simulation)

18 Q4: Get Drunk (array index simulation)
Main Function: Pass in the address of the middle element of the array declared.


Download ppt "Tutorial 8 Pointers and Strings"

Similar presentations


Ads by Google