19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Lecture 20 Arrays and Strings
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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
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.
Kernighan/Ritchie: Kelley/Pohl:
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
C programming---String. String Literals A string literal is a sequence of characters enclosed within double quotes: “hello world”
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Chapter 10.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
C strings (Reek, Ch. 9) 1CS 3090: Safety Critical Programming in C.
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]
Computer Science 210 Computer Organization Strings in C.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
More Pointers and Arrays Kernighan/Ritchie: Kelley/Pohl: Chapter 5 Chapter 6.
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.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
Arrays and Pointers.
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).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
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 may.
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.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Pointers and Dynamic Arrays
Computer Organization and Design Pointers, Arrays and Strings in C
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Pointers and Pointer-Based Strings
Lecture 18 Arrays and Pointer Arithmetic
C++ Pointers and Strings
Arrays, Pointers, and Strings
C++ Pointers and Strings
Presentation transcript:

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur2 Pointer arithmetic and element size double a[2], * p, *q ; p=a;/*points to base of array*/ q=p+1;/*  q=&a[1] */ printf(“%d\n”, q-p);/* 1 printed */ printf(“%d\n”, (int)q - (int)p); /* 8 printed */

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur3 Arrays as function arguments  In a function definition, a formal parameter that is declared as an array is actually a pointer.  When an array is passed as an argument to a function, the base address of the array is passed “call by value”. The array elements are not copied. double sum (double a[], int n) { int i; double sum; for (i=0,sum=0.0; i<n; i++) sum += a[i]; return sum; } double sum (double *a, int n) { int i; double sum; for (i=0,sum=0.0; i<n; i++) sum += a[i]; return sum; }

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur4 Recursively finding the sum double sum (int a[], int n) { if ( ) return ; return + ; } n == 0 0 a[0] sum (a+1, n-1)

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur5 Various ways that sum() might be called Invocation What gets computed and returned

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur6 Recursive programs using arrays int sum (int a[], int size) { if (size==0) return 0; return a[0]+sum(a+1,size-1); } void reverse (int a[], int size) { if (size==0 || size==1) return; swap2 (&a[0], &a[size-1]); reverse (a+1,size-2); } could have written swap2(a,a+size-1);

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur7 Recursive programs using arrays int * binsearch (int elem, int a[], int size) { if (size==0) return NULL; mid = size/2; if (elem == a[mid]) return &a[mid]; if (elem > a[mid]) { return binsearch (elem, a+mid+1, size-mid-1); } else return (elem, a, mid); }

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur8 Recursive programs using arrays void remove (char c, char s[]) { char * p; if (s[0]==‘\0’) return; if (s[0] == c) { shiftleft (s); remove (c, s); } else remove (c, s+1); } void shiftleft (char s[]) { int i; for (i=0; s[i] !=‘\0’;i++) { s[i] = s[i+1]; }

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur9 Strings  1-d arrays of type char  By convention, a string in C is terminated by the end-of-string sentinel \0 (null character)  char s[21] - can have variable length delimited with \0.  Max length is 20 as the size must include storage needed for the delimiter.  String constants : “hello”, “abc”  “abc” is a character array of size 4.

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur10 Strings zA string constant is treated as a pointer. Its value is the base address of the string. char *p = “abc”; printf (“%s %s\n”,p,p+1); /* abc bc is printed */ abc\0 p

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur11 Differences : array & pointers char *p = “abcde”; The compiler allocates space for p, puts the string constant “abcde” in memory somewhere elese, initializes p with the base address of the string constant. char s[] = “abcde”;  char s[] = {‘a’,’b’,’c’,’d’,’e’.’\0’}; The compiler allocates 6 bytes of memory for the array s which are initialized with the 6 characters. abcde\0abcde s p

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur12 A program using strings /* Count the number of words in a string */ #include int word_cnt (char s[]) { int cnt = 0, i; for (i=0; s[i] != ‘\0’) { while (isspace(s[i])) /* skip white space */ ++i; if (s[i] != ‘\0’) { /* found a word */ ++cnt; while (!isspace(s[i]) && s[i]!=‘\0’) /* skip the word */ ++i; } return cnt; }

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur13 The program with pointers /* Count the number of words in a string */ #include int word_cnt (char *s) { int cnt = 0; while (*s != ‘\0’) { while (isspace(*s)) /* skip white space */ ++s; if (*s != ‘\0’) { /* found a word */ ++cnt; while (!isspace(*s) && *s!=‘\0’) /* skip the word */ ++s; } return cnt; }

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur14 String-handling functions in the standard library z#define string.h zchar *strcat (char *s1, const char *s2); zTakes 2 strings as arguments, concatenates them, and puts the result in s1. Returns s1. Programmer must ensure that s1 points to enough space to hold the result. char *strcat(char *s1, const char *s2) { char *p = s1; while (*p) /* go to the end */ ++p; while (*p++ = *s2++) /* copy */ ; return s1; }

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur15 Dissection of the strcat() function char *p = s1; p is being initialized, not *p. The pointer p is initialized to the pointer value s1. Thus p and s1 point to the same memory location. while (*p) ++p; /*  while (*p != ‘\0’) ++p; */ As long as the value pointed to by p is non-zero, p is incremented, causing it to point at the next character in the string. When p points to \0, the expression *p has the value 0, and control exits the while statement. while (*p++ = *s2++) ; At the beginning, p points to the null character at the end of string s1. The characters in s2 get copied one after another.

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur16 zint strcmp (const char *s1, const char *s2); Two strings are passed as arguments. An integer is returned that is less than, equal to, or greater than 0, depending on whether s1 is lexicographically less than, equal to, or greater than s2. int strcmp(char *s1, char *s2) { for (;*s1!=‘\0’&&*s2!=‘\0’; s1++,s2++) { if (*s1>*s2) return 1; if (*s2>*s1) return -1; } if (*s1 != ‘\0’) return 1; if (*s2 != ‘\0’) return -1; return 0; }

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur17 char *strcpy (char *s1, const char *s2); The characters is the string s2 are copied into s1 until \0 is moved. Whatever exists in s1 is overwritten. It is assumed that s1 has enough space to hold the result. The pointer s1 is returned. size_t strlen (const char *s); A count of the number of characters before \0 is returned. size_t strlen (const char *s) { size_t n; for (n=0; *s!=‘\0’; ++s) ++n; return n; } char * strcpy (char *s1, char *s2) { char *p = s1; while (*p++ = *s2++) ; return s1; }

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur18 Examples of string handling functions char s1[] = “beautiful big sky country”, s2[] = “how now brown cow”; Expression Value strlen (s1) strlen (s2+8) strcmp(s1,s2) Statements What gets printed printf(“%s”,s1+10); strcpy(s1+10,s2+8); strcat(s1, “s!”); printf(“%s”, s1);

19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur19 Examples of string handling functions char s1[] = “beautiful big sky country”, s2[] = “how now brown cow”; Expression Value strlen (s1) 25 strlen (s2+8) 9 strcmp(s1,s2) negative integer Statements What gets printed printf(“%s”,s1+10); big sky country strcpy(s1+10,s2+8); strcat(s1, “s!”); printf(“%s”, s1); beautiful brown cows!