logo

UTK Notes


Clicker Questions - 03-Pointers

Behold the procedure a():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
typedef unsigned long UL;

void a(unsigned long *d,
       unsigned int  *j,
       unsigned int  *k)
{
    int i;
    printf("d     = 0x%016lx\n", (UL) d);
    printf("j     = 0x%016lx\n", (UL) j);
    printf("k     = 0x%016lx\n\n", (UL) k);
    printf("*j    = 0x%08x\n", *j);
    printf("*k    = 0x%08x\n\n", *k);

    for (i = 0; i < 4; i++) {
        printf("d[%d]  = 0x%16lx\n", i, d[i]);
    }
}

When I run this, I get the following output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
d    = 0x00007f9cec401798
j    = 0x00007f9cec4017e8
k    = 0x00007f9cec401790

*j   = 0xe3c017ff
*k   = 0x00c01705

d[0] = 0x00007f9cec4017c8
d[1] = 0x00007f9cec4017e8
d[2] = 0x00007f9cec4017e0
d[3] = 0x00007f9cec401798
d[4] = 0x00007f9cec4017d8
d[5] = 0x00007f9cec4017b0
d[6] = 0x00007f9cec4017c8
d[7] = 0x00007f9cec4017e8
d[8] = 0x00007f9cec401798
d[9] = 0x00007f9cec4017d8

My machine has 8 byte pointers and is in little endian. Please answer the following questions:

Question 1: What is the byte, in hex, at address 0x00007f9cec4017e8?

Answer This address is equal to j, so this is the first byte of *j. Remember that the machine is little endian, so the first byte of *j is 0xff.

Question 1: 0xff

Question 2: What is the byte, in hex, at address 0x00007f9cec4017e9?

Answer This is the second byte of *j;

Question 2: 0x17

Question 3: What is the byte, in hex, at address 0x00007f9cec4017a8?

Answer We need to use d to answer this. The pointer value in the question is 16 bytes greater than d. So, this is the first byte of d[2]. Again - little endian -- so 0xe0.

Question 3: 0xe0

Question 4: What will the following printf() statement print?

1
printf("0x%08lx\n", k[4]);
Answer k[4] is equal to *(k+4). Since each element of k is four bytes, (k+4) is 0x00007f9cec4017a0. That's eight bytes more than d, so this is the first four bytes of d[1]: Oxecd017e8.

Question 4: 0xec4017e8

Question 5: What wil the following printf() statement print?

1
2
3
4
unsigned int **x;

x = (unsigned int **) d[5];
printf("0x%08x\n", **x);
Answer The hardest question. d[5] is 0x00007f9cec4017b0, which is (d+3). So, *x is equal to d[3], which is 0x00007f9cec401798. That address is equal to d, so **x is equal to the first four bytes of d[0]: Oxec4017c8.

Question 5: 0xec4017c8
Extra Answer On an exam, I would cut-and-paste d, and then add everything I know about d, j and k (and even x). I'll put *'s for bytes I don't know. I'll also label the bytes with their byte number in hex, so that the little endian is less confusing. From this labeled drawing, I can get all of the answers. It may help you understand them, too, so this is a good exercise for you:
Answers-Table

PDF Download, 2022 Class Stats, 2023 Class Stats, 2024 Class Stats