fundamentals of programming
play

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


  1. 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

  2. Outlines  Relationship between Pointers and Arrays  Arrays of Pointers  Fundamentals of Strings and Characters 2

  3. 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

  4. Relationship between Pointers and Arrays … 4

  5. Relationship between Pointers and Arrays … 5

  6. Relationship between Pointers and Arrays … 6

  7. 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

  8. Relationship between Pointers and Arrays … 8

  9. Relationship between Pointers and Arrays … 9

  10. 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

  11. 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

  12. Arrays of Pointers … 12

  13. 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

  14. 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

  15. 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

  16. Character- Handling Library … 16

  17. Character- Handling Library … 17

  18. Character- Handling Library … 18

  19. Character- Handling Library … 19

  20. Character- Handling Library … 20

  21. 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

  22. Character- Handling Library … 22

  23. Character- Handling Library … 23

  24. Character- Handling Library … 24

  25. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend