Fundamentals of Programming Session 22 Instructor: Reza - - PowerPoint PPT Presentation

fundamentals of programming
SMART_READER_LITE
LIVE PREVIEW

Fundamentals of Programming Session 22 Instructor: Reza - - PowerPoint PPT Presentation

Fundamentals of Programming Session 22 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitels slides Sharif University of Technology Outlines Relationship between Pointers


slide-1
SLIDE 1

Fall 2014

Instructor: Reza Entezari-Maleki

Email: entezari@ce.sharif.edu

Sharif University of Technology

1

Fundamentals of Programming

Session 22

These slides have been created using Deitel’s slides

slide-2
SLIDE 2

Outlines

 Relationship between Pointers and Arrays  Arrays of Pointers  Fundamentals of Strings and Characters

2

slide-3
SLIDE 3

 Pointers can be used to do any operation involving array

subscripting.

 Assume that integer array b[5] and integer pointer variable bPtr

have been defined.

 Since the array name (without a subscript) is a pointer to the first

element of the array, we can set bPtr equal to the address of the first element in array b with the statement

 bPtr = b;  This statement is equivalent to taking the address of the array’s first

element as follows:

 bPtr = &b[ 0 ];

];

 Array element b[3] can alternatively be referenced with the pointer

expression

 *( bPtr + 3 )  The preceding notation is referred to as pointer/offset notation.

3

Relationship between Pointers and Arrays

slide-4
SLIDE 4

4

Relationship between Pointers and Arrays …

slide-5
SLIDE 5

5

Relationship between Pointers and Arrays …

slide-6
SLIDE 6

6

Relationship between Pointers and Arrays …

slide-7
SLIDE 7

 To further illustrate the interchangeability of arrays and

pointers, let’s look at the two string-copying functions— copy1 and copy2—in the program of Fig. 7.21.

 Both functions copy a string (possibly a character array) into

a character array.

 After a comparison of the function prototypes for copy1

and copy2, the functions appear identical.

 They

accomplish the same task; however, they’re implemented differently.

7

Relationship between Pointers and Arrays …

slide-8
SLIDE 8

8

Relationship between Pointers and Arrays …

slide-9
SLIDE 9

9

Relationship between Pointers and Arrays …

slide-10
SLIDE 10

 Arrays may contain pointers.  A common use of an array of pointers is to form an

array of strings, referred to simply as a string array.

 Each entry in the array is a string, but in C a string is

essentially a pointer to its first character.

 So each entry in an array of strings is actually a pointer

to the first character of a string.

 Consider the definition of string array suit, which

might be useful in representing a deck of cards.

 const char

const char *suit[ *suit[ 4 ] = { ] = { "Hearts" "Hearts", , "Diamonds" "Diamonds", , "Clubs" "Clubs", , "Spades" "Spades" }; };

10

Arrays of Pointers

slide-11
SLIDE 11

 The four strings are 7, 9, 6 and 7 characters long,

respectively.

 Although it appears as though these strings are being

placed in the suit array, only pointers are actually stored in the array (Fig. 7.22).

 Each

pointer points to the first character

  • f

its corresponding string.

 Thus, even though the suit array is fixed in size, it

provides access to character strings of any length.

 This flexibility is one example of C’s powerful data-

structuring capabilities.

11

Arrays of Pointers …

slide-12
SLIDE 12

12

Arrays of Pointers …

slide-13
SLIDE 13

 What will be the output of the following code?

int main(){ int a = 40; int *b = {5}; int *p = &b[0]; int y = a/*p /*divide a by *p*/; printf("y = %d\n", y); return 0; }

 Answer:

40

13

Question 1

slide-14
SLIDE 14

 A string in C is an array of characters ending in the null

character ('\0').

 The definitions

 char

char color[] = color[] = "blue" "blue"; const char const char *colorPtr colorPtr = = "blue" "blue";

each initialize a variable to the string "blue".

 The first definition creates a 5-element array color

containing the characters 'b', 'l', 'u', 'e' and '\0'.

 The second definition creates pointer variable

colorPtr that points to the string "blue" somewhere in memory.

14

Fundamentals of Strings and Characters

slide-15
SLIDE 15

 The character-handling library (<ctype.h>) includes

several functions that perform useful tests and manipulations of character data.

 Each function receives a character—represented as an

int—or EOF as an argument.

 EOF normally has the value –1, and some hardware

architectures do not allow negative values to be stored in char variables, so the character-handling functions manipulate characters as integers.

 Figure 8.1 summarizes the functions of the character-

handling library.

15

Character-Handling Library …

slide-16
SLIDE 16

16

Character-Handling Library …

slide-17
SLIDE 17

17

Character-Handling Library …

slide-18
SLIDE 18

18

Character-Handling Library …

slide-19
SLIDE 19

19

Character-Handling Library …

slide-20
SLIDE 20

20

Character-Handling Library …

slide-21
SLIDE 21

 Figure 8.4 demonstrates functions isspace, iscntrl,

ispunct, isprint and isgraph.

 Function isspace determines if a character is one of

the following white-space characters: space (' '), form feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t') or vertical tab ('\v').

21

Character-Handling Library …

slide-22
SLIDE 22

22

Character-Handling Library …

slide-23
SLIDE 23

23

Character-Handling Library …

slide-24
SLIDE 24

24

Character-Handling Library …

slide-25
SLIDE 25

 What will be the output of the following code?

int main(){ char arr[]="A B C D E F G H L J K M"; double *fptr; fptr=(double *)arr; fptr++; printf("%s",fptr); return 0; }

 Answer:

E F G H L J K M

25

Question 2