Fundamentals of Programming Session 22 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel’s slides Sharif University of Technology
Outlines Relationship between Pointers and Arrays Arrays of Pointers Fundamentals of Strings and Characters 2
Relationship between Pointers and Arrays 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 … 4
Relationship between Pointers and Arrays … 5
Relationship between Pointers and Arrays … 6
Relationship between Pointers and Arrays … 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’re They accomplish the same task; however, implemented differently. 7
Relationship between Pointers and Arrays … 8
Relationship between Pointers and Arrays … 9
Arrays of Pointers 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 … 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 of 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 … 12
Question 1 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
Fundamentals of Strings and Characters 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
Character- Handling Library … 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 … 16
Character- Handling Library … 17
Character- Handling Library … 18
Character- Handling Library … 19
Character- Handling Library … 20
Character- Handling Library … 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 … 22
Character- Handling Library … 23
Character- Handling Library … 24
Question 2 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
Recommend
More recommend