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
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
Fall 2013
Sharif University of Technology
1
Session 19
These slides have been created using Deitel’s slides
2
The const qualifier enables you to inform the compiler that
If an attempt is made to modify a value that is declared
There are four ways to pass a pointer to a function: a non-
Each of the four combinations provides different access
These are discussed in the next several examples.
3
The highest level of data access is granted by a non-
In this case, the data can be modified through the
Function convertToUppercase of Fig. 7.10
4
5
6
A non-constant pointer to constant data can be
Such a pointer might be used to receive an array
For
7
8
9
Figure 7.12 illustrates the attempt to compile a
This function attempts to modify the data pointed to
10
11
12
A constant pointer to non-constant data always
This is the default for an array name. Pointers
Figure 7.13 attempts to modify a constant pointer.
13
14
15
The least access privilege is granted by a constant
Such a pointer always points to the same memory
This is how an array should be passed to a function that
Figure 7.14 defines pointer variable ptr (line 13) to be
16
17
18
What will be the output of the following program?
int main() { char *p; p="hello"; printf("%s\n", *&*&p); return 0; }
Answer:
hello
19
What will be the output of the following program?
int main() { printf("%c\n", 5["Salaam"]); return 0; }
Answer:
m
20
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
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
C provides the special unary operator sizeof to
When applied to the name of an array as in Fig. 7.16
Variables of type float are normally stored in 4 bytes
Therefore, there are a total of 80 bytes in array.
23
24
25
The number of elements in an array also can be determined
For example, consider the following array definition:
doubl
uble real[ eal[ 22 22 ]; ];
Variables of type double normally are stored in 8 bytes of
Thus, array real contains a total of 176 bytes. To determine the number of elements in the array, the
si
sizeo eof( ( real eal ) / / si sizeof eof( re ( real[ l[ 0 ] ) ] )
Figure 7.17 calculates the number of bytes used to store each
26
27
28
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
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
What will be the output of the program assuming that the array
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
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
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
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
What will be the output of the following program?
int main() { printf("%d, %d\n", sizeof(NULL), sizeof("")); return 0; }
Answer:
4, 1
34
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