sample-midterm-2-ans

sample-midterm-2-ans - Question 1(pointer and structures...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Question 1. (pointer and structures) What is the output of the following program? #include<stdio.h> #include<string.h> #include<stdlib.h> struct sample{ int a; int b; char name[21]; }; void print_struct (const struct sample *p){ printf("a : %d\nb : %d\nname : %s\n", p->a, p->b, p->name); } int main(){ struct sample * p = malloc(sizeof(struct sample)*2); struct sample *q; p->a=10; p->b=11; strcpy(p->name,"cs240"); print_struct(p); (p+1)->a=12; (p+1)->b=13; strcpy((p+1)->name,"midterm"); print_struct((p+1)); q = (p+strlen((p+5%2)->name)%2); print_struct(q); return 0; } Answer: a : 10 b : 11 name : cs240 a : 12 b : 13 name : midterm a : 12 b : 13 name : midterm The key to answering this question is realizing that (a) the region allocated by malloc is partitioned to contain space for two 'sample' structures, and that (b) the expression (p+1)->a refers to the second of these structures. Thus, the first printf prints the first structure, the second prints the second (whose contents are assigned by structure manipulating expressions involving (p+1)); the third printf refers to the second structure as well, since the value of (p+strlen((p+5%2)->name)%2) = p+strlen((p+1)->name)%2 = p+strlen("midterm")%2 = p+1. ================================================================ Question 2. (casts) What is the output of the following program? #include <stdio.h>
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
int main() { char array[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}; char *cptr = (char *)&array; int *iptr = (int *)&array; cptr += 2; iptr += 2; printf("%c\n", *cptr); printf("%c\n", *(char *)iptr); cptr = (char *)(((int *)cptr) + 1); iptr = (int *) (((char *)iptr) + 1); printf("%c\n", *cptr); printf("%c\n", *(char *)iptr); } Answer: c i g j cptr is a pointer to an array of characters named array. The assignment cptr+=2 sets the pointer to character 'c' defined in this array. On the other hand, iptr treats 'array' as a collection of integers; the assignment iptr+=2 thus has iptr index 8 bytes into the array, or the character i. cptr is subsequently cast to an integer and incremented by 1 (i.e., 4 bytes) which now has it pointing to g; conversely, iptr is cast to a character pointer, and incremented by 1 which now has it pointing to j. ================================================================ Question 3. (pointers) What are the values of i,j,ptr, *ptr, pptr, and **pptr after executing the following (write '?' if the value is unknown): int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = 8; **pptr = 9; *ptr = &i; *ptr = 2 Answer: i = 7 j = 10 ptr = address 8 *ptr = 2 **pptr = 2
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

sample-midterm-2-ans - Question 1(pointer and structures...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online