Programming II Worksheet - Pointers
Programming II Worksheet - Pointers
1. The _______ operator is used with a pointer to dereference the address contained in
the pointer.
6. Write the function header for a function called process that accepts a pointer to an
integer and two floating point numbers (in that order) and returns a double number.
Which of the following expressions are not allowed? Why or why not?
(a) p = &x;
(b) p = &d;
(c) q = &x;
(d) q = &d;
(e) p = x;
8. State the values that would be printed in the code fragment below.
int x = 2, y;
int *Ptr;
Ptr = &x;
y = *Ptr;
*Ptr = *Ptr + 10;
printf("%d %d %d\n", x, y, *Ptr);
y = *Ptr + 1;
(*Ptr)++;
printf("%d %d\n",y,*Ptr);
9. Given the following declarations, show the contents of memory after each set of
statements is executed.
1
int i = 4, q = 7, *f = NULL, *s = NULL;
float c = 2.5, *j = NULL;
f = &i;
j = &c; 1001 i 2001 q 3001 f 4001 s 5001 c 6001 j
s = f;
*j = *j / 0.5;
1001 i 2001 q 3001 f 4001 s 5001 c 6001 j
*s = i * q;
q++; 1001 i 2001 q 6001 j
3001 f 4001 s 5001 c
f = &q;
c = *j + 12.5;
1001 i 2001 q 3001 f 4001 s 5001 c 6001 j
*f = *s – 1;
s = NULL;
1001 i 2001 q 3001 f 4001 s 5001 c 6001 j
int i, j = 25;
int *Iptr, *Jptr = &j;
….
*Jptr = j + 5;
i = *Jptr + 5;
Iptr = Jptr;
*Iptr = i + j;
Suppose each integer quantity occupies 4 bytes of memory. If the value assigned
to i begins at address 11200 and the value assigned to j begins at address 11204,
then
(a) What value is represented by &i?
(b) What value is represented by &j?
(c) What value is assigned to Jptr?
(d) What value is assigned to *Jptr?
(e) What value is assigned to i?
(f) What value is represented by Iptr?
(g) What final value is assigned to *Iptr?
(h) What value is represented by Iptr + 2?
(i) What value is represented by the expression (*Iptr + 2)?
(j) What value is represented by the expression *(Iptr + 2)?
i) printf("%p”, p);
ii) printf("%d, %p", (*q), q);
iii) printf("%p”, (q + 1));
iv) printf("%p, %d",r, *r);
v) printf("%d", (*p)++ - (*w));
vi) printf("%d", *s * w + *(R + 1));
vii) printf("%d, %p", (*q), q);
2
viii) printf("%p %d", (p - 1), *(p - 1));
8. Complete the program below using pointer notation. The program should convert a
given string to uppercase letters.
main( )
{
char string[] = “Programming is still fun”;
printf("%s", string);
changeToUppercase(string);
printf("%s", string);
}
}
}