Review of Chapter 10: String and Pointers
description
Transcript of Review of Chapter 10: String and Pointers
![Page 1: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/1.jpg)
1
Review of Chapter 10:String and Pointers
![Page 2: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/2.jpg)
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
![Page 3: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/3.jpg)
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.
![Page 4: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/4.jpg)
4
The End-of-String Sentinel \0
Example:
#include <stdio.h>int main(void){ char w[100]; w[0]='A'; w[1]='B'; w[2]='C'; w[3]='\0'; printf("%s\n", w);}
% a.outABC
#include <stdio.h>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
![Page 5: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/5.jpg)
5
Using scanf to reading string
Using scanf to read in a string scanf(“%s”, w);
read in non-white space characters o positions the input stream to an initial non-
white space charactero read in non-white space characterso The process stops when a white space
character or EOF is encountered.
a null character is placed in memory to end the string.
![Page 6: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/6.jpg)
6
Using scanf to reading string
#include <stdio.h>int main(void){ char w[10]; printf("Enter strings\n", w); scanf("%s", w); printf("%s\n", w);}% a.outEnter stringsHelloHello
% a.outEnter stringsHello WorldHello
scanf(”%s”,w);
read in non-white space characters
positions the input stream to an initial non-white space characterread in non-white space charactersThe process stops when a white space character or EOF is encountered.
a null character is placed in memory to end the string.
![Page 7: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/7.jpg)
7
Initialization of Strings
Initialization of Strings Example: initialize a string variable as “abc”
char s[] = {‘a’, ‘b’, ‘c’, ‘\0’};char s[]=“abc”;
#include <stdio.h>int main(void){ char w[]="abc"; printf("%d\n", sizeof(w));}
% a.out4
The size of the string must include the storage needed for the null character \0.
![Page 8: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/8.jpg)
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 <stdio.h>int main(void){ char *p="abc"; printf("%s\n",p);}
% a.outabc
![Page 9: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/9.jpg)
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 stringA string constant is stored in memory by
the compiler.the pointer is assigned the address of the
constant string in memory.
![Page 10: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/10.jpg)
10
String-Handling Functions in the Standard Library
String-handling functions: Function prototypes are provided by string.h
#include <string.h> 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);
![Page 11: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/11.jpg)
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
![Page 12: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/12.jpg)
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?
![Page 13: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/13.jpg)
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
![Page 14: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/14.jpg)
14
Passing Arguments to main()
#include <stdio.h>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 World3a.outHelloWorld
argc: the number of the command line argumentsargv: an array of strings
![Page 15: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/15.jpg)
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
![Page 16: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/16.jpg)
16
End of Chapter 10: String and PointersRead 10.1 – 10.10
![Page 17: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/17.jpg)
17
Chapter 12 Structures and ADTs
![Page 18: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/18.jpg)
18
Introduction
Programming Questions: How to represent a date?
Three components are required:o day, month, year
Three variables can be used to represent a date.
o int day, month, year; /* date */
![Page 19: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/19.jpg)
19
Introduction
Programming Questions: How to represent a student record?
components: o last_name, first_name;o UINo scores of six assignments;o scores of three midterms and final;
Six variables are required to represent a student record.
o char[20] last_name;o char[20] first_name;o int UIN;o int assignment[6], midterm[3], final;
![Page 20: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/20.jpg)
20
Introductionrepresent 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 — structureStructure is a means of aggregating
a collection of data items of possibly different types.
components are individually named.These components are called
members.
![Page 21: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/21.jpg)
21
Chapter 12: Structures and ADTs
Outline Declaring Structures Accessing a Member in a structure variable Initialization of Structures
![Page 22: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/22.jpg)
22
Declaring Structures
How to declare a structure data type? Example: a structure type to represent a
date: Components: day, month, yearstruct 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
![Page 23: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/23.jpg)
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;
![Page 24: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/24.jpg)
24
#include <stdio.h>
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;
![Page 25: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/25.jpg)
25
#include <stdio.h>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); }}
![Page 26: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/26.jpg)
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
declarationStorage is allocated when variables are
declared of a structure type.
![Page 27: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/27.jpg)
27
Chapter 12: Structures and ADTs
Outline Declaring Structures Accessing a Member in a structure variable Initialization of Structures
![Page 28: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/28.jpg)
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;
![Page 29: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/29.jpg)
29
#include <stdio.h>
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);}
![Page 30: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/30.jpg)
30
#include <stdio.h>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); }}
![Page 31: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/31.jpg)
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
![Page 32: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/32.jpg)
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
![Page 33: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/33.jpg)
33
#include <stdio.h>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
![Page 34: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/34.jpg)
34
Notes The member name must be unique within
the specified structurestruct name_str{
char[15] last_name;char[15] first_name;
};struct name_str{
char[15] name;char[15] name;
}; X
![Page 35: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/35.jpg)
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
![Page 36: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/36.jpg)
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
![Page 37: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/37.jpg)
37
Chapter 12: Structures and ADTs
Outline Declaring Structures Accessing a Member in a structure variable Initialization of Structures
![Page 38: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/38.jpg)
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};
![Page 39: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/39.jpg)
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”, 80002211};
![Page 40: Review of Chapter 10: String and Pointers](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d6c550346895da74b9b/html5/thumbnails/40.jpg)
40
Chapter 12: Structures and ADTs
Summary Declaring Structures Accessing a Member in a structure variable
member operator “.”:o structure_variable.member_name
structure pointer operator “ -> ” :o pointer_to_structure -> member_name
Initialization of StructuresA structure variable can be followed by
o an equal sign = and o a list of constants contained within braceso If there are not enough values, the remaining
members are assigned the value zero.
Read Chapter 12.1 – 12. 6