Tutorial 8 Pointers and Strings

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Week 8 Arrays Part 2 String & Pointer
CISC105 – General Computer Science Class 11 – 07/10/2006.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.
Computer Science 210 Computer Organization Strings in C.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
Tutorial 5 Arrays Basics (1D, 2D) NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.
C++ Lecture 3 Monday, 14 July Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
1 CSE 2341 Object Oriented Programming with C++ Note Set #2.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Pointers *, &, array similarities, functions, sizeof.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Tutorial 2 Control Flow: Loops NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
ECE Application Programming
Chapter 7 User-Defined Methods.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
ECE Application Programming
Lecture 8 String 1. Concept of strings String and pointers
C Programming Tutorial – Part I
Programming Languages and Paradigms
CS1010 Discussion Group 11 Week 11 – Recursion recursion recursion….
Lecture-5 Arrays.
CS1010 Discussion Group 11 Week 6 – One dimensional arrays.
Programming Paradigms
Computer Science 210 Computer Organization
Arrays in C.
Programming Languages and Paradigms
Object-Oriented Programming Using C++
Arrays … The Sequel Applications and Extensions
C Stuff CS 2308.
CS1100 Computational Engineering
Popping Items Off a Stack Lesson xx
Chapter 8 Arrays Objectives
Chapter 9 One-Dimensional Arrays
Recursion.
Beginning C for Engineers
Topics discussed in this section:
EECE.2160 ECE Application Programming
Chapter 17 Recursion.
C++ Pointers and Strings
Chapter 8 Arrays Objectives
EECE.2160 ECE Application Programming
CS250 Introduction to Computer Science II
Arrays.
Lecture 19: Working with strings
Tutorial 6 Array Problem Solving
Characters and Strings
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
C++ Pointers and Strings
EECE.2160 ECE Application Programming
Pointers.
Introduction to Pointers
Introduction to Problem Solving and Programming
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Q4: Get Drunk (array index simulation)

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:

Q4: Get Drunk (array index simulation)

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