0% found this document useful (0 votes)
77 views3 pages

Programming II Worksheet - Pointers

This document contains a programming worksheet on pointers with 9 questions. Some key points: 1) It covers pointer basics like dereferencing with *, pointer declarations and definitions, passing pointers to functions, and pointer arithmetic. 2) Questions involve determining valid pointer operations, initializing and accessing values through pointers, and tracing code that uses pointers. 3) The last question involves writing a function that uses a pointer to modify a string by changing each character to uppercase.

Uploaded by

Leia Michaelson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views3 pages

Programming II Worksheet - Pointers

This document contains a programming worksheet on pointers with 9 questions. Some key points: 1) It covers pointer basics like dereferencing with *, pointer declarations and definitions, passing pointers to functions, and pointer arithmetic. 2) Questions involve determining valid pointer operations, initializing and accessing values through pointers, and tracing code that uses pointers. 3) The last question involves writing a function that uses a pointer to modify a string by changing each character to uppercase.

Uploaded by

Leia Michaelson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Programming II Worksheet – Pointers

1. The _______ operator is used with a pointer to dereference the address contained in
the pointer.

2. Explain the meaning of the following declarations


float a, b
float *pa, *pb;

char c1, c2, c3;


char *pc1, *pc2, *pc3 = &c1;

3. Which of the following statements will not add 1 to a variable?


a. a++;
b. a += 1;
c. a = a + 1;
d. *p = *p + 1;
e. *p++;

4. Which of the following defines a pointer variable to an integer?


a. int& ptr;
b. int&& ptr;
c. int* ptr;
d. int** ptr;
e. int^ ptr;

5. Which of the following defines and initialized a pointer to the address of x?


a. int* ptr = *x;
b. int* ptr = ^x;
c. int * ptr = &x;
d. int& ptr = *x;
e. int& ptr = ^x;

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.

7. Given the following declarations:


int x;
double d;
int *p;
double *q;

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

6. A C program contains the following statements:

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)?

7. What will be printed after execution of a following program block?

int x = 6; //x address:2143010


int y = 3; //y address:2143014
int w = 9; //w address:2143018
int z = 7; //z address: 2143022

int *p = &w; //p address:2143136


int *q = &y; //q address:2143132
int *r = --q; //r address:2143128
int *s = p + 1;

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);
}

void changeToUppercase (char *str)


{
while (*str != ‘\0’) {

(what goes here?)

}
}

You might also like