# Pesky Pointers Quiz

Pointers are typically the one thing people coming from Javaland struggle with when encountering C for the first time. If you come bottom up from assemblerland you are OK.

Here is our pesky pointers quiz. You will get a marked up copy of your answer sheet back. We will grade it but it will have zero impact on your course grade. This is purely fun (in a torturous sense of course)!

### Slippery Strings

This set of problems asks you to guess the execution results of C code. In this exercise, you will understand how C handles strings and how to write correct string processing code. Assume that:

char* p = "HELLO";

for all questions.

• printf("Print p: %s\n", p);

• printf("Print p[1]: %c\n", p[1]);

• printf("Print *p+2: %c\n", *p+2);

• printf("Print p: %c\n", p);

Answer: Print p: (STRANGE RANDOM CHARACTERS)

• printf("Print (p+1): %s\n", (p+1));

### 1 Array!

We have the following intialization

int a[3]; a[0]=3; a[1]=6; a[2]=9;
int *b=a;
short *c=a;

for all questions in this section.

• printf("%d\n", a);

• printf("%d\n", *a);

• printf("%d\n", *(a+1));

• printf("%d\n", *(a+3));

• printf("%d\n", *a+1);

• printf("%d\n", b[2]);

• printf("%d\n", c[0]);

Answer: 3, first half of a[0] is casted into a short int

### 2 Double Array!

We have the following initialization

int B[2][2];
B[0][0]=1;
B[0][1]=2;
B[1][0]=3;
B[1][1]=4;

for all questions in this section.

•  printf("%u\n", B);

•  printf("%u\n", &B[0][0]);

•  printf("%u\n", B[1]);

•  printf("%u\n", *B[0]);

• Can we succeed in compiling by writing:
int D[2][2] = B;

• Can we succeed in compiling by writing:
int D[][] = B;

• Can we succeed in compiling by writing:
int *D = B;

When you want to declare a array and initialize it at the same time, the right way is to do as follows:

int D[2][2] = {{1, 2}, {3, 4}};

or:

int D[] = {1, 2, 3, 4};

• Can we succeed in compiling by writing:
int *D = B;
printf("%u\n", D[1][1]);

• breath...

Following exact string questions were not mentioned today, but similar questions were talked about.

• int *D = B;
printf("%u\n", D+2);

• int *D = B;
printf("%u\n", *(D+2));

### 3 Pointer of a Pointer

This set of questions ask you to think about the value and equivalent expressions of an expression. You will need the similar skill in Lab 4.Suppose:

int a = 12;
int* b = &a;
int** c = &b;

•  *b