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

fundamentals of programming
SMART_READER_LITE
LIVE PREVIEW

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

Fundamentals of Programming Session 19 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitels slides Sharif University of Technology Outlines Using the const Qualifier with


slide-1
SLIDE 1

Fall 2013

Instructor: Reza Entezari-Maleki

Email: entezari@ce.sharif.edu

Sharif University of Technology

1

Fundamentals of Programming

Session 19

These slides have been created using Deitel’s slides

slide-2
SLIDE 2

Outlines

 Using the const Qualifier with Pointers  sizeof Operator

2

slide-3
SLIDE 3

 The const qualifier enables you to inform the compiler that

the value of a particular variable should not be modified.

 If an attempt is made to modify a value that is declared

const, the compiler catches it and issues either a warning

  • r an error, depending on the particular compiler.

 There are four ways to pass a pointer to a function: a non-

constant pointer to non-constant data, a constant pointer to nonconstant data, a non-constant pointer to constant data, and a constant pointer to constant data.

 Each of the four combinations provides different access

privileges.

 These are discussed in the next several examples.

3

Using the const Qualifier with Pointers

slide-4
SLIDE 4

 The highest level of data access is granted by a non-

constant pointer to non-constant data.

 In this case, the data can be modified through the

dereferenced pointer, and the pointer can be modified to point to other data items.

 Function convertToUppercase of Fig. 7.10

declares its parameter, a non-constant pointer to non-constant data called sPtr (char *sPtr), in line 21.

4

Using the const Qualifier with Pointers …

slide-5
SLIDE 5

5

Using the const Qualifier with Pointers …

slide-6
SLIDE 6

6

Using the const Qualifier with Pointers …

slide-7
SLIDE 7

 A non-constant pointer to constant data can be

modified to point to any data item of the appropriate type, but the data to which it points cannot be modified.

 Such a pointer might be used to receive an array

argument to a function that will process each element without modifying the data.

 For

example, function printCharacters (Fig. 7.11) declares parameter sPtr to be of type const char * (line 22).

7

Using the const Qualifier with Pointers …

slide-8
SLIDE 8

8

Using the const Qualifier with Pointers …

slide-9
SLIDE 9

9

Using the const Qualifier with Pointers …

slide-10
SLIDE 10

 Figure 7.12 illustrates the attempt to compile a

function that receives a non-constant pointer (xPtr) to constant data.

 This function attempts to modify the data pointed to

by xPtr in line 20—which results in a compilation error.

10

Using the const Qualifier with Pointers …

slide-11
SLIDE 11

11

Using the const Qualifier with Pointers …

slide-12
SLIDE 12

12

Using the const Qualifier with Pointers …

slide-13
SLIDE 13

 A constant pointer to non-constant data always

points to the same memory location, and the data at that location can be modified through the pointer.

 This is the default for an array name.  Pointers

that are declared const must be initialized when they’re defined (if the pointer is a function parameter, it’s initialized with a pointer that is passed to the function).

 Figure 7.13 attempts to modify a constant pointer.

13

Using the const Qualifier with Pointers …

slide-14
SLIDE 14

14

Using the const Qualifier with Pointers …

slide-15
SLIDE 15

15

Using the const Qualifier with Pointers …

slide-16
SLIDE 16

 The least access privilege is granted by a constant

pointer to constant data.

 Such a pointer always points to the same memory

location, and the data at that memory location cannot be modified.

 This is how an array should be passed to a function that

  • nly looks at the array using array subscript notation

and does not modify the array.

 Figure 7.14 defines pointer variable ptr (line 13) to be

  • f type const int *const, which is read from right

to left as “ptr is a constant pointer to an integer constant.”

16

Using the const Qualifier with Pointers …

slide-17
SLIDE 17

17

Using the const Qualifier with Pointers …

slide-18
SLIDE 18

18

Using the const Qualifier with Pointers …

slide-19
SLIDE 19

 What will be the output of the following program?

int main() { char *p; p="hello"; printf("%s\n", *&*&p); return 0; }

 Answer:

hello

19

Question 1

slide-20
SLIDE 20

 What will be the output of the following program?

