1 Review of Chapter 10: String and Pointers. 2 Outline  String:  Representation of a string: \0  Using scanf to read in string  Initilization of strings.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
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 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
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.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Chapter 10.
1 Review of Class on Nov 23:. 2 Chapter 12: Structures and ADTs  Outline  Declaring Structures  Accessing a Member in a structure variable  Initialization.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
Chapter 8 Arrays and Strings
1 Review of Chapter 6: The Fundamental Data Types.
Computer Science 210 Computer Organization Strings in C.
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Characters and Strings File Processing Exercise C Programming:Part 3.
File IO and command line input CSE 2451 Rong Shi.
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.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
1 This chapter covers both string constants (or literals, as they're called in the C standard) and string variables, which can change during the execution.
Arrays as Function Arguments Array can be used as a function argument. E.g., #include int sum(int b[], int n) { int i, res; res = 0; for(i = 0; i < n;
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
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.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
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. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Pointers and Tools
Chapter 1 Basic C Programming
Today’s Material Strings Definition Representation Initialization
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.
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.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
Command Line Arguments
CSE 303 Concepts and Tools for Software Development
Command-Line Arguments
Module 2 Arrays and strings – example programs.
Strings A string is a sequence of characters treated as a group
Computer Science 210 Computer Organization
CS111 Computer Programming
C Stuff CS 2308.
Pointers and Pointer-Based Strings
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Programming Languages and Paradigms
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Presentation transcript:

1 Review of Chapter 10: String and Pointers

2 Outline  String:  Representation of a string: \0  Using scanf to read in string  Initilization of strings  String-Handling Functions in the Standard Library  Passing Arguments to main() using an array of strings

3 String  A string is  a one-dimensional array of type char. char w[100];  character value \0 is used to terminate a string strings have a variable length delimited by the null character \0 but with a maximum length determined by the size of the character array The size of the string must include the storage needed for the null character \0.

4 The End-of-String Sentinel \0  Example: #include int main(void){ char w[100]; w[0]='A'; w[1]='B'; w[2]='C'; w[3]='\0'; printf("%s\n", w); } % a.out ABC #include int main(void){ char w[100]; w[0]='A'; w[1]='B'; w[2]='C'; w[3]='\0'; w[4]=‘D'; printf("%s\n", w); } % a.out ABC the null character value \0 is used to terminate a string

5 Using scanf to reading string  Using scanf to read in a string  scanf(“%s”, w); read in non-white space characters opositions the input stream to an initial non-white space character oread in non-white space characters oThe process stops when a white space character or EOF is encountered. a null character is placed in memory to end the string.

6 Using scanf to reading string #include int main(void){ char w[10]; printf("Enter strings\n", w); scanf("%s", w); printf("%s\n", w); } % a.out Enter strings Hello % a.out Enter strings Hello World Hello scanf(”%s”,w);  read in non-white space characters positions the input stream to an initial non-white space character read in non-white space characters The process stops when a white space character or EOF is encountered.  a null character is placed in memory to end the string.

7 Initialization of Strings  Initialization of Strings  Example: initialize a string variable as “abc” char s[] = {‘a’, ‘b’, ‘c’, ‘\0’}; char s[]=“abc”; #include int main(void){ char w[]="abc"; printf("%d\n", sizeof(w)); } % a.out 4 The size of the string must include the storage needed for the null character \0.

8 Initialization of Strings  A pointer to char can also be initialized with a constant string.  A string constant is stored in memory by the compiler.  the pointer is assigned the address of the constant string in memory.  Example: char p* = “abc”; #include int main(void){ char *p="abc"; printf("%s\n",p); } % a.out abc

9 Initialization of Strings  Difference between  initializing an array with a constant string the array contains the individual characters followed by the null character  initializing a pointer with a constant string A string constant is stored in memory by the compiler. the pointer is assigned the address of the constant string in memory.

10 String-Handling Functions in the Standard Library  String-handling functions:  Function prototypes are provided by string.h #include  Functions: Concatenate two strings: strcat (s1, s2); Compare two strings: int strcmp (s1, s2); Copy s2 to s1: strcpy (s1, s2); Length of a string: strlen (s);

11 Outline  String:  Representation of a string: \0  Using scanf to read in string  Initilization of strings  String-Handling Functions in the Standard Library  Passing Arguments to main() using an array of strings

12 Passing Arguments to main()  Unix Commands, take arguments  %pico q1.c  %gcc q12.c prime.c  %gcc –lm q12.c prime.c  In our project, write code, compile, execute a.out  %a.out  Can we pass arguments to our program?  Can we pass arguments to the main() function?

13 Passing Arguments to main()  How main() communicates with the operating system?  int main(void)  int main( int argc, char *argv[]) argc: the number of the command line arguments argv: an array of strings

14 Passing Arguments to main() #include int main(int argc, char *argv[]){ int i; printf("%d \n", argc); for (i=0; i < argc; ++ i) printf("%s\n", argv[i]); } %a.out Hello World 3 a.out Hello World argc: the number of the command line arguments argv: an array of strings

15 Summary  String:  Representing a string using an array of characters  \0 is used to terminated a string  strings have a variable length delimited by the null character \0 but with a maximum length determined by the size of the character array  initialization of strings  String-Handling Functions in the Standard Library  Passing Arguments to main()  argc: number of arguments  argv: an array of strings

16 End of Chapter 10: String and Pointers Read 10.1 – 10.10

17 Chapter 12 Structures and ADTs

18 Introduction  Programming Questions:  How to represent a date? Three components are required: oday, month, year  Three variables can be used to represent a date. oint day, month, year; /* date */

19 Introduction  Programming Questions:  How to represent a student record? components: olast_name, first_name; oUIN oscores of six assignments; oscores of three midterms and final; Six variables are required to represent a student record. ochar[20] last_name; ochar[20] first_name; oint UIN; oint assignment[6], midterm[3], final;

20 Introduction represent a student record.  char[20] last_name;  char[20] first_name;  int UIN;  int assignment[6], midterm[3], final; represent a date.  int day, month, year; Can we represent a collection of components of possibly different types by a single variable? A derived date type — structure  Structure is a means of aggregating a collection of data items of possibly different types.  components are individually named. These components are called members.

21 Chapter 12: Structures and ADTs  Outline  Declaring Structures  Accessing a Member in a structure variable  Initialization of Structures

22 Declaring Structures  How to declare a structure data type?  Example: a structure type to represent a date: Components: day, month, year struct date_str{ int day; int month; int year; };  This declaration creates the derived date type struct date_str. members of the structure structure tag name

23 Declaring Structures  How to declare variables of a structure type?  Declare variables in declaration of a structure type struct date_str{ int day; int month; int year; } date1, date2;  Declare variables “struct str_name variable_list;” struct date_str{ int day; int month; int year; }; struct date_str date3, date4;

24 #include int main(void){ struct date_str{ int day; int month; int year; } date1, date2; printf("Input data in format DD/MM/YYYY:"); scanf("%d/%d/%d", &date1.day, &date1.month, &date1.year); printf("Input data in format DD/MM/YYYY:"); scanf("%d/%d/%d", &date2.day, &date2.month, &date2.year); } struct date_str{ int day; int month; int year; }; struct date_str date1; struct date_str date2;

25 #include int main(void){ struct student_str{ char last_name[15]; char first_name[15]; int UIN; int assign[6]; int midterm[3]; int final; } students[110]; int i; printf("Input last name, first name and UIN for each students:\n"); for (i=0; i< 110; i++){ scanf("%s", students[i].last_name); scanf("%s", students[i].first_name); scanf("%d", &students[i].UIN); }

26 Declaring Structures  Summary:  Declare a structure type  Declare variables of a structure type  The declaration of a structure type creates a derived date type. No storage is allocated upon this declaration Storage is allocated when variables are declared of a structure type.

27 Chapter 12: Structures and ADTs  Outline  Declaring Structures  Accessing a Member in a structure variable  Initialization of Structures

28 Access a member  How to access a member?  member operator “.” structure_variable.member_name  Example: struct date_str{ int day; int month; int year; } date1, date2; date1.year = 2000; data2.year= 2005; date1.day = date2.day = 10; date1.month = date2.month = 11;

29 #include int main(void){ struct date_str{ int day; int month; int year; } date1, date2; printf("Input data in format DD/MM/YYYY:"); scanf("%d/%d/%d", &date1.day, &date1.month, &date1.year); printf("Input data in format DD/MM/YYYY:"); scanf("%d/%d/%d", &date2.day, &date2.month, &date2.year); }

30 #include int main(void){ struct student_str{ char last_name[15]; char first_name[15]; int UIN; int assign[6]; int midterm[3]; int final; } students[110]; int i; printf("Input last name, first name and UIN for each students:\n"); for (i=0; i< 110; i++){ scanf("%s", students[i].last_name); scanf("%s", students[i].first_name); scanf("%d", &students[i].UIN); }

31 Accessing a Member  Question:  Given the following declaration: struct date_str{ int day; int month; int year; } date1; struct date_str *pDate = &date1; How to access the members of the variable to which pDate points? (*pDate).day

32 Accessing a Member  How to access a member?  structure pointer operator -> access the members of a structure via a pointer. pointer_to_structure -> member_name  (*pointer_to_structure).member_name  Example: struct date_str *pDate = &date1; (*pDate).day  pDate->day

33 #include struct date_str{ int day; int month; int year; }; void getDate(struct date_str *pDate); void printDate(struct date_str *pDate); #include "date.h" int main(void){ struct date_str date1, date2; getDate(&date1); getDate(&date2); printDate(&date1); printDate(&date2); } date.h #include "date.h“ void getDate(struct date_str *pDate){ printf("Input a date in DD/MM/YYYY:"); scanf("%d/%d/%d", &(pDate->day), &(pDate->month), &(pDate->year)); } void printDate(struct date_str *pDate){ printf("The input date is: %d/%d/%d\n", pDate->day, pDate->month, pDate->year); } date.c gcc date.c main.c main.c

34  Notes  The member name must be unique within the specified structure struct name_str{ char[15] last_name; char[15] first_name; }; struct name_str{ char[15] name; }; X

35  Notes  Can we have two members having the same name in different structures? Since the member must always be accessed through a unique structure variable identifier,  there is no confusion

36 Accessing a Member  Summary  member operator “.” structure_variable.member_name  structure pointer operator “ -> ” access the members of a structure via a pointer. pointer_to_structure -> member_name  (*pointer_to_structure).member_name

37 Chapter 12: Structures and ADTs  Outline  Declaring Structures  Accessing a Member in a structure variable  Initialization of Structures

38 Initialization of Structures  Initialization  A structure variable can be followed by an equal sign = and a list of constants contained within braces  Example: struct date_str{ int day; int month; int year; }; struct date_str date={12, 12, 2000};

39 Initialization of Structures  Initialization  If there are not enough values, the remaining members are assigned the value zero.  Example: struct student_str{ char last_name[15]; char first_name[15]; int UIN; int assign[6]; int midterm[3]; int final; } strcut student_str s1={“Bush”, “Jenny”, };

40 Chapter 12: Structures and ADTs  Summary  Declaring Structures  Accessing a Member in a structure variable member operator “.”: ostructure_variable.member_name structure pointer operator “ -> ” : opointer_to_structure -> member_name  Initialization of Structures A structure variable can be followed by oan equal sign = and oa list of constants contained within braces oIf there are not enough values, the remaining members are assigned the value zero. Read Chapter 12.1 – 12. 6