Fundamentals of Programming Session 20 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology
Outlines sizeof Operator Pointer Expressions and Pointer Arithmetic 2
sizeof Operator 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 of bytes in the array as an integer. Variables of type float are normally stored in 4 bytes of memory, and array is defined to have 20 elements. Therefore, there are a total of 80 bytes in array. 3
4 sizeof Operator
5 sizeof Operator
sizeof Operator 6 The number of elements in an array also can be determined with sizeof. For example, consider the following array definition: double real[ 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: sizeof( real ) / sizeof( real[ 0 ] ) Figure 7.17 calculates the number of bytes used to store each of the standard data types.
7 sizeof Operator
8 sizeof Operator
sizeof Operator 9 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 of 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 oneword type name is supplied as its operand, but they can still be included without causing an error.
Question 1 What will be the output of the following program? int main() { printf("%d, %d\n", sizeof(null), sizeof("")); return 0; } Answer: 4, 1 10
Question 2 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: 11 4 4 4 4
Question 3 12 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
Question 4 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. 13
Pointer Expressions and Pointer Arithmetic 14 Pointers are valid operands in arithmetic expressions, assignment expressions and comparison expressions. A pointer may be incremented (++) or decremented (--), an integer may be added to a pointer (+ or +=), an integer may be subtracted from a pointer (- or -=) and one pointer may be subtracted from another. If a pointer is being incremented or decremented by one, the increment (++) and decrement (--) operators can be used. Either of the statements ++vptr; vptr++;
Pointer Expressions and Pointer Arithmetic 15 Assume that array int v[5] has been defined and its first element is at location 3000 in memory. Assume pointer vptr has been initialized to point to v[0] i.e., the value of vptr is 3000. Figure 7.18 illustrates this situation for a machine with 4-byte integers. In conventional arithmetic, 3000 + 2 yields the value 3002. This is normally not the case with pointer arithmetic. The number of bytes depends on the object s data type. For example, the statement vptr += 2; would produce 3008 (3000 + 2 * 4) assuming an integer is stored in 4 bytes of memory.
16 Pointer Expressions and Pointer Arithmetic
Pointer Expressions and Pointer Arithmetic For example, if vptr contains the location 3000, and v2ptr contains the address 3008, the statement x = v2ptr - vptr; would assign to x the number of array elements from vptr to v2ptr, in this case 2 (not 8). Pointer arithmetic is meaningless unless performed on an array. A pointer can be assigned to another pointer if both have the same type. The exception to this rule is the pointer to void (i.e., void *), which is a generic pointer that can represent any pointer type. 17
Pointer Expressions and Pointer Arithmetic All pointer types can be assigned a pointer to void, and a pointer to void can be assigned a pointer of any type. In both cases, a cast operation is not required. A pointer to void cannot be dereferenced. Pointers can be compared using equality and relational operators, but such comparisons are meaningless unless the pointers point to elements of the same array. 18 A comparison of two pointers pointing to elements in the same array could show, for example, that one pointer points to a higher-numbered element of the array than the other pointer does.
Question 5 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: 19 8 10
Question 6 20 What does the following code do in C? int main() { char str[80]; char token[80]; 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!
Question 7 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 21