int main() { printf("%c\n", 5["Salaam"]); return 0; }

 Answer:

m

20

Question 2

slide-21
SLIDE 21

 What will be the output of the following program?

int main() { char *str; str = "A%d\n"; str++; str++; printf(str-1, 300); return 0; }

 Answer:

300

21

Question 3

slide-22
SLIDE 22

 What will be the output of the following program?

int main() { int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8}; int *p, *q; p = &arr[1][1][1]; q = arr[0][1]; printf("%d, %d\n", *p, *q); return 0; }

 Answer:

8, 3

22

Question 4

slide-23
SLIDE 23

 C provides the special unary operator sizeof to

determine the size in bytes of an array (or any other data type) during program compilation.

 When applied to the name of an array as in Fig. 7.16

(line 14), the sizeof operator returns the total number

  • f bytes in the array as an integer.

 Variables of type float are normally stored in 4 bytes

  • f memory, and array is defined to have 20 elements.

 Therefore, there are a total of 80 bytes in array.

23

sizeof Operator

slide-24
SLIDE 24

24

sizeof Operator …

slide-25
SLIDE 25

25

sizeof Operator …

slide-26
SLIDE 26

 The number of elements in an array also can be determined

with sizeof.

 For example, consider the following array definition:

 doubl

uble real[ eal[ 22 22 ]; ];

 Variables of type double normally are stored in 8 bytes of

memory.

 Thus, array real contains a total of 176 bytes.  To determine the number of elements in the array, the

following expression can be used:

 si

sizeo eof( ( real eal ) / / si sizeof eof( re ( real[ l[ 0 ] ) ] )

 Figure 7.17 calculates the number of bytes used to store each

  • f the standard data types.

26

sizeof Operator …

slide-27
SLIDE 27

27

sizeof Operator …

slide-28
SLIDE 28

28

sizeof Operator …

slide-29
SLIDE 29

 Operator sizeof can be applied to any variable name, type or

value (including the value of an expression).

 When applied to a variable name (that is not an array name) or

a constant, the number of bytes used to store the specific type

  • f variable or constant is returned.

 The parentheses used with sizeof are required if a type name

with two words is supplied as its operand (such as long double or unsigned short).

 Omitting the parentheses in this case results in a syntax error.  The parentheses are not required if a variable name or a one-

word type name is supplied as its operand, but they can still be included without causing an error.

29

sizeof Operator …

slide-30
SLIDE 30

 What will be the output of the program assuming that the array

begins at location 1002?

int main() { int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8}; int *p, *q; p = &arr[1][1][1]; q = (int*) arr; printf("%d, %d\n", *p, *q); return 0; }

 Answer:

8 10

30

Question 5

slide-31
SLIDE 31

 What will be the output of the following program?

int main(){ register int a = 25; int *p; p=&a; printf("%d ",*p); return 0; }

 Answer:

It depends! Basically it should generate an error.

31

Question 6

slide-32
SLIDE 32

 What will be the output of the following program?

int power(int**); int main() { int a=5, *aa; /* Address of 'a' is 1000 */ aa = &a; a = power(&aa); printf("%d\n", a); return 0; } int power(int **ptr) { int b; b = **ptr***ptr; return (b); }

 Answer:

25

32

Question 7

slide-33
SLIDE 33

 What does the following code do in C?

int main() { char str[80]; char token[10]; char *p , *q; printf("Enter a sentence: "); scanf("%s",str); p = str; while (*p) { q = token; while (*p) { *q = *p; q++ ; p++; } if (*p) p++; *q = '\0'; printf("%s\n" , token); } return 0; }

 Answer: It gets a sentence and prints it!

33

Question 8

slide-34
SLIDE 34

 What will be the output of the following program?

int main() { printf("%d, %d\n", sizeof(NULL), sizeof("")); return 0; }

 Answer:

4, 1

34

Question 9

slide-35
SLIDE 35

 What will be the output of the following program?

int main() { int *p1,**p2; double *q1,**q2; printf("%d %d ",sizeof(p1),sizeof(p2)); printf("%d %d",sizeof(q1),sizeof(q2)); return 0; }

 Answer:

4 4 4 4

35

Question 10