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
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
Fall 2014
Sharif University of Technology
1
Session 22
These slides have been created using Deitel’s slides
2
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
5
6
To further illustrate the interchangeability of arrays and
Both functions copy a string (possibly a character array) into
After a comparison of the function prototypes for copy1
They
7
8
9
Arrays may contain pointers. A common use of an array of pointers is to form an
Each entry in the array is a string, but in C a string is
So each entry in an array of strings is actually a pointer
Consider the definition of string array suit, which
const char
const char *suit[ *suit[ 4 ] = { ] = { "Hearts" "Hearts", , "Diamonds" "Diamonds", , "Clubs" "Clubs", , "Spades" "Spades" }; };
10
The four strings are 7, 9, 6 and 7 characters long,
Although it appears as though these strings are being
Each
Thus, even though the suit array is fixed in size, it
This flexibility is one example of C’s powerful data-
11
12
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
A string in C is an array of characters ending in the null
The definitions
char
char color[] = color[] = "blue" "blue"; const char const char *colorPtr colorPtr = = "blue" "blue";
The first definition creates a 5-element array color
The second definition creates pointer variable
14
The character-handling library (<ctype.h>) includes
Each function receives a character—represented as an
EOF normally has the value –1, and some hardware
Figure 8.1 summarizes the functions of the character-
15
16
17
18
19
20
Figure 8.4 demonstrates functions isspace, iscntrl,
Function isspace determines if a character is one of
21
22
23
24
